Reputation: 29
I have this xml, and a problem with the xslt transform:
<start>
<row>
<xxx Caption="School1"></xxx>
<yyy Caption="Subject1"></yyy>
<zzz></zzz>
</row>
<row>
<xxx Caption="School2"></xxx>
<yyy Caption="Subject2"></yyy>
<zzz></zzz>
</row>
</start>
The Xsl-transform is like this:
<xsl:stylesheet>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<schools>
<xsl:apply-templates select="//row/*" />
</schools>
</xsl:template>
<xsl:template match="//row/*">
<school>
<xsl:if test="name()='xxx'">
<name>
<xsl:value-of select="@Caption"/>
</name>
</xsl:if>
<xsl:if test="name()='yyy'">
<subject>
<xsl:value-of select="@Caption"/>
</subject>
</xsl:if>
</school>
</xsl:template>
</xsl:stylesheet>
The xsl transform gives this result, which is not exactly what i want it to be:
<schools>
<school>
<name>School1</name>
</school>
<school>
<subject>Subject1</subject>
</school>
<school />
<school>
<name>School2</name>
</school>
<school>
<subject>Subject2</subject>
</school>
<school />
</schools>
I want the result to be like this:
<schools>
<school>
<name>School1</name>
<subject>Subject1</subject>
</school>
<school>
<name>School2</name>
<subject>Subject2</subject>
</school>
</schools>
The name and subject elements should be inside the same school element.
Please help me for a better solution.
Upvotes: 0
Views: 53
Reputation: 116959
Why don't you do simply:
XSLT 1.0
<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:strip-space elements="*"/>
<xsl:template match="/start">
<schools>
<xsl:apply-templates/>
</schools>
</xsl:template>
<xsl:template match="row">
<school>
<xsl:apply-templates select="xxx | yyy"/>
</school>
</xsl:template>
<xsl:template match="xxx">
<name>
<xsl:value-of select="@Caption"/>
</name>
</xsl:template>
<xsl:template match="yyy">
<subject>
<xsl:value-of select="@Caption"/>
</subject>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1