Reputation: 79
How to check two values to see if they are different? -> Concerns the nodes Item.DeliveryDate
and Item.OrigDeliveryDate
.
If yes, a new node LineChangeDeliveryDate
should be created with the value yes
, otherwise with the value no
.
XML at the moment:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<OrderResponse>
<Interchange>
<Interchange_Control_Number>5637248870</Interchange_Control_Number>
</Interchange>
<HeaderInformation>
<ConfirmDocNum>SO0009783-1</ConfirmDocNum>
<TransportDetails>
<DeliveryMode>ROU</DeliveryMode>
</TransportDetails>
</HeaderInformation>
<LineInformation>
<Item>
<DeliveryDate>2020-01-27</DeliveryDate>
<OrigDeliveryDate>2019-12-07</OrigDeliveryDate>
</Item>
</LineInformation>
</OrderResponse>
XML new/correct:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<OrderResponse>
<Interchange>
<Interchange_Control_Number>5637248870</Interchange_Control_Number>
</Interchange>
<HeaderInformation>
<ConfirmDocNum>SO0009783-1</ConfirmDocNum>
<TransportDetails>
<DeliveryMode>ROU</DeliveryMode>
</TransportDetails>
</HeaderInformation>
<LineInformation>
<Item>
<DeliveryDate>2020-01-27</DeliveryDate>
<OrigDeliveryDate>2019-12-07</OrigDeliveryDate>
<LineChangeDeliveryDate>yes</LineChangeDeliveryDate>
</Item>
</LineInformation>
</OrderResponse>
Current XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:key name="header_text" match="HeaderText" use="Text"/>
<xsl:key name="line_text" match="LineText" use="concat(../LineNum, '|', Text)"/>
<xsl:key name="allowance_charge_header" match="AllowanceOrCharge_Header" use="concat(Code, '|', Amount)"/>
<xsl:key name="allowance_charge_line" match="AllowanceOrCharge_Line" use="concat(../LineNum, '|', Code, '|', Amount)"/>
<xsl:key use="concat(../LineNum, '|', Text)" match="LineText" name="line_text"/>
<!-- Identity-Template für die nicht explizit benannten Elemente -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="HeaderText[generate-id() != generate-id(key('header_text', Text)[1])]" />
<xsl:template match="LineText[generate-id() != generate-id(key('line_text', concat(../LineNum, '|', Text))[1])]" />
<xsl:template match="AllowanceOrCharge_Header[generate-id() != generate-id(key('allowance_charge_header', concat(Code, '|', Amount))[1])]" />
<xsl:template match="AllowanceOrCharge_Line[generate-id() != generate-id(key('allowance_charge_line', concat(../LineNum, '|', Code, '|', Amount))[1])]" />
<xsl:template match="LineText[generate-id() != generate-id(key('line_text', concat(../LineNum, '|', Text))[1])]"/>
<xsl:template match="Item">
<xsl:copy>
<LineChangeDeliveryDate>
<xsl:choose>
<xsl:when test="OrigDeliveryDate = DeliveryDate"/>
<xsl:value-of select="'yes'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'no'"/>
</xsl:otherwise>
</xsl:choose>
</LineChangeDeliveryDate>
<!--copy all other nodes-->
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- delete empty nodes -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/>
</xsl:stylesheet>
Upvotes: 0
Views: 52
Reputation: 163635
In the template rule with match="Item"
, do
<LineChangeDeliveryDate>
<xsl:choose>
<xsl:when test="OrigDeliveryDate = DeliveryDate">
<xsl:value-of select="'yes'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'no'/>
</xsl:otherwise>
</xsl:choose>
</LineChangeDeliveryDate>
This is one of many cases where the XSLT 2.0 code is much more concise:
<LineChangeDeliveryDate>
<xsl:value-of select="if (OrigDeliveryDate = DeliveryDate) then 'yes' else 'no'"/>
</LineChangeDeliveryDate>
and in 3.0 it reduces further to
<LineChangeDeliveryDate>{
if (OrigDeliveryDate = DeliveryDate) then 'yes' else 'no'
}</LineChangeDeliveryDate>
Upvotes: 1