How to select the preceding-sibling inside a for each loop? - xslt

My XML
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>
<name>Empire Burlesque1 </name>
</title>
</cd>
<cd>
<title>
<name>Empire Burlesque 2</name>
</title>
</cd>
<cd>
<title>
<name>Empire Burlesque 2</name>
</title>
</cd>
</catalog>
XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="catalog/cd">
<xsl:call-template name="currentValue" />
<xsl:call-template name="prevValue" />
</xsl:for-each>
</xsl:template>
<xsl:template name="currentValue">
<xsl:value-of select="title/name" />
</xsl:template>
<xsl:template name="prevValue">
<xsl:value-of select="preceding-sibling::title[name][1]" />
</xsl:template>
</xsl:stylesheet>
The preceding sibling does not print anything. I want to store both in different variables and compare them. Can you help me pointing what is wrong here?

Currently, when you are checking for the preceding element you are positioned on cd element, and that only has other cd elements as siblings. Therefore, the expression you want is this:
<xsl:value-of select="preceding-sibling::cd[1]/title/name" />

Related

XSLT sum concatenated instead of summed

I have the following input
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<carac NAME="aaa" NOT="10"/>
<value VAL="1"/>
</cd>
<cd>
<carac NAME="aaa" NOT="10"/>
<value VAL="2"/>
</cd>
<cd>
<carac NAME="aaa" NOT="20"/>
<value VAL="3"/>
</cd>
<cd>
<carac NAME="aaa" NOT="10"/>
<value VAL="4"/>
</cd>
<cd>
<carac NAME="bbb" NOT="30"/>
<value VAL="5"/>
</cd>
<cd>
<carac NAME="bbb" NOT="30"/>
<value VAL="6"/>
</cd>
<cd>
<carac NAME="ccc" NOT="40"/>
<value VAL="7"/>
</cd>
<cd>
<carac NAME="ccc" NOT="50"/>
<value VAL="8"/>
</cd>
</catalog>
and I want to get for every different NAME the sum of all the different NOT, so if for the same NAME the NOT is repeated, it has to be summed only once.
The output for this example has to be: aaa30 bbb30 ccc90
My XSL looks like this, but instead of giving the result I want is showing aaa1020 bbb30 ccc4050
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="nameList" match="catalog/cd/carac" use="#NAME"/>
<xsl:key name="notList" match="catalog/cd/carac" use="concat(#NAME,'_',#NOT)"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//carac[generate-id()=generate-id(key('nameList', #NAME)[1])]">
<xsl:variable name="name" select="./#NAME"/>
<xsl:variable name="lines">
<xsl:for-each select="//carac[generate-id()=generate-id(key('notList',concat($name,'_',#NOT))[1])]">
<noti>
<xsl:value-of select="#NOT"/>
</noti>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$name"/>
<xsl:value-of select="sum($lines)"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Try it this way?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="nameList" match="catalog/cd/carac" use="#NAME"/>
<xsl:key name="notList" match="catalog/cd/carac" use="concat(#NAME, '_', #NOT)"/>
<xsl:template match="/catalog">
<html>
<body>
<xsl:for-each select="cd/carac[generate-id()=generate-id(key('nameList', #NAME)[1])]">
<xsl:value-of select="#NAME"/>
<xsl:variable name="current-group" select="key('nameList', #NAME)" />
<xsl:value-of select="sum($current-group[generate-id()=generate-id(key('notList', concat(#NAME, '_', #NOT))[1])]/#NOT)"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

<apply-templates/> vs <apply-templates select="..."/> in XSLT

