Reputation: 45
XML File:
<generic-cv:generic-cv xmlns:generic-cv="http://www.cihr-irsc.gc.ca/generic-cv/1.0.0" lang="en" dateTimeGenerated="2014-05-30 11:40:50">
<section id="f589cbc028c64fdaa783da01647e5e3c" label="Personal Information">
<section id="2687e70e5d45487c93a8a02626543f64" label="Identification" recordId="4f7c2ebd789f407b939e05664f6aa7c0">
<field id="ee8beaea41f049d8bcfadfbfa89ac09e" label="Title">
<lov id="00000000000000000000000000000318">Mr.</lov>
</field>
<field id="98ad36fee26a4d6b8953ea764f4fed04" label="First Name">
<value type="String">Hara</value>
</field>
</section>
<section id="2687e70e5d45487c93a8a02626543f64" label="Identification" recordId="4f7c2ebd789f407b939e05664f6aa7c0">
<field id="ee8beaea41f049d8bcfadfbfa89ac09e" label="Title">
<lov id="00000000000000000000000000000318">Mr.</lov>
</field>
<field id="98ad36fee26a4d6b8953ea764f4fed04" label="First Name">
<value type="String">ali</value>
</field>
</section>
</section>
and an xslt file like this
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:variable name="FirstName" select=".//field[@id='98ad36fee26a4d6b8953ea764f4fed04']/value"/>
<xsl:template match="/">
<html>
<head>
<title>Xml Convertor</title>
</head>
<body>
<h2><b> Personal Information</b></h2>
<ul>
<xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" >
<li>Name: $FirstName></li>
</xsl:for-each>
</ul>
</body>
</html>
Instead of using specific code values to get to the right information in the xml file. I wanted to use (global) variables so that i can change the values easily. For instance, when i want to find and display (in html) the first name, i look for code "98ad36fee26a4d6b8953ea764f4fed04". Instead i want to set a variable, for instance firstName to this value and look for the variable value in the xml file.However when i set first name to variable and put it under a for each loop it will only print out the name Hara and not ali. Is there a way i can fix this problem without putting the variable declaration in the for each loop
Upvotes: 0
Views: 3776
Reputation: 122414
The select expression of an xsl:variable
is evaluated in whatever context the variable declaration is found. In your example the FirstName
variable will contain the set of value
elements from inside all the first name fields in the whole document (since the context for the variable declaration is the root node /
) and the value-of
that node set is defined to be the string value of the first node in the set in document order, namely "Hara".
It looks to me like you're trying to declare a kind of function rather than a variable - something that will give you the value of the first name field for whatever context you call it from. In XSLT 1.0 that means a named template
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template name="firstName">
<xsl:value-of select=".//field[@id='98ad36fee26a4d6b8953ea764f4fed04']/value" />
</xsl:template>
<xsl:template match="/">
<html>
<head>
<title>Xml Convertor</title>
</head>
<body>
<h2><b> Personal Information</b></h2>
<ul>
<xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" >
<li>Name: <xsl:call-template name="firstName"/></li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
When you call a named template the context does not change, so the .//
in the firstName
template will be relative to the node for the current iteration of the for-each
Upvotes: 3