How do i append input xml attribute into name of m

2019-09-13 10:36发布

问题:

We have following input xml file in which we are trying XSLT processing instructions.

** Complete Input XML file :**

It consist of Loop element as StockLineItem.for every single OrderHeader and OrderDetails we can have many StockineItems. Document tells single SalesforceOrderNumber element only. Hence We have to Output this element with xml Filename on each XML write.

<?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>

We tried XSLT transformation on it.

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>

The Output after transformation result: that is
Splitting in this manner SORTOIDOC1.XML SORTOIDOC2.XML SORTOICDOC3.XML

Output we are trying for

SORTOIDOC_SalesForceOrderNumber

FOR EX: SORTOIDOC_ORD-380804.XML instead of SORTOIDOC1.XML

Thanks in advance!

回答1:

Output we are trying for

SORTOIDOC_SalesForceOrderNumber

Since the context element is Orders, you can use the following relative path to get the corresponding SalesForceOrderNumber element :

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


回答2:

Instead of position() use SalesForceOrderNumber.