Look at the XSLT-code under the address http://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_apply ... Below you find the first part of this code (and the one decisive for my question):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
If you now change only the line
<xsl:apply-templates/>
to
<xsl:apply-templates select="cd"/>
the transformation does not work anymore ... (The code now looks as follows:)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates select="cd"/> <!--ONLY LINE OF CODE THAT WAS CHANGED-->
</body>
</html>
</xsl:template>
My question is: Why does the change break the code? In my opinion, the logic is the same in both cases:
apply the template matching "cd"
inside template "cd" apply the other two templates ("title" + "artist")
UPDATE:
The whole xslt code is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
Here's an excerpt from the xml:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
......
</catalog>
What W3C schools doesn't tell you is about XSLT's Built-in Template Rules.
When you do <xsl:apply-templates select="cd"/> you are positioned on the document node, which is the parent of the catalog element. Doing select="cd" will select nothing, because cd is a child of the catalog element, and not a child of the document node itself. Only catalog is a child.
(Note that catalog is the "root element" of the XML. An XML document can have only one root element).
However, when you do <xsl:apply-templates />, then this is equivalent to <xsl:apply-templates select="node()" /> which will select the catalog element. This is where the built-in templates kick in. You don't have a template matching catalog in your XSLT, and so the built-in one is used.
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
(Here * matches any element). Thus, this built-in template will select the child nodes of catalog, and so match the other templates in your XSLT.
Note that, in your second example, you can change the template match to this...
<xsl:template match="/*">
This will match the catalog element, and so then <xsl:apply-templates select="cd" /> will work.

Using XPATHs from file and adding relevant attribute

May be my XSL approach is wrong? please correct me the way to handle this situation
I want to grab XPATHs and Attrs from a mapping file, then use XPATH to match, and apply attributes to XML.
Here is my 3 inputs files:
mappings.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<map xpath="//title" class="title" others="moreToCome" />
<map xpath="//subtitle" class="subtitle" others="moreToCome" />
<map xpath="//p" class="p" others="moreToCome" />
</mappings>
Source.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title>title text</title>
<subtitle>subtitle text</subtitle>
<p>subtitle text</p>
</root>
StyleMapping.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="fMappings" select="document('mappings.xml')" />
<xsl:variable name="xpath"><xsl:text>justToMakeItGlobal</xsl:text></xsl:variable>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
<!-- loop thru map in mappings.xml -->
<xsl:for-each select="$fMappings//mappings/map">
<xsl:call-template name="dyn">
<xsl:with-param name="xpath" select="#xpath" />
<xsl:with-param name="class" select="#class" />
<xsl:with-param name="others" select="#others" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template match="$xpath" mode="dyn">
<xsl:param name="xpath"/>
<xsl:param name="class"/>
<xsl:param name="others"/>
<xsl:attribute name="class"><xsl:value-of select="$class" /></xsl:attribute>
<xsl:attribute name="others"><xsl:value-of select="$others" /></xsl:attribute>
</xsl:template>
</xsl:stylesheet>
This is what is planning to do, but i'm not getting the correct way of doing it in XSLT:
1. Read mappings.xml file
2. Loop thru each map tag
3. grab xpath and attr's
4. apply template match/select with above xpath
5. add attr's to above selected nodes
output.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title class="title" others="moreToCome">title text</title>
<subtitle class="subtitle" others="moreToCome">subtitle text</subtitle>
<p class="p" others="moreToCome">subtitle text</p>
</root>
I don't think you can have a template with a calculated match pattern. Let me suggest a different approach:
mappings.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<map elem="title" class="Title" others="moreToCome1" />
<map elem="subtitle" class="Subtitle" others="moreToCome2" />
<map elem="p" class="P" others="moreToCome3" />
</mappings>
stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="mappings" select="document('mappings.xml')/mappings" />
<xsl:template match="*">
<xsl:variable name="elem" select="name()" />
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:if test="$elem = $mappings/map/#elem">
<xsl:attribute name="class">
<xsl:value-of select="$mappings/map[#elem=$elem]/#class"/>
</xsl:attribute>
<xsl:attribute name="others">
<xsl:value-of select="$mappings/map[#elem=$elem]/#others"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Testing with the following source XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<title original="yes">title text</title>
<subtitle>subtitle text</subtitle>
<p>para text</p>
<nomatch attr="test">another text</nomatch>
</root>
results in:
<?xml version="1.0" encoding="utf-8"?>
<root>
<title original="yes" class="Title" others="moreToCome1">title text</title>
<subtitle class="Subtitle" others="moreToCome2">subtitle text</subtitle>
<p class="P" others="moreToCome3">para text</p>
<nomatch attr="test">another text</nomatch>
</root>

XSLT CallTemplate ForEach XML

