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



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

**完整的输入XML文件:**

它由循环元素StockLineItem组成。对于每个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.XMLSORTOIDOC2.XMLSORTOICDOC3.XML

我们正在尝试的输出

SORTOIDOC_SalesForceOrderNumber

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

提前感谢!

我们正在尝试的输出

SORTOIDOC_SalesForceOrderNumber

由于上下文元素是Orders,因此可以使用以下相对路径来获得相应的SalesForceOrderNumber元素:

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

使用SalesForceOrderNumber而不是position()

相关内容

  • 没有找到相关文章

最新更新