I have XML output representing a table of data being output like this:
<results>
<Row1.Name>Henry</Row1.Name>
<Row1.Id>P162</Row1.Id>
<Row1.Age>23</Row1.Age>
<Row2.Name>John</Row2.Name>
<Row2.Id>P137</Row2.Id>
<Row2.Age>27</Row2.Age>
<Row3.Name>Mary</Row3.Name>
<Row3.Id>L493</Row3.Id>
<Row3.Age>32</Row3.Age>
</results>
and I want to convert it to this:
<results>
<Row>
<Name>Henry</Name>
<Id>P162<Id>
<Age>23</Age>
</Row>
<Row>
<Name>John</Name>
<Id>P137<Id>
<Age>27</Age>
</Row>
<Row>
<Name>Mary</Name>
<Id>L493<Id>
<Age>32</Age>
</Row>
</results>
The application I am working with forces me to use XSLT 1.0, and I'm sure this is really simple, but I'm having a mental block today so I figured I'd ask my virtual colleagues.
Anybody got any ideas?
NOTE: Amended the desired output to not show iterative row numbers, which is what I want.
Haven't got anything close to working yet. Still playing around with different things.
Thought I could write something like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="Row" select="distinct-values(*[contains(name(),'Row')])" />
<xsl:for-each select="$Row">
<xsl:variable name="rowName" select="name()" />
<Row>
<xsl:for-each select="*[contains(name(),$Row)]">
<xsl:copy select="." />
</xsl:for-each>
</Row>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
to produce this:
<results>
<Row>
<Row1.Name>Henry</Row1.Name>
<Row1.Id>P162</Row1.Id>
<Row1.Age>23</Row1.Age>
</Row>
<Row>
<Row2.Name>John</Row2.Name>
<Row2.Id>P137</Row2.Id>
<Row2.Age>27</Row2.Age>
</Row>
<Row>
<Row3.Name>Mary</Row3.Name>
<Row3.Id>L493</Row3.Id>
<Row3.Age>32</Row3.Age>
</Row>
</results>
and then go back and remove all the Row# prefixes with:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*[contains(name(),'.')]">
<xsl:variable name="Name" select="substring-after(name(),'.')" />
<xsl:element name="{$Name}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Neither transform works though and I don't think I can use distinct-values
with XSLT 1.0