SQL XML Insert multiple elements - sql-server

Please help! I have a field of xml within a database. I am trying to insert a new element for each element in the field. E.g:
<some-element>
<a> </a>
<b> </b>
</some-element>
<some-element>
<a> </a>
<b> </b>
</some-element>
would insert element c and go to:
<some-element>
<a> </a>
<b> </b>
<c> </c>
</some-element>
<some-element>
<a> </a>
<b> </b>
<c> </c>
</some-element>
I know I can use #fieldname.modify('insert #CElement into (/some-element)[1]') but this only changes the first element! I want to repeat this for all elements.
Any help would be very much appreciated! (I am using SQL server 2008)
Moved from answer:
Thank you for your reply! Perhaps it would help if I was more specific... I am describing a simple table of data within the xml. I am trying to add a new column. I can add the column within the column descriptions but I need to add the column element to all the rows. (It would not be a simple task to change the structure and I would like to avoid this!) E.g:
<Table>
<Columns>
<Column ID="0">
<Column-Name>0NAME</Column-Name>
</Column>
<Column ID="1">
<Column-Name>1NAME</Column-Name>
</Column>
<Column ID="2">
<Column-Name>2NAME</Column-Name>
</Column>
<Column ID="3">
<Column-Name>!!!! THIS COLUMN IS BEING ADDED !!!!!</Column-Name>
</Column>
</Columns>
<Rows>
<Row ID="0">
<C ID="0">0 contents here</C>
<C ID="1">0 contents here</C>
<C ID="2">0 contents here</C>
<!-- NEW COLUMN NEEDS TO BE CREATED HERE -->
</Row>
<Row ID="1">
<C ID="0">1 contents here</C>
<C ID="1">1 contents here</C>
<C ID="2">1 contents here</C>
<!-- NEW COLUMN NEEDS TO BE CREATED HERE -->
</Row>
<Row ID="2">
<C ID="0">2 contents here</C>
<C ID="1">2 contents here</C>
<C ID="2">2 contents here</C>
<!-- NEW COLUMN NEEDS TO BE CREATED HERE -->
</Row>
</Rows>
</Table>

