I've been provided an SML file where the designer (not in my company, so I have no control over this) has created some data that I need to consume; but they setup enumerated tags, so I'm having a difficult time creating a loop to read the data.
Their code looks like
<Root>
<Subjects>
<...more XML Data>
<Data>
<...other XML Data>
<Demographic_Information>
<Age1>33</Age1>
<Age2>66</Age2>
<Age3 />
<Age4 />
<Age5 />
<Age6 />
<Age7 />
<Age8 />
<Age9 />
<Age10 />
<Gender1>M</Gender1>
<Gender2>F</Gender2>
<Gender3 />
<Gender4 />
<Gender5 />
<Gender6 />
<Gender7 />
<Gender8 />
<Gender9 />
<Gender10 />
<Race1>W</Race1>
<Race2>H</Race2>
<Race3 />
<Race4 />
<Race5 />
<Race6 />
<Race7 />
<Race8 />
<Race9 />
<Race10 />
</Demographic_Information>
</...other XML Data>
</Data>
</...more XML Data>
</Subjects>
</Root>
I just need to loop through this, and ensure that Age1, Gender1, and Race1 go into my data like
<Person subject="1">
<Age>33</Age>
<Gender>M</Gender>
<Race>W</Race>
</Person>
<Person subject="2">
<Age>66</Age>
<Gender>F</Gender>
<Race>A</Race>
</Person>
This is a subset of data inside a larger set, but I need to get it into this format if possible. I'm sure it can be done, I just don't know how to go about it.
My XSLT is version 1.0 in Microsoft Visual Studio 2008. I start with
<xsl:template match="/Root/Subjects">
***Modified to provide a better sample of my issue.
Here's a quick stab that works - I'm going to continue to look at this to find efficiencies, but I wanted to get you an answer.
EDIT: thanks to @MartinHonnen for a nice simplification.
When this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable
name="vNums"
select="'0123456789'"/>
<xsl:key
name="kElemByNumber"
match="Demographic_Information/*"
use="translate(name(), translate(name(), $vNums, ''), '')"/>
<xsl:template match="/*">
<Demographic_Information>
<xsl:apply-templates
select="*[generate-id() =
generate-id(key(
'kElemByNumber',
translate(name(), translate(name(), $vNums, ''),
''
))[1])][normalize-space()]">
<xsl:sort
select="translate(name(), translate(name(), $vNums, ''), '')"
data-type="number"/>
</xsl:apply-templates>
</Demographic_Information>
</xsl:template>
<xsl:template match="*">
<Person subject="{position()}">
<xsl:apply-templates
select="key('kElemByNumber', position())"
mode="children">
<xsl:sort select="name()"/>
</xsl:apply-templates>
</Person>
</xsl:template>
<xsl:template match="*" mode="children">
<xsl:element name="{translate(name(), $vNums, '')}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
...is applied to the provided source XML:
<Demographic_Information>
<Age1>33</Age1>
<Age2>66</Age2>
<Age3/>
<Age4/>
<Age5/>
<Age6/>
<Age7/>
<Age8/>
<Age9/>
<Age10/>
<Gender1>M</Gender1>
<Gender2>F</Gender2>
<Gender3/>
<Gender4/>
<Gender5/>
<Gender6/>
<Gender7/>
<Gender8/>
<Gender9/>
<Gender10/>
<Race1>W</Race1>
<Race2>H</Race2>
<Race3/>
<Race4/>
<Race5/>
<Race6/>
<Race7/>
<Race8/>
<Race9/>
<Race10/>
</Demographic_Information>
...the wanted result is produced:
<Demographic_Information>
<Person subject="1">
<Age>33</Age>
<Gender>M</Gender>
<Race>W</Race>
</Person>
<Person subject="2">
<Age>66</Age>
<Gender>F</Gender>
<Race>A</Race>
</Person>
</Demographic_Information>
another alternative:
<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output encoding="utf-8" method="xml" indent="yes"/>
<xsl:template match="/Demographic_Information">
<Demographic_Information>
<xsl:for-each select="./*[starts-with(name(), 'Age')][normalize-space()]">
<xsl:variable name="i"><xsl:value-of select="string(position())"></xsl:value-of></xsl:variable>
<Person>
<xsl:attribute name="subject"><xsl:value-of select="$i"/></xsl:attribute>
<Age>
<xsl:apply-templates select="node()" />
</Age>
<xsl:apply-templates select="../*[starts-with(name(), 'Gender')][position()=$i]"/>
<xsl:apply-templates select="../*[starts-with(name(), 'Race')][position()=$i]"/>
</Person>
</xsl:for-each>
</Demographic_Information>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Age')]">
<xsl:copy>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Gender')]">
<Gender>
<xsl:apply-templates select="node()" />
</Gender>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Race')]">
<Race>
<xsl:apply-templates select="node()" />
</Race>
</xsl:template>
<!-- defaults -->
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The following will do. But perhaps not the fastest solution.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*[substring(name(),1,3) = 'Age']">
<xsl:variable name="id" select="substring(name(),4)"/>
<Person id="{$id}">
<Age><xsl:value-of select="."/></Age>
<Gender><xsl:value-of select="//*[name() =concat('Gender',$id)]"/></Gender>
<Race><xsl:value-of select="//*[name() =concat('Race',$id)]"/></Race>
</Person>
</xsl:template>
<xsl:template match="/" >
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>