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

时间:2016-03-28 19:35:53

标签: xml xslt xslt-1.0 xslt-2.0 xslt-grouping

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

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

它包含Loop元素作为StockLineItem。对于每个OrderHeader和OrderDetails,我们可以有许多StockineItems。 Document仅告诉单个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

FOR EX:SORTOIDOC_ORD-380804.XML而不是SORTOIDOC1.XML

提前致谢!

2 个答案:

答案 0 :(得分:1)

  

我们正在尝试输出

     

SORTOIDOC_SalesForceOrderNumber

由于context元素是Orders,您可以使用以下相对路径来获取相应的SalesForceOrderNumber元素:

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

答案 1 :(得分:0)

而不是position()使用SalesForceOrderNumber