Replace XML Span Element with Inner text of Span Tag in SQL Server - 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>

Related

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

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>

List from repeated items in XSLT

I'm new to XSLT and I'm looking for help in my problem. Here is my code in XML:
<?xml version="1.0" encoding="UTF-8"?>
<nadrazi jmeno="Podlešín">
<vlak typ="os" cislo="5112" z="Hostivice" cil="Podlešín" naVlak="53001">
<prijezd>16:20:00</prijezd>
<slozeni>
<vuz cislo="1" oznaceniC="810" />
<vuz cislo="2" oznaceniC="010" oznaceniT="Btax" />
</slozeni>
</vlak>
<vlak typ="os" cislo="53001" z="Podlešín" smer="Zloněves" cil="Kralupy nad Vltavou" zVlaku="5112">
<odjezd>17:00:00</odjezd>
<slozeni>
<vuz cislo="1" oznaceniC="810" />
<vuz cislo="2" oznaceniC="010" oznaceniT="Btax" />
</slozeni>
</vlak>
<vlak typ="sp" cislo="1389" z="Louny" smer="Zloněves" cil="Praha Holešovice">
<prijezd>16:18:00</prijezd>
<odjezd>16:23:00</odjezd>
<slozeni>
<vuz cislo="1" oznaceniC="843" />
<vuz cislo="2" oznaceniT="Btn" />
<vuz cislo="3" oznaceniT="Btn" />
</slozeni>
</vlak>
<vlak typ="r" cislo="572" z="Praha Holešovice" smer="Slaný předměstí" cil="Chomutov">
Nechranice
<prijezd>14:27:00</prijezd>
<odjezd>14:28:00</odjezd>
<slozeni>
<vuz cislo="0" oznaceniC="754" />
<vuz cislo="250" oznaceniT="B" />
<vuz cislo="251" oznaceniT="B" />
<vuz cislo="252" oznaceniT="Bp" />
<vuz cislo="253" oznaceniT="Bp" />
<vuz cislo="254" oznaceniT="BDs" />
<vuz cislo="255" oznaceniT="A" />
</slozeni>
</vlak>
</nadrazi>
I want get a vlaue from all elements vlak attribute smer make an array (or a list or something similar) from this values and then use something like unique() function to have only one item of every repeated value. In this case in my array will be "Zloněves" and "Slaný předměstí".
Here is my XSLT code. I tried some ways to create some arrays but I failed and erased that lines (some time ago).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Rozpis vlaků pro stanici <xsl:value-of select="nadrazi/#jmeno" /></title>
</head>
<script type="text/javascript">
function show(element) {
var srcElement = document.getElementById(element);
if(srcElement != null) {
if(srcElement.style.display == "block") {
srcElement.style.display= 'none';
} else {
srcElement.style.display='block';
}
return false;
}
}
</script>
<body>
<h1>Stanice <xsl:value-of select="nadrazi/#jmeno" /></h1>
<xsl:apply-templates select="nadrazi" mode="smer" />
</body>
</html>
</xsl:template>
<xsl:template match="vlak" mode="smer">
<div>
<xsl:attribute name="id"><xsl:value-of select="#smer" /></xsl:attribute>
</div>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
Output will be HTML file, where items of the array will be div's id (div id="arrayItem"). Now the output is (that times and spaces aren't important now but some help I would welcome (link etc.)):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rozpis vlaků pro stanici Podlešín</title>
</head>
<script type="text/javascript">
function show(element) {
var srcElement = document.getElementById(element);
if(srcElement != null) {
if(srcElement.style.display == "block") {
srcElement.style.display= 'none';
} else {
srcElement.style.display='block';
}
return false;
}
}
</script><body>
<h1>Stanice Podlešín</h1>
<div id=""></div>
16:20:00
<div id="Zloněves"></div>
17:00:00
<div id="Zloněves"></div>
16:18:00
16:23:00
<div id="Slaný předměstí"></div>
Nechranice
14:27:00
14:28:00
</body>
</html>
and I want this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rozpis vlaků pro stanici Podlešín</title>
</head>
<script type="text/javascript">
function show(element) {
var srcElement = document.getElementById(element);
if(srcElement != null) {
if(srcElement.style.display == "block") {
srcElement.style.display= 'none';
} else {
srcElement.style.display='block';
}
return false;
}
}
</script><body>
<h1>Stanice Podlešín</h1>
<div id="Zloněves"></div>
<div id="Slaný předměstí"></div>
</body>
</html>
Into those divs I will add more data and that ids I need for hiding divs with Javascript function show(id). And all elements which have smer="Zloněves" will be in one <div id="Zloněves"> and all elements which have smer="Slaný předměstí" will be in another one <div id="Slaný předměstí">
I have found some questions how to make an array but nothning solving this problem. Thanks for help.
This is a grouping problem, and in XSLT 1.0 you need to use a technique called Muenchain grouping.
You are grouping vlak elements by their smer attribute, so you define a key like so:
<xsl:key name="vlak" match="vlak[#smer]" use="#smer" />
So, for a given value, the key will return all vlak elements whose #smer attribute equals that value.
Now, to get the distinct elements, you need to match the vlak elements who occur first in the key for their given #smer value. This is done like so:
<xsl:template match="vlak[generate-id() = generate-id(key('vlak', #smer)[1])]" mode="smer">
And then within this template, should you so wish, to get all the vlak elements with this group, just access the key
<xsl:for-each select="key('vlak', #smer)">
The only other thing you would need is a template to match the other vlak elements to stop them being output outside this loop
<xsl:template match="vlak" mode="smer" />
Here is the full XSLT (I've removed the javascript for brevity)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:key name="vlak" match="vlak[#smer]" use="#smer" />
<xsl:template match="/">
<html>
<head>
<title>Rozpis vlaků pro stanici <xsl:value-of select="nadrazi/#jmeno" /></title>
</head>
<body>
<h1>Stanice <xsl:value-of select="nadrazi/#jmeno" /></h1>
<xsl:apply-templates select="nadrazi" mode="smer" />
</body>
</html>
</xsl:template>
<xsl:template match="vlak[generate-id() = generate-id(key('vlak', #smer)[1])]" mode="smer">
<div>
<xsl:attribute name="id"><xsl:value-of select="#smer" /></xsl:attribute>
<xsl:for-each select="key('vlak', #smer)">
<xsl:value-of select="odjezd" />
</xsl:for-each>
</div>
</xsl:template>
<xsl:template match="vlak" mode="smer" />
</xsl:stylesheet>
When applied to your XML, the following is output
<html>
<head>
<META http-equiv="Content-Type" content="text/html">
<title>Rozpis vlaků pro stanici Podlešín</title>
</head>
<body>
<h1>Stanice Podlešín</h1>
<div id="Zloněves">17:00:00 16:23:00</div>
<div id="Slaný předměstí">14:28:00</div>
</body>
</html>
HiHere i'm not giving the full code just giving the sample of fetching values
<xsl:for-each select="nadrazi/vlak">
<xsl:if test="#smer='Zloneves'">
<div id =<xsl:value-of select="#smer"/>>
<xsl:value-of select="prijezd"/>
<xsl:value-of select="odjezd"/>
</div>
</xsl:if>
</xsl:for-each>
In the above example i've used the <xsl:if> element <xsl:choose>
The sample ouput for the above code will be
<div id=Zloněves></div>
17:00:00
<div id=Zloněves></div>
16:18:00
16:23:00

How to make NewsList from News package show news as list

I'am calling the NewsList (from the standard Composite.News package) from a template like this:
<f:function name="Composite.News.NewsList" xmlns:f="http://www.composite.net/ns/function/1.0">
<f:param name="ListOptions" value="Show date,Show teaser" />
<f:param name="PageSize" value="10" />
<f:param name="DateFormat" value="dddd, MMMM dd, yyyy" />
</f:function>
I can't make it to show the news as a list. It only shows the first newsitem in the detailsmode.
What does the below line in the NewsList function do?
How can I make the control show my news in a list and not the first (most recent) item?
I have talked to a more experienced Composite Developer and he couldn't either make the Composite.News.Newslist show news in a list.
UPDATE:
I can se that the OMNICORP site doesn't use the standard News package. What is the reason for that?
Omnicorp Demo Site has its own News module based on global data type "Omnicorp.Content.News" and XSLT function "Omnicorp.Content.News"
Composite.News package is different and based on page data folder named "Composite.News.NewsItem" and two XSLT functions: "Composite.News.NewsList" and "Composite.News.NewsTeasers". So to use this package you should follow the steps described here - http://docs.composite.net/Packages/CompositeNews: create a page with page type = "News page". Add news item to the News page data folder (under the page). On the Content tab, have the Composite.News.NewsList function, which will render News List with links to the details.
But can I make the NewsTeasers list all the news and set up paging as well?
Yes you can. You can modify the existent NewsTeaser function or create the new function. On the Function Call tab modify the "Composite.News.NewsItem.GetNewsItemXml" function call and specify next parameters "PageNumber" and "IncludePagingInfo" with next values:
<f:param name="PageNumber">
<f:function name="Composite.Web.Request.QueryStringIntegerValue">
<f:param name="ParameterName" value="Page" />
<f:param name="FallbackValue" value="1" />
</f:function>
</f:param>
<f:param name="IncludePagingInfo" value="True" />
On the function Template tab add template to display PagingInfo:
<xsl:param name="pagingInfo" select="/in:inputs/in:result[#name='GetNewsItemXml']/PagingInfo" />
...
<xsl:if test="$pagingInfo/#TotalPageCount > 1">
<div class="Paging">
<xsl:apply-templates select="$pagingInfo" />
</div>
</xsl:if>
...
<xsl:template match="PagingInfo">
<xsl:param name="page" select="1" />
<xsl:if test="$page < #TotalPageCount + 1">
<xsl:if test="$page = #CurrentPageNumber">
<span><xsl:value-of select="$page" /></span>
</xsl:if>
<xsl:if test="not($page = #CurrentPageNumber)">
<xsl:value-of select="$page" />
</xsl:if>
<xsl:apply-templates select=".">
<xsl:with-param name="page" select="$page+1" />
</xsl:apply-templates>
</xsl:if>
</xsl:template>
For more information how to add Paging to the XSLT read this article- http://docs.composite.net/XSLT/SortingAndPaging

SQL XML Insert multiple elements

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>
')

Resources