You can recreate your XML, e.g.:
declare #x xml = '<some-element>
<a>1</a>
<b>2</b>
</some-element>
<some-element>
<a>3</a>
<b>4</b>
</some-element>'
select #x.query
('
for $e in some-element
return
<some-element>
{ $e/a }
{ $e/b }
<c/>
</some-element>
')

Adapting the answer provided by Kirill Polishchuk to the new XML structure:
select #XML.query
('
for $c in Table/Columns
return
<Table>
<Columns>
{ $c/* }
</Columns>
<Rows>
{
for $r in Table/Rows/Row
return
<Row>
{ $r/* }
<C ID="3"/>
</Row>
}
</Rows>
</Table>
')

Related

Basex Xquery filter retrieved data

<a id="aId" deleted="false">
<b id="bIdOne" deleted="false">
<c>cValue</c>
<d id="dId" deleted="true">
<e>eValue</e>
</d>
<f id="fId" deleted="false">
<g>gValue</g>
</f>
</b>
<b id="bIdTwo" deleted="true">
<c>cValue</c>
<d id="dId" deleted="true">
<e>eValue</e>
</d>
<f id="fId" deleted="false">
<g>gValue</g>
</f>
</b>
<b id="bIdThree" deleted="false">
<c>cValue</c>
<d id="dId" deleted="true">
<e>eValue</e>
</d>
<f id="fId" deleted="true">
<g>gValue</g>
</f>
</b>
<Y><yValue</Y>
</a>
Expected Output :
retrieve list of b and its child where deleted attribute is false, Expected output is below
query should be something like :
for $x in $data//descendant::[(#deleted=\"false\" or empty(#deleted))] return $x/b";
<b id="bIdOne" deleted="false">
<c>cValue</c>
<f id="fId" deleted="false">
<g>gValue</g>
</f>
</b>
<b id="bIdThree" deleted="false">
<c>cValue</c>
<Y><yValue</Y>
</b>
You are probably looking for something like this:
let $data := [your code above, fixed - your <Y> is in the wrong place for your output]
return
element root
{
for $e1 in $data//b[#deleted="false"]
let $dat :=$e1//*[not(#deleted="true")][not(ancestor::*[#deleted="true"])]
return
element{name($e1)}{$e1/#*,$dat}
}
Output should be your desired output.

Exclude a grandchild from XML using XPath

Trying to exclude a specific grandchild when reading an XML structure using SQL Server 2017.
I have an example structure as follows:
<root>
<a id="parent">
<b id = "child1">
<c id = "grandchild1"/>
<c id = "grandchild2"/>
</b>
<b id = "child2">
<c id = "grandchild3"/>
<c id = "grandchild4"/>
</b>
</a>
</root>
I can exclude a specific child using c[not(contains(#id,"grandchild3"))] when I navigate to for example the node <b id = "child2">.
But how would I exclude that specific grandchild when processing the rest of the XML from the root node?
What i would like to receive, so I can write it to a file, is a structure like this:
<root>
<a id="parent">
<b id = "child1">
<c id = "grandchild1"/>
<c id = "grandchild2"/>
</b>
<b id = "child2">
<c id = "grandchild4"/>
</b>
</a>
</root>
As can be seen grandchild3 is now missing from this structure.
Doing this in a SELECT is surprisingly involved, but fortunately you don't need that. You can make a copy of your xml, modify it with .modify, then use that:
declare #original xml = '<root>
<a id="parent">
<b id = "child1">
<c id = "grandchild1"/>
<c id = "grandchild2"/>
</b>
<b id = "child2">
<c id = "grandchild3"/>
<c id = "grandchild4"/>
</b>
</a>
</root>'
declare #new xml = #original
set #new.modify('delete //c[#id="grandchild3"]')
select #new
gives
<root>
<a id="parent">
<b id="child1">
<c id="grandchild1" />
<c id="grandchild2" />
</b>
<b id="child2">
<c id="grandchild4" />
</b>
</a>
</root>
as desired.
The xpath //c[#id="grandchild3"] means "Any c node (anywhere in the document) with an id attribute with value grandchild3".

T-SQL Find node with matching text and extract value from sibling node in XML field

I've a table with an xml field having content like this:
<ContentBlock xmlns="http://corp.com/wdpr/ContentBlock">
<key xlink:href="tcm:0-133036" xlink:title="entityCard" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<template xlink:href="tcm:0-132970" xlink:title="card-header-read-more-all-media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<section>
<name xlink:href="tcm:0-132988" xlink:title="header" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Make a Friend</p>
</content>
</section>
<section>
<name xlink:href="tcm:0-133110" xlink:title="readMore" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals at the only petting zoo in Disney’s Animal
Kingdom park.
</p>
</content>
</section>
<section>
<name xlink:href="tcm:0-132939" xlink:title="readAll" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals.
</p>
<p xmlns="http://www.w3.org/1999/xhtml">Pet, brush and feed domesticated creatures
</p>
<ul xmlns="http://www.w3.org/1999/xhtml">
<li xmlns="http://www.w3.org/1999/xhtml">Goats</li>
</ul>
<p xmlns="http://www.w3.org/1999/xhtml">Handy animal brushes are available .
</p>
<p xmlns="http://www.w3.org/1999/xhtml">
<strong xmlns="http://www.w3.org/1999/xhtml">Keeping Clean</strong>
<br xmlns="http://www.w3.org/1999/xhtml"/>Guests are encouraged to cleanse.</p>
</content>
</section>
<media>
<name xlink:href="tcm:0-201994" xlink:title="media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<key xlink:href="tcm:0-132952" xlink:title="170 x 96" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<image xlink:href="tcm:0-231377" xlink:title="section-01.jpg" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</media>
</ContentBlock>
My goal is to have a query where I can find (for example) the <p> that contains the text "Make a Friend", then get the xlink:href of the <name> element in the same <section> tag.
I tried a few options like in this posts: here and here but I'm not getting the results I need.
This doesn't work
SELECT a.value1,
x.XmlCol.value('(section/content/p)[1]','VARCHAR(100)') AS SendMethod
FROM #test a
CROSS APPLY a.AppConfig.nodes('/ContentBlock') x(XmlCol)
WHERE x.XmlCol.exist('section/content/p[contains(.,"Make a Friend")]') = 1
How can I get it?
Thanks in advance. Guillermo.
Try it like this:
DECLARE #mockup TABLE(ID INT IDENTITY,YourXML XML);
INSERT INTO #mockup VALUES
('<ContentBlock xmlns="http://corp.com/wdpr/ContentBlock">
<key xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-133036" xlink:title="entityCard" xlink:type="simple" />
<template xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132970" xlink:title="card-header-read-more-all-media" xlink:type="simple" />
<section>
<name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132988" xlink:title="header" xlink:type="simple" />
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Make a Friend</p>
</content>
</section>
<section>
<name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-133110" xlink:title="readMore" xlink:type="simple" />
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals at the only petting zoo in Disney’s Animal
Kingdom park.
</p>
</content>
</section>
<section>
<name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132939" xlink:title="readAll" xlink:type="simple" />
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals.
</p>
<p xmlns="http://www.w3.org/1999/xhtml">Pet, brush and feed domesticated creatures
</p>
<ul xmlns="http://www.w3.org/1999/xhtml">
<li xmlns="http://www.w3.org/1999/xhtml">Goats</li>
</ul>
<p xmlns="http://www.w3.org/1999/xhtml">Handy animal brushes are available .
</p>
<p xmlns="http://www.w3.org/1999/xhtml">
<strong xmlns="http://www.w3.org/1999/xhtml">Keeping Clean</strong>
<br xmlns="http://www.w3.org/1999/xhtml" />Guests are encouraged to cleanse.</p>
</content>
</section>
<media>
<name xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-201994" xlink:title="media" xlink:type="simple" />
<key xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-132952" xlink:title="170 x 96" xlink:type="simple" />
<image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-231377" xlink:title="section-01.jpg" xlink:type="simple" />
</media>
</ContentBlock>');
-First I declare a variable to make the search-string dynamic:
DECLARE #SearchFor VARCHAR(100)='Make a Friend';
--Your XML has a default namespace in the outermost element.
--What makes things a bit weird: There is another default namespace on the element <p>
--We can declare this with a speaking prefix
WITH XMLNAMESPACES(DEFAULT 'http://corp.com/wdpr/ContentBlock'
,'http://www.w3.org/1999/xlink' AS xlink
,'http://www.w3.org/1999/xhtml' AS InnerDflt )
SELECT YourXML.value('(/ContentBlock/section[(content/InnerDflt:p/text())[1]=sql:variable("#SearchFor")]/name/#xlink:href)[1]','nvarchar(max)')
FROM #mockup
The query runs as this:
Start with <ContentBlock>. Look for a <section>, where the text() of <p> below <content> is the search-string. Important: At this stage we are still on the level of <section>. So we can continue the XPath with <name> and find the attribute there.
Shnugo stole my thunder but I'm still posting what I put together as it will work as well and demonstrates a few tricks (e.g. *: for when you're too lazy to add the correct namespace syntax) ;). I was going to mention the use of sql:variable to pass a SQL variable into your XPath expressions - shnugo's post demonstrates how to do that (it's missing in what I'm posting).
-- bonus sample data
DECLARE #xml XML =
'<ContentBlock xmlns="http://corp.com/wdpr/ContentBlock">
<key xlink:href="tcm:0-133036" xlink:title="entityCard" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<template xlink:href="tcm:0-132970" xlink:title="card-header-read-more-all-media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<section>
<name xlink:href="tcm:0-132988" xlink:title="header" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Make a Friend</p>
</content>
</section>
<section>
<name xlink:href="tcm:0-133110" xlink:title="readMore" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals at the only petting zoo in Disney’s Animal
Kingdom park.
</p>
</content>
</section>
<section>
<name xlink:href="tcm:0-132939" xlink:title="readAll" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<content>
<p xmlns="http://www.w3.org/1999/xhtml">Meet and greet friendly animals.</p>
<p xmlns="http://www.w3.org/1999/xhtml">Pet, brush and feed domesticated creatures
</p>
<ul xmlns="http://www.w3.org/1999/xhtml">
<li xmlns="http://www.w3.org/1999/xhtml">Goats</li>
</ul>
<p xmlns="http://www.w3.org/1999/xhtml">Handy animal brushes are available .
</p>
<p xmlns="http://www.w3.org/1999/xhtml">
<strong xmlns="http://www.w3.org/1999/xhtml">Keeping Clean</strong>
<br xmlns="http://www.w3.org/1999/xhtml"/>Guests are encouraged to cleanse.</p>
</content>
</section>
<media>
<name xlink:href="tcm:0-201994" xlink:title="media" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<key xlink:href="tcm:0-132952" xlink:title="170 x 96" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
<image xlink:href="tcm:0-231377" xlink:title="section-01.jpg" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink"/>
</media>
</ContentBlock>';
DECLARE #test TABLE (someId INT IDENTITY, AppConfig XML);
INSERT #test (AppConfig) VALUES (#xml),(CAST(REPLACE(REPLACE(CAST(#xml AS VARCHAR(8000)),
'Make a Friend','xxx'),'Keeping Clean','Make a Friend') AS XML));
-- SOLUTION
WITH XMLNAMESPACES ('http://www.w3.org/1999/xlink' AS xlink)
SELECT t.someId, href = cb.x.value('(../*:name/#xlink:href)[1]', 'varchar(8000)')
FROM #test AS t
CROSS APPLY t.AppConfig.nodes('*:ContentBlock/*:section/*:content') AS cb(x)
WHERE cb.x.exist('*:p[contains(.,"Make a Friend")]') = 1;
Returns:
someId href
----------- -------------
1 tcm:0-132988
2 tcm:0-132939

Replace XML Span Element with Inner text of Span Tag in SQL Server

Replace span Element with its inner text whose class is "TAGGED_ITEM " in multiple rows with a column of type XML
<Item title="1234" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
<ItemBody>
<div class="item_text">
<div>
<span class="TAGGED_ITEM " id="c1_ae1">This is a map on a grid.</span>
<span class="TAGGED_ITEM " id="c1_ae2"> It shows a car.</span>
</div>
<span class="TAGGED_ITEM " id="c1_ae3"> It shows a car on Road.</span>
</div>
</ItemBody>
</Item>
Once Element is updated it should looks as below.
<Item title="1234" xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2">
<ItemBody>
<div class="item_text">
<div>
This is a map on a grid.
It shows a car.
</div>
It shows a car on Road.
</div>
</ItemBody>
</Item>
This question had a particular namespace problem which, probably, had aroused the question. Eliminating namespaces on the match= does solve the problem. So an identity transform and a namespace-neutral matching gives the desired result:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name() = 'span']">
<xsl:value-of select="text()" />
</xsl:template>
</xsl:stylesheet>
Result:
<?xml version="1.0"?>
<Item xmlns="http://www.imsglobal.org/xsd/imsqti_v2p2" title="1234">
<ItemBody>
<div class="item_text">
<div>
This is a map on a grid.
It shows a car.
</div>
</div>
</ItemBody>
</Item>

Transfer XML file with a languages section to a database table using XSLT

below is a part of the xml file I get. I want to transfer this to a single mssql database table. My problem are the different languages of each job-element. The three languages below won't change in time.
I want to create 2 columns for each language in the db-table.
Example: EnglishTitle & EnglishDescription
<jobs>
<job nr="1">
<language name="English">
<title>English title</title>
<description>
<li>English description</li>
<li>English description 2</li>
<li>English description 3</li>
</description>
</language>
<language name="German">
<title>German title</title>
<description>German description</description>
</language>
<language name="Chinese">
<title>Business Mission Chinese</title>
<description>German description</description>
</language>
<general>For all languages</general>
</job>
</jobs>
How can I use XSLT to lookup the language name attribute and create two elements for each language with the content of the name attribute?
UPDATE:
This is what I think the output should look like:
<jobs>
<job nr="1">
<EnglishTitle>English title</EnglishTitle>
<EnglishDescription>
<li>English description</li>
<li>English description 2</li>
<li>English description 3</li>
</EnglishDescription>
<GermanTitle>German title</GermanTitle>
<GermanDescription>German description</GermanDescription>
<ChineseTitle>Business Mission Chinese</ChineseTitle>
<ChineseDescription>Chinese description</ChineseDescription>
<general>For all languages</general>
</job>
</jobs>
Additionally how should I store the <li>-Elements
of a description in the table? Should I replace them with new lines or with a special character?
If there is a better way feel free to tell me.
IMHO, you should have a separate row for each individual description, with columns describing the job number (hopefully unique), the language and the description itself. So something like:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/jobs">
<table>
<xsl:for-each select="job/language/description/text() | job/language/description/li ">
<row>
<col><xsl:value-of select="ancestor::job/#nr"/></col>
<col><xsl:value-of select="ancestor::language/#name"/></col>
<col><xsl:value-of select="."/></col>
</row>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
which in your example would return:
<?xml version="1.0" encoding="utf-8"?>
<table>
<row>
<col>1</col>
<col>English</col>
<col>English description</col>
</row>
<row>
<col>1</col>
<col>English</col>
<col>English description 2</col>
</row>
<row>
<col>1</col>
<col>English</col>
<col>English description 3</col>
</row>
<row>
<col>1</col>
<col>German</col>
<col>German description</col>
</row>
<row>
<col>1</col>
<col>Chinese</col>
<col>German description</col>
</row>
</table>

Resources