I need a little XSLT help. Couldn't figure out why the actual output is different from my expected output. Any help much appreciated!
XML
<?xml version="1.0"?>
<a>
<b c="d"/>
<b c="d"/>
<b c="d"/>
</a>
XSL
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="foo">
<xsl:param name="content"></xsl:param>
<xsl:value-of select="$content"></xsl:value-of>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="foo">
<xsl:with-param name="content">
<xsl:for-each select="a/b">
<e>
<xsl:value-of select="#c" />
</e>
</xsl:for-each>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
Actual Output
<?xml version="1.0"?>
ddd
Desired Output
<?xml version="1.0"?>
<e>d</e>
<e>d</e>
<e>d</e>
Note: Calling the template is mandatory. In my situation the template does more with extension functions.
Contrary to what ABach says, your xsl:param is fine. The only thing you need to change is your xsl:value-of. It should be a xsl:copy-of:
<xsl:template name="foo">
<xsl:param name="content"/>
<xsl:copy-of select="$content"/>
</xsl:template>
You're very close; you've just mixed up relative positioning and correct parameter usage within templates. Here's a slightly revised answer.
When this XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template name="foo">
<xsl:param name="pContent" />
<xsl:for-each select="$pContent">
<e>
<xsl:value-of select="#c" />
</e>
</xsl:for-each>
</xsl:template>
<xsl:template match="/*">
<xsl:call-template name="foo">
<xsl:with-param name="pContent" select="*" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
...is applied to the original XML:
<?xml version="1.0"?>
<a>
<b c="d" />
<b c="d" />
<b c="d" />
</a>
...the desired result is produced:
<?xml version="1.0"?>
<e>d</e>
<e>d</e>
<e>d</e>
In particular, notice the correct usage of <xsl:param> to include nodes based on their relative position. In your case, you are telling the XSLT parser to output the text values of the parameter that you're passing, rather than altering the nodes' contents in the way you want.

Excluding Namespaces in XML results

I am trying to figure out how best I can process the XML file below so the resulting XML file excludes namespace declarations.
XML Input
<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/">
<b:title>Book Title</b:title>
<p:number>page001</p:number>
<p:number>page002</p:number>
<p:number>page001</p:number>
<p:number>page002</p:number>
</page>
Current XSL File
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b="http://book.com/"
xmlns:p="http://page.com/"
>
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="resource">
<xsl:apply-templates select="b:title" />
<xsl:apply-templates select="p:number" />
</xsl:template>
<xsl:template match="b:title">
<title exclude-result-prefixes="#all">
<xsl:value-of select="." />
</title>
</xsl:template>
<xsl:template match="p:number">
<page exclude-result-prefixes="#all">
<xsl:value-of select="." />
</page>
</xsl:template>
</xsl:stylesheet>
Current Output
<title xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">Book Title</title>
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page001</page>
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page002</page>
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page001</page>
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page002</page>
Desired Output
<?xml version="1.0" encoding="UTF-8"?>
<title>Book Title</title>
<page>page001</page>
<page>page002</page>
<page>page001</page>
<page>page002</page>
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="*[local-name()='number']">
<page>
<xsl:value-of select="."/>
</page>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/">
<b:title>Book Title</b:title>
<p:number>page001</p:number>
<p:number>page002</p:number>
<p:number>page001</p:number>
<p:number>page002</p:number>
</page>
produces the wanted, correct result:
<page>
<title>Book Title</title>
<page>page001</page>
<page>page002</page>
<page>page001</page>
<page>page002</page>
</page>
Explanation:
Use of the xsl:element instruction to create (not copy!) a new element with name that is the local-name() of the matched element.
A template matching elements with local-name() number to "rename" them to page
Use the exclude-result-prefixes attribute on the xsl:stylesheet element.
In your case, something like:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b="http://book.com/"
xmlns:p="http://page.com/"
exclude-result-prefixes="b p"
>
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="resource">
<xsl:apply-templates select="b:title" />
<xsl:apply-templates select="p:number" />
</xsl:template>
<xsl:template match="b:title">
<title>
<xsl:value-of select="." />
</title>
</xsl:template>
<xsl:template match="p:number">
<page>
<xsl:value-of select="." />
</page>
</xsl:template>
</xsl:stylesheet>