【问题标题】:XSL 模板的条件执行
【发布时间】: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


    【解决方案1】:

    希望有一个冗长而复杂的匹配模式来检查所有内容并更改数量

    <xsl:template match="/Items[Item[not (*[2]) and not(*/*[2]) and Supplies/Supply[@ShipNode='YKDC']] and Item[*/*[2]][Supplies/Supply[@ShipNode='YKDC'] and Supplies/Supply[@ShipNode='BDDC']]]/Item/Supplies/Supply[@ShipNode ='BDDC']/@Quantity">
        <xsl:attribute name="{name()}">0</xsl:attribute>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2017-11-06
      • 2021-05-30
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多