如何使用 XSLT 将输入 xml 属性附加到多个输出 xml 文件的名称中

How do i append input xml attribute into name of multiple output xml file using XSLT

我们有以下输入 xml 文件,我们在其中尝试 XSLT 处理指令。

** 完成输入 XML 文件:**

它由循环元素组成 StockLineItem.for 每个单独的 OrderHeader 和 OrderDetails 我们可以有很多 StockineItems。文档仅告诉单个 SalesforceOrderNumber 元素。因此,我们必须在每次 XML 写入时使用 xml 文件名输出此元素。

<?xml version="1.0" encoding="Windows-1252"?>
<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"
             xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
   <Orders>
      <OrderHeader>
         <CustomerPoNumber/>
         <OrderActionType>A</OrderActionType>
         <NewCustomerPoNumber/>
         <Supplier/>
         <Customer>005352</Customer>
         <OrderDate>2016-03-28</OrderDate>
         <InvoiceTerms/>
         <Currency/>
         <ShippingInstrs/>
         <CustomerName>TARGET DC0594</CustomerName>
         <ShipAddress1/>
         <ShipAddress2/>
         <ShipAddress3/>
         <ShipAddress4/>
         <ShipAddress5/>
         <ShipPostalCode/>
         <Email/>
         <OrderDiscPercent1/>
         <OrderDiscPercent2/>
         <OrderDiscPercent3/>
         <Warehouse/>
         <SpecialInstrs/>
         <SalesOrder/>
         <OrderType/>
         <MultiShipCode/>
         <ShipAddressPerLine/>
         <AlternateReference/>
         <Salesperson/>
         <Branch/>
         <Area/>
         <RequestedShipDate/>
         <InvoiceNumberEntered/>
         <InvoiceDateEntered/>
         <OrderComments/>
         <Nationality/>
         <DeliveryTerms/>
         <TransactionNature/>
         <TransportMode/>
         <ProcessFlag/>
         <TaxExemptNumber/>
         <TaxExemptionStatus/>
         <GstExemptNumber/>
         <GstExemptionStatus/>
         <CompanyTaxNumber/>
         <CancelReasonCode/>
         <DocumentFormat/>
         <State/>
         <CountyZip/>
         <City/>
         <InvoiceWholeOrderOnly/>
         <SalesOrderPromoQualifyAction/>
         <SalesOrderPromoSelectAction/>
         <GlobalTradePromotionCodes/>
         <eSignature/>
         <SalesForceOrderNumber>ORD-374881</SalesForceOrderNumber>
      </OrderHeader>
      <OrderDetails>
         <StockLine>
            <CustomerPoLine>9999</CustomerPoLine>
            <LineCancelCode/>
            <StockCode>ABSO-NHO-5OZ-01</StockCode>
            <StockDescription>NHO AFRICAN BLACK SOAP 5OZ</StockDescription>
            <Warehouse/>
            <CustomersPartNumber/>
            <OrderQty>3.0</OrderQty>
            <OrderUom>EA</OrderUom>
            <Price>56.0</Price>
            <PriceUom>EA</PriceUom>
            <PriceCode/>
            <AlwaysUsePriceEntered>Y</AlwaysUsePriceEntered>
            <Units/>
            <Pieces/>
            <ProductClass/>
            <LineDiscPercent1/>
            <LineDiscPercent2/>
            <LineDiscPercent3/>
            <AlwaysUseDiscountEntered/>
            <CustRequestDate/>
            <CommissionCode/>
            <LineShipDate/>
            <LineDiscValue/>
            <LineDiscValFlag/>
            <OverrideCalculatedDiscount/>
            <UserDefined>1</UserDefined>
            <NonStockedLine/>
            <NsProductClass/>
            <NsUnitCost/>
            <UnitMass/>
            <UnitVolume/>
            <StockTaxCode/>
            <StockNotTaxable/>
            <StockFstCode/>
            <StockNotFstTaxable/>
            <AllocationAction/>
            <ConfigPrintInv/>
            <ConfigPrintDel/>
            <ConfigPrintAck/>
            <TariffCode/>
            <LineMultiShipCode/>
            <SupplementaryUnitsFactor/>
            <ReserveStock/>
            <ReserveStockRequestAllocs/>
            <TradePromotionCodes/>
         </StockLine>
      </OrderDetails>
  </Orders>
</SalesOrders>

我们尝试对其进行 XSLT 转换。

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>

<xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

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

<xsl:template match="/">
    <xsl:for-each-group select="SalesOrders/Orders" group-by="OrderHeader">
        <xsl:result-document href="SORTOIDOC{position()}.xml">
            <SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="xmlfilename.XSD">
                <xsl:apply-templates select="current-group()"/>
            </SalesOrders>
        </xsl:result-document>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

转换后输出结果:
以这种方式拆分 SORTOIDOC1.XML SORTOIDOC2.XML SORTOICDOC3.XML

我们正在尝试的输出

SORTOIDOC_SalesForceOrderNumber

例如:SORTOIDOC_ORD-380804.XML 而不是 SORTOIDOC1.XML

提前致谢!

而不是 position() 使用 SalesForceOrderNumber.

Output we are trying for

SORTOIDOC_SalesForceOrderNumber

由于上下文元素是Orders,可以使用下面的相对路径获取对应的SalesForceOrderNumber元素:

<xsl:result-document href="SORTOIDOC_{OrderHeader/SalesForceOrderNumber}.xml">