Hi,
This is my first posting on this site and hopefully I've understood the posting requirements correctly. My XML example below is a small extract of a more complicated file. I need to copy the XML via XSLT except for 1 element (StoreNumber) that is based on other factors.
XML example:
Code:
<Store>
<StoreHeader>
<StoreNumber>1234</StoreNumber>
<StoreCountry>AUS</StoreCountry>
</StoreHeader>
<Transaction>
<Item>12123</Item>
<Code>02341CD</Code>
<Sale>-10.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>17652</Item>
<Code>02342CH</Code>
<Sale>-50.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>00947</Item>
<Code>02343CD</Code>
<Sale>-50.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>16467</Item>
<Code>02344CD</Code>
<Sale>-79.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>01184</Item>
<Code>02345CH</Code>
<Sale>-7.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
</Store>
Exception Rules:
A.
If ALL ‘Sale’ elements have a negative value (a ‘-‘ prefix) then the StoreNumber value must change to
1234#1
Code:
<StoreNumber>1234#1</StoreNumber>
B.
If the value of the ‘StoreCountry’ element is ‘AUS’ then the StoreNumber value must change to
1234AUS
Code:
<StoreNumber>1234AUS</StoreNumber>
C.
If ALL ‘Sale’ elements have a negative value AND the ‘StoreCountry’ value is ‘AUS’ then the StoreNumber value should be
1234#1AUS
Code:
<StoreNumber>1234#1AUS</StoreNumber>
Otherwise the StoreNumber value remains the same.
My 2 basic XSLT approaches:
1
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:when test="name()='StoreNumber'">
<!-- Code needed -->
</xsl:when>
</xsl:stylesheet>
OR
2
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="name()='StoreNumber'">
<xsl:element name="StoreNumber">
<!-- Code needed -->
</xsl:element>
</xsl:when>
<xsl:otherwise>
<!-- -->
<xsl:call-template name="copy"/>
<!-- -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Copy an element and all of its attributes -->
<xsl:template name="copy">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In this example the XML output needs to be similar to the original file with the one exception (StoreNumber):
XML output file
Code:
<Store>
<StoreHeader>
<StoreNumber>1234#1AUS</StoreNumber>
<StoreCountry>AUS</StoreCountry>
</StoreHeader>
<Transaction>
<Item>12123</Item>
<Code>02341CD</Code>
<Sale>-10.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>17652</Item>
<Code>02342CH</Code>
<Sale>-50.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>00947</Item>
<Code>02343CD</Code>
<Sale>-50.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>16467</Item>
<Code>02344CD</Code>
<Sale>-79.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
<Transaction>
<Item>01184</Item>
<Code>02345CH</Code>
<Sale>-7.000</Sale>
<Date>2017-04-20</Date>
</Transaction>
</Store>
Maybe my approach is incorrect but I don’t know how to access other elements during the name()=’StoreNumber’ condition (I have also tried a recursive template). Is it possible to use the exception rule here or do I need to transform the whole XML? Can anyone assist please?
Again, my XML is a simplified example consisting of many other elements.
Thanks in advance,