Reputation: 21
i have for example this source XML:
<People>
<Person Name="NameOne"/>
<Person Name="NameTwo"/>
</People>
I have this XSL:
<?xml version="1.0" encoding="Windows-1250"?>
<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:template match="/">
<xsl:for-each select="People">
<Names>
<Human><xsl:value-of select="Person/@Name"/></Human>
</Names>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have this XML output:
<Names>
<Human>NameOne</Human>
</Names>
But i need this output with all records:
<Names>
<Human>NameOne</Human>
<Human>NameTwo</Human>
</Names>
Have you got any ideas please?
Upvotes: 0
Views: 74
Reputation: 1299
Please use the below code if you wanna use for-each
<xsl:template match="/">
<Names>
<xsl:for-each select="People/Person">
<Human><xsl:value-of select="@Name"/></Human>
</xsl:for-each>
</Names>
</xsl:template>
Upvotes: 0
Reputation: 70618
The issue is you are iterating over People elements
<xsl:for-each select="People">
But you only have one People element in your XML. You need to "match" the People element, then iterate over the Person elements
Try this XSLT
<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:template match="People">
<Names>
<xsl:for-each select="Person">
<Human><xsl:value-of select="@Name"/></Human>
</xsl:for-each>
</Names>
</xsl:template>
</xsl:stylesheet>
Or better still, use a fully template approach without xsl:for-each
<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:template match="People">
<Names>
<xsl:apply-templates select="Person" />
</Names>
</xsl:template>
<xsl:template match="Person">
<Human><xsl:value-of select="@Name"/></Human>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2