Reputation: 35
I've got xml file:
<root>
<host>
<address addr="192.168.0.1" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="10"></port>
<port protocol="tcp" portid="20"></port>
</ports>
</host>
<host>
<address addr="192.168.0.2" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="30"></port>
<port protocol="tcp" portid="40"></port>
</ports>
</host>
<root>
I want get results like this:
192.168.0.1 10,20
192.168.0.2 30,40
I’m using for-each (for first column):
<xsl:for-each select="root/host">
<xsl:value-of select="address/@addr"/></fo:block>
</xsl:for-each>
But second column is a problem.. I get wrong results:
192.168.0.1 10,20,30,40
192.168.0.2 10,20,30,40
Please help :)
Upvotes: 1
Views: 109
Reputation: 763
<xsl:for-each select="root/host">
<xsl:value-of select="address/@addr"/>
<xsl:for-each select="ports/port">
<xsl:value-of select="@portid"/>
</xsl:for-each>
</xsl:for-each>
you can try above on some sample xslt editor like http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex3
Upvotes: 1
Reputation: 5402
You need to iterate again through all the child 'port' elements. The following should work:
<xsl:for-each select="root/host">
<xsl:value-of select="address/@addr" />
<xsl:text> </xsl:text>
<xsl:for-each select="ports/port">
<xsl:value-of select="@portid" />
<xsl:if test="following-sibling::*">,</xsl:if>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
Upvotes: 1