Reputation: 245
I have an XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<row>
<cell colname="1"><Name>SomeValue<Ref format="Ref" Idref="ABC"/></Name></cell>
<cell colname="2"><Selection>
<Config><Configuration>AAA</Configuration></Config>
<Config><Configuration>BBB</Configuration></Config>
</Selection></cell>
<cell colname="6"><Notes>
<Text><IteratorDef>
<IteratorTag Id="ABC">DDi</IteratorTag>
<IteratorList>
<IteratorChoice>05</IteratorChoice>
<IteratorChoice>10</IteratorChoice>
<IteratorChoice>15</IteratorChoice>
</IteratorList>
</IteratorDef></Text>
</Notes></cell>
</row>
I'd like to get the result like this:
The Ref points to the IteratorTag: There are two Configuration namely AAA and BBB. For each Configuration there are several IteratorChoice values. Each need to be concatenated with the Name value + IteratorTag value so SomeValue+DDi+05, SomeValue+DDi+10, and SomeValue+DDi+15.
AAA
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
BBB
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
My thought is write a loop in a loop with for-each keying on IteratorList and Configuration tags (Configuration loop will be inside IteratorList loop), is this the correct approach using XSLT ?.
TIA,
John
Upvotes: 0
Views: 144
Reputation: 12154
solution1: this works only on provided input.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:variable name="newline" select="' '"/>
<xsl:template match="row/cell/Selection/Config/Configuration">
<xsl:value-of select="concat(.,$newline)"/><!--AAA, BBB, etc-->
<xsl:for-each select="/row/cell/Notes/Text/IteratorDef/IteratorList/IteratorChoice">
<xsl:variable name="zstring" select="."/>
<xsl:for-each select="ancestor::IteratorDef/IteratorTag">
<xsl:variable name="ystring" select="concat(.,$zstring)"/>
<xsl:for-each select="ancestor::row/cell/Name">
<xsl:value-of select="concat(normalize-space(.),$ystring,$newline)"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
<!--this is for extra line space after each CONFIGURATION block-->
<xsl:value-of select="$newline"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Edit: Solution 2.. I have used no reverse XPath but complete XPath .. this works on robust XML. the method is slightly different .. here I am going from high level (XPath) to lower level. This is the code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:variable name="newline" select="' '"/>
<xsl:template match="row/cell/Selection/Config/Configuration">
<xsl:value-of select="concat(.,$newline)"/><!--AAA, BBB, etc-->
<xsl:for-each select="/row/cell/Name">
<xsl:variable name="astring" select="normalize-space(.)"/>
<xsl:for-each select="/row/cell/Notes/Text/IteratorDef/IteratorTag">
<xsl:variable name="bstring" select="concat($astring,normalize-space(.))"/>
<xsl:for-each select="/row/cell/Notes/Text/IteratorDef/IteratorList/IteratorChoice">
<xsl:value-of select="concat($bstring,normalize-space(.),$newline)"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
<!--this is for extra line space after each CONFIGURATION block-->
<xsl:value-of select="$newline"/>
</xsl:template>
<!--disable all unwanted text nodes-->
<xsl:template match="text()"/>
</xsl:stylesheet>
If this is your XML:
<?xml version="1.0" encoding="UTF-8"?>
<row>
<cell colname="1">
<Name>
SomeValue<Ref format="Ref" Idref="ABC"/>
</Name>
</cell>
<cell colname="2">
<Selection>
<Config>
<Configuration>AAA</Configuration>
</Config>
<Config>
<Configuration>BBB</Configuration>
</Config>
<Config>
<Configuration>CCC</Configuration>
</Config>
</Selection>
</cell>
<cell colname="6">
<Notes>
<Text>
<IteratorDef>
<IteratorTag Id="ABC">DDi</IteratorTag>
<IteratorTag Id="ABC">EEi</IteratorTag>
<IteratorList>
<IteratorChoice>05</IteratorChoice>
<IteratorChoice>10</IteratorChoice>
<IteratorChoice>15</IteratorChoice>
<IteratorChoice>20</IteratorChoice>
</IteratorList>
</IteratorDef>
</Text>
</Notes>
</cell>
</row>
This would be your output:
AAA
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
SomeValueDDi20
SomeValueEEi05
SomeValueEEi10
SomeValueEEi15
SomeValueEEi20
BBB
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
SomeValueDDi20
SomeValueEEi05
SomeValueEEi10
SomeValueEEi15
SomeValueEEi20
CCC
SomeValueDDi05
SomeValueDDi10
SomeValueDDi15
SomeValueDDi20
SomeValueEEi05
SomeValueEEi10
SomeValueEEi15
SomeValueEEi20
Upvotes: 2