I am new to XML and XSLT. What I am trying to do is generate a CSV file by applying an XSLT.
This is my XML file:
<people>
<person>
<codes>
<code>53-8907</code>
</codes>
<first-name>Matt</first-name>
<licenses>
<license>
<state>TX</state>
</license>
</licenses>
</person>
<person>
<codes>
<code>66-8907</code>
</codes>
<first-name>Mike</first-name>
<licenses>
<license>
<state>NY</state>
</license>
</licenses>
</person>
<person>
<codes>
<code>53-8907</code>
<code>66-8907</code>
</codes>
<first-name>Rob</first-name>
<licenses>
<license>
<state>TX</state>
</license>
<license>
<state>NY</state>
</license>
</licenses>
</person>
</people>
This is the XSL I am using:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:template match="/people">
<xsl:for-each select="person/licenses/license">
<xsl:value-of select="ancestor::person/codes/code"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="ancestor::person/first-name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="state"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This is what Output I want:
53-8907,Matt,TX
66-8907,Mike,NY
53-8907,Rob,TX
66-8907,Rob,NY
This is what I am getting:
53-8907,Matt,TX
66-8907,Mike,NY
53-8907,Rob,TX
53-8907,Rob,NY
Can someone guide me how to count the for-each value for license?
This code renders the requested result:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:template match="/people">
<xsl:for-each select="person/licenses">
<xsl:for-each select="license">
<xsl:variable name="Pos" select="position()"/>
<xsl:value-of select="ancestor::person/codes/code[position() = $Pos]"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="ancestor::person/first-name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="state"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
An alternative solution, which stores things in variables and uses concat()
.
Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="person">
<xsl:variable name="first-name" select="first-name"/>
<xsl:variable name="licenses" select="licenses/license"/>
<xsl:for-each select="codes/code">
<xsl:variable name="pos" select="position()"/>
<xsl:value-of select="concat(.,',',$first-name,',',$licenses[$pos],' ')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Text Output
53-8907,Matt,TX
66-8907,Mike,NY
53-8907,Rob,TX
66-8907,Rob,NY
Try it online here: http://xsltransform.net/94hvTze/3 .
This should fix your issue :
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:template match="/people">
<xsl:for-each select="person/licenses">
<xsl:for-each select="license">
<xsl:variable name="licensePos" select="position()"/>
<xsl:value-of select="ancestor::person/codes/code[position()=$licensePos]"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="ancestor::person/first-name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="state"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
What I did here is keep the position
of the license in a variable, than when checking for the parent code, make sure it have the same position.
See it in action : http://xsltransform.net/94hvTze/1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.