【发布时间】:2022-01-16 11:17:18
【问题描述】:
您好,我遇到的问题是,我希望模板匹配仅在另一个 XPATH 中满足特定条件时才执行
这是我的输入 XML
<Items OrderReference="" TransactionId="">
<Item Type="OPEN" EnterpriseCode="XXXX"
FirstDate="2021-12-10" IgnorePromised="N"
IgnoreUnpromised="N" ItemID="Item1"
LastShipDate="2022-01-10" OrganizationCode="YK"
ProductClass="GOOD" TotalRequiredQty="1.00" UnitOfMeasure="EACH">
<Supplies>
<Supply FirstDate="2021-12-10" IsUnplannedSupply="N"
LastShipDate="2023-12-10" EnterpriseCode="XXXX"
Quantity="100.00" Segment="" SegmentType=""
ShipNode="YKDC" Track="Y"/>
</Supplies>
</Item>
<Item DemandType="OPEN_ORDER" EnterpriseCode="XXXX"
FirstDate="2021-12-10" IgnorePromised="N"
IgnoreUnpromised="N" ItemID="Item2" LastShipDate="2022-01-10"
OrganizationCode="YK" ProductClass="GOOD"
TotalRequiredQty="1.00" UnitOfMeasure="EACH">
<Supplies>
<Supply FirstDate="2021-12-10" IsUnplannedSupply="N"
LastShipDate="2023-12-10" EnterpriseCode="XXXX"
Quantity="152.00" Segment="" SegmentType=""
ShipNode="BDDC" SoftAssignedQty="0.00" TagNumber="" Track="Y"/>
<Supply FirstDate="2021-12-10" IsUnplannedSupply="N"
LastShipDate="2023-12-10" EnterpriseCode="XXXX"
Quantity="100.00" Segment="" SegmentType=""
ShipNode="YKDC" SoftAssignedQty="0.00" TagNumber="" Track="Y"/>
</Supplies>
</Item>
<Item DemandType="OPEN_ORDER" EnterpriseCode="XXXX"
FirstDate="2021-12-10" IgnorePromised="N"
IgnoreUnpromised="N" ItemID="Item3" LastShipDate="2022-01-10"
OrganizationCode="YK" ProductClass="GOOD"
TotalRequiredQty="1.00" UnitOfMeasure="EACH">
<Supplies>
<Supply FirstDate="2021-12-10" IsUnplannedSupply="N"
LastShipDate="2023-12-10" EnterpriseCode="XXXX"
Quantity="952.00" Segment="" SegmentType=""
ShipNode="BDDC" SoftAssignedQty="0.00" TagNumber="" Track="Y"/>
<Supply FirstDate="2021-12-10" IsUnplannedSupply="N"
LastShipDate="2023-12-10" EnterpriseCode="XXXX"
Quantity="776.00" Segment="" SegmentType=""
ShipNode="YKDC" SoftAssignedQty="0.00" TagNumber="" Track="Y"/>
</Supplies>
</Item>
</Items>
仅当存在 YKDC 是特定项目的唯一子节点且所有其他项目同时存在 BDDC 和 YKDC 的节点时,我才想将 ShipNode BDDC 的数量清零。
这是我目前的解决方案
<xsl:stylesheet exclude-result-prefixes="xalan" version="2.0"
xmlns:xalan="http://xml.apache.org/xslt" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml" xalan:indent-amount="2"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Items/Item/Supplies/Supply[@ShipNode='BDDC']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:if test="not(preceding-sibling::Supply) and ../Supply[@ShipNode='YKDC']">
<xsl:attribute name="Quantity">
<xsl:value-of select="'0.00'"/>
</xsl:attribute>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
```
However I am unable to test my another main condition for Item1 where availability is only on YKDC.
Please excuse for long and convoluted question.
【问题讨论】:
标签: xml xslt xml-parsing xslt-1.0 xalan