the xml is
<XYZ>
<manager>
<mId>m1</mId>
<mName>mName1</mName>
<manager>
<manager>
<mId>m2</mId>
<mName>mName2</mName>
<manager>
<department>
<dName>d1</dName>
<dManager>m1</dManager>
<department>
<department>
<dName>d2</dName>
<dManager>m1</dManager>
<department>
<department>
<dName>d3</dName>
<dManager>m2</dManager>
<department>
</XYZ>
for each manager, output all the department name he manages, my code is like
<xsl:for-each select="XYZ/manager">
<xsl:variable name='mId'>
<xsl:value-of select="mId"/>
</xsl:variable>
<p>
manager <xsl:value-of select="mName"/> manages department
<xsl:for-each select="XYZ/department[dManager=$mId]">
<xsl:value-of select="XYZ/department/dName"/>,
</xsl:for-each>
</p>
</xsl:for-each>
and it outputs nothing after the manages department
, anyone know what's wrong? thank you!
The <xsl:for-each> element loops through each node in a specified node set. <!-- Content --> Required. An XPath expression that specifies which node set to be processed.
The <xsl:for-each> element allows you to do looping in XSLT. The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set: Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.
The xsl:for-each element can be used as an alternative to xsl:apply-templates where the child nodes of the current node are known in advance. It may have one or more xsl:sort child elements to define the order of sorting.
XSLT for each is defined as the iteration process that allows retrieving data from the specified nodes. Based on the selection criteria defined with these functions, it makes a loop through multiple nodes. This works in adding up with the function <xsl:value-of>. It repeats the block of content over each element in the given node-set.
You had a context problem inside your for-each
: the for-each
instruction changes the context, then when you apply your second for-each
and/or when you call for the value of department/dName
, you're not in the right context.
Then just fix your two last select as below :
<xsl:for-each select="XYZ/manager">
<xsl:variable name='mId'>
<xsl:value-of select="mId"/>
</xsl:variable>
<p>
manager <xsl:value-of select="mName"/> manages department
<xsl:for-each select="/XYZ/department[dManager=$mId]">
<xsl:value-of select="dName"/>,
</xsl:for-each>
</p>
</xsl:for-each>
This might help you. Have corrected your XPaths with little modification in output format:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:for-each select="/XYZ/manager">
<xsl:variable name='mId'>
<xsl:value-of select="mId"/>
</xsl:variable>manager <xsl:value-of select="mName"/> manages department <xsl:for-each select="/XYZ/department[dManager=$mId]">
<xsl:value-of select="dName"/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>
<xsl:if test="position() != last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With