【问题标题】:Apply xslt template on many lines在多行上应用 xslt 模板
【发布时间】:2018-02-27 07:54:45
【问题描述】:

我有一个 XML,它可以有许多 PurchaseOrderLine 部分,由 LineNumber(10、20、30 等)区分。在每一个中,我都需要在 UserArea 部分中插入一个带有 TaxID 的最后一个字符的子部分。我当前的 xslt 部分工作,因为它总是从第一行获取最后一个字符,并将其放在所有行上。我无法让它单独查看每一行。

谢谢。

传入的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<SyncPurchaseOrder xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:inforpo="http://schema.infor.com/eam/inforbodmodel/100/purchaseorder" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" languageCode="en-US" releaseID="9.2" systemEnvironmentCode="Production" versionID="2.9.1">
    <DataArea>
        <PurchaseOrder>
            <PurchaseOrderLine>
                <LineNumber>10</LineNumber>
                <BuyerParty>
                    <PartyIDs>
                        <TaxID>BFPAA000</TaxID>
                    </PartyIDs>
                </BuyerParty>
                <UserArea>
                    <Property>
                        <NameValue name="eam.CostCode">a1</NameValue>
                    </Property>
                </UserArea>
            </PurchaseOrderLine>
            <PurchaseOrderLine>
                <LineNumber>20</LineNumber>
                <BuyerParty>
                    <PartyIDs>
                        <TaxID>BFPAA000XCCN</TaxID>
                    </PartyIDs>
                </BuyerParty>
                <UserArea>
                    <Property>
                        <NameValue name="eam.CostCode">a1</NameValue>
                    </Property>
                </UserArea>
            </PurchaseOrderLine>
        </PurchaseOrder>
    </DataArea>
</SyncPurchaseOrder>

当前 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schema.infor.com/InforOAGIS/2" xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="my java">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />    

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//my:PurchaseOrder/my:PurchaseOrderLine/my:UserArea/my:Property[my:NameValue/@name='eam.CostCode']">
      <xsl:copy-of select="."/> 
    <Property>
        <NameValue name="llp.lastCharTaxCode">
            <xsl:value-of select="substring(//my:PurchaseOrder/my:PurchaseOrderLine/my:BuyerParty/my:PartyIDs/my:TaxID, string-length(//my:PurchaseOrder/my:PurchaseOrderLine/my:BuyerParty/my:PartyIDs/my:TaxID), 1)" />
        </NameValue>
    </Property>
   </xsl:template> 
</xsl:stylesheet>

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<SyncPurchaseOrder xmlns="http://schema.infor.com/InforOAGIS/2"
                   xmlns:inforpo="http://schema.infor.com/eam/inforbodmodel/100/purchaseorder"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   languageCode="en-US"
                   releaseID="9.2"
                   systemEnvironmentCode="Production"
                   versionID="2.9.1">
    <DataArea>
        <PurchaseOrder>
            <PurchaseOrderLine>
                <LineNumber>10</LineNumber>
                <BuyerParty>
                    <PartyIDs>
                        <TaxID>BFPAA000</TaxID>
                    </PartyIDs>
                </BuyerParty>
                <UserArea>
                    <Property>
                        <NameValue name="eam.CostCode">a1</NameValue>
                    </Property>
                    <Property>
                        <NameValue name="llp.lastCharTaxCode">0</NameValue>
                    </Property>
                </UserArea>
            </PurchaseOrderLine>
            <PurchaseOrderLine>
                <LineNumber>20</LineNumber>
                <BuyerParty>
                    <PartyIDs>
                        <TaxID>BFPAA000XCCN</TaxID>
                    </PartyIDs>
                </BuyerParty>
                <UserArea>
                    <Property>
                        <NameValue name="eam.CostCode">a1</NameValue>
                    </Property>
                    <Property>
                        <NameValue name="llp.lastCharTaxCode">N</NameValue>
                    </Property>
                </UserArea>
            </PurchaseOrderLine>
        </PurchaseOrder>
    </DataArea>
</SyncPurchaseOrder>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    除了将模板与&lt;Property&gt; 匹配外,您还可以将其与&lt;UserArea&gt; 匹配,如下所示。

    <xsl:template match="my:UserArea">
        <xsl:copy>
            <xsl:copy-of select="my:Property" />
            <xsl:variable name="taxID" select="../my:BuyerParty/my:PartyIDs/my:TaxID" />
            <Property>
                <NameValue name="llp.lastCharTaxCode">
                    <xsl:value-of select="substring($taxID, string-length($taxID), 1)" />
                </NameValue>
            </Property>
        </xsl:copy>
    </xsl:template>
    

    这将提供所需的输出

    <SyncPurchaseOrder xmlns="http://schema.infor.com/InforOAGIS/2"
        xmlns:inforpo="http://schema.infor.com/eam/inforbodmodel/100/purchaseorder"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" languageCode="en-US"
        releaseID="9.2" systemEnvironmentCode="Production" versionID="2.9.1">
        <DataArea>
            <PurchaseOrder>
                <PurchaseOrderLine>
                    <LineNumber>10</LineNumber>
                    <BuyerParty>
                        <PartyIDs>
                            <TaxID>BFPAA000</TaxID>
                        </PartyIDs>
                    </BuyerParty>
                    <UserArea>
                        <Property>
                            <NameValue name="eam.CostCode">a1</NameValue>
                        </Property>
                        <Property>
                            <NameValue name="llp.lastCharTaxCode">0</NameValue>
                        </Property>
                    </UserArea>
                </PurchaseOrderLine>
                <PurchaseOrderLine>
                    <LineNumber>20</LineNumber>
                    <BuyerParty>
                        <PartyIDs>
                            <TaxID>BFPAA000XCCN</TaxID>
                        </PartyIDs>
                    </BuyerParty>
                    <UserArea>
                        <Property>
                            <NameValue name="eam.CostCode">a1</NameValue>
                        </Property>
                        <Property>
                            <NameValue name="llp.lastCharTaxCode">N</NameValue>
                        </Property>
                    </UserArea>
                </PurchaseOrderLine>
            </PurchaseOrder>
        </DataArea>
    </SyncPurchaseOrder>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多