Sydney Office
SYDNEY
Here's an answer similar to Dimitre's. I'd already written it, so I thought I'd go ahead and post it...
XSLT (2.0)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:choose>
<xsl:when test="starts-with(local-name(), 'W_') and ends-with(local-name(), '_Dlr')">
<xsl:variable name="period" select="substring(local-name(),3,8)"/>
<Expenditure>
<Period><xsl:value-of select="$period"/></Period>
<Value><xsl:apply-templates/></Value>
<Spots><xsl:value-of select="following-sibling::*[starts-with(local-name(), 'W_') and ends-with(local-name(),concat($period,'_Spots'))]"/></Spots>
</Expenditure>
</xsl:when>
<xsl:when test="ends-with(local-name(), '_Spots')"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Also, if you're using XSLT 2.0 (I assumed you were since you used ends-with()
) you can use tokenize()
to capture pieces of the name.
Example:
<xsl:variable name="period" select="tokenize(local-name(),'_')[2]"/>
I would start with a template like
<xsl:template match="Row/*[starts-with(name(), 'W_') and
ends-with(name(), '_Dlr')]">
which should more precisely match the element you want matched. As to how to select the adjacent <W_${DATE}_Spots>
sibling element... why not use the following-sibling
XPath axis with the proper string?
<xsl:template match="Row/*[starts-with(local-name(), 'W_') and
ends-with(local-name(), '_Dlr')]">
<xsl:variable name="date" select="substring(local_name(),3,8)"/>
...
<xsl:apply-templates select="following-sibling::*[local-name() ==
concat('W_', concat($date, '_Spots'))]"/>
...
</xsl:template>
<xsl:template match="Row/*[starts-with(local-name(), 'W_') and
ends-with(local-name(), '_Spots')]">
<xsl:variable name="date" select="substring(local_name(),3,8)"/>
...
</xsl:template>
BTW, this looks like an endless loop waiting to happen:
<xsl:template match="*">
...
<xsl:apply-templates select="node()"/>
...
</xsl:template>
I am sure there are a few mistakes in my answer, but it should be helpful in some capacity anyway.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match=
"*[starts-with(name(),'W_')
and
substring-after(substring-after(name(),'_'),'_')='Dlr'
and
text()
]">
<Expenditure>
<Period>
<xsl:value-of select="substring(name(),3,8)"/>
</Period>
<Value>
<xsl:apply-templates/>
</Value>
<xsl:apply-templates mode="extract" select=
"following-sibling::*[1]
[starts-with(name(),'W_')
and
substring-after(substring-after(name(),'_'),'_')='Spots'
]
"/>
</Expenditure>
</xsl:template>
<xsl:template mode="extract" match=
"*[starts-with(name(),'W_')
and
substring-after(substring-after(name(),'_'),'_')='Spots'
]
">
<Spots><xsl:value-of select="."/></Spots>
</xsl:template>
<xsl:template match=
"*[starts-with(name(),'W_')
and
substring-after(substring-after(name(),'_'),'_')='Spots'
]
"/>
</xsl:stylesheet>
when applied on the provided source XML document:
<RowSet>
<Row>
<Location_Long_Desc>Sydney Office</Location_Long_Desc>
<Location_Code>SYDNEY</Location_Code>
<Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
<Daypart_Code>PEANIG</Daypart_Code>
<W_20050703_Dlr>30849.3</W_20050703_Dlr>
<W_20050703_Spots>9</W_20050703_Spots>
<W_20050710_Dlr>16.35</W_20050710_Dlr>
<W_20050710_Spots>19</W_20050710_Spots>
</Row>
</RowSet>
produces the wanted, correct result:
<RowSet>
<Row>
<Location_Long_Desc>Sydney Office</Location_Long_Desc>
<Location_Code>SYDNEY</Location_Code>
<Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
<Daypart_Code>PEANIG</Daypart_Code>
<Expenditure>
<Period>20050703</Period>
<Value>30849.3</Value>
<Spots>9</Spots>
</Expenditure>
<Expenditure>
<Period>20050710</Period>
<Value>16.35</Value>
<Spots>19</Spots>
</Expenditure>
</Row>
</RowSet>
when applied on the second provided XML document, requested in an Update by the OP:
<RowSet>
<Row>
<Location_Long_Desc>Sydney Office</Location_Long_Desc>
<Location_Code>SYDNEY</Location_Code>
<Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
<Daypart_Code>PEANIG</Daypart_Code>
<W_20050703_Dlr>30849.3</W_20050703_Dlr>
<W_20050710_Dlr>16.35</W_20050710_Dlr>
<W_20050710_Spots>19</W_20050710_Spots>
</Row>
</RowSet>
again the wanted, correct result (No <Spot>
element is generated if the immediate sibling isn't a W_nnnnnnnn_Spots
) is produced:
<RowSet>
<Row>
<Location_Long_Desc>Sydney Office</Location_Long_Desc>
<Location_Code>SYDNEY</Location_Code>
<Daypart_Long_Desc>Peak Night</Daypart_Long_Desc>
<Daypart_Code>PEANIG</Daypart_Code>
<Expenditure>
<Period>20050703</Period>
<Value>30849.3</Value>
</Expenditure>
<Expenditure>
<Period>20050710</Period>
<Value>16.35</Value>
<Spots>19</Spots>
</Expenditure>
</Row>
</RowSet>
Do note:
The use of the identity rule to copy any node "as-is".
The overriding of the identity template only for W_nnnnnnnn_Dlr
elements.
The overriding of the identity template with an empty template matching W_nnnnnnnn_Spots
elements.
The use of the standard XPath functions: name(), starts-with() and substring-after()
The function ends-with() is only available in XPath 2.0 (XSLT 2.0) and isn't used in this XSLT 1.0 solution.