I have a XML like this
<?xml version="1.0" encoding="UTF-8"?>
<Orders>
<Order>
<Order_Number>8221</Order_Number>
<Order_Date>2019-03-12 04:28</Order_Date>
<Billing_First_Name>sdfsdff</Billing_First_Name>
<Billing_Last_Name>zam</Billing_Last_Name>
<Billing_Company/>
<Billing_Address>asd aasdasd, asd</Billing_Address>
<Billing_City>asd</Billing_City>
<Billing_State/>
<Billing_Postcode>5968</Billing_Postcode>
<Billing_Country>SE</Billing_Country>
<Billing_Email>sdfdsf@sdfsdf.se</Billing_Email>
<Billing_Phone>454565798</Billing_Phone>
<Shipping_First_Name>sdfsdf</Shipping_First_Name>
<Shipping_Last_Name>sdfsdf</Shipping_Last_Name>
<Shipping_Address>asd aasdasd, asd</Shipping_Address>
<Shipping_City>asd</Shipping_City>
<Shipping_State/>
<Shipping_Postcode>5968</Shipping_Postcode>
<Shipping_Country>XX</Shipping_Country>
<Products>
<Product>
<Line_Id>1</Line_Id>
<Name>My-Love</Name>
<Product_Id>7978</Product_Id>
<Product_Variation>A4</Product_Variation>
<Variation_Id>0</Variation_Id>
<Qty>1</Qty>
</Product>
<Product>
<Line_Id>2</Line_Id>
<Name>Other</Name>
<Product_Id>7697</Product_Id>
<Product_Variation>A5</Product_Variation>
<Variation_Id>0</Variation_Id>
<Qty>1</Qty>
</Product>
</Products>
</Order>
</Orders>
I split the XML with XSLT like this.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<!-- Create an output file per order from the previous query -->
<xsl:for-each select="Orders/Order/Products/Product">
<xsl:variable name="InputFile" select="base-uri()"/>
<xsl:variable name="OutputFile" select="Line_Id"/>
<xsl:result-document href="{$OutputFile}.xml" method="xml">
<xsl:copy-of select="current()"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This works good and I get new XML files like this
File_1:
<Product>
<Line_Id>1</Line_Id>
<Name>My-Love</Name>
<Product_Id>7978</Product_Id>
<Product_Variation>A4</Product_Variation>
<Variation_Id>0</Variation_Id>
<Qty>1</Qty>
</Product>
File_2:
<Product>
<Line_Id>2</Line_Id>
<Name>Other</Name>
<Product_Id>7697</Product_Id>
<Product_Variation>A5</Product_Variation>
<Variation_Id>0</Variation_Id>
<Qty>1</Qty>
</Product>
But my problem is that when I split the XML I also want to pick up the value in the tag and put it last in tag with a space in the splitted XML-file.
The splitted files should be like this (see the Name and Variation tag):
File_1:
<Product>
<Line_Id>1</Line_Id>
<Name>My-Love A4</Name>
<Product_Id>7978</Product_Id>
<Product_Variation>A4</Product_Variation>
<Variation_Id>0</Variation_Id>
<Qty>1</Qty>
</Product>
File_2:
<Product>
<Line_Id>2</Line_Id>
<Name>Other A5</Name>
<Product_Id>7697</Product_Id>
<Product_Variation>A5</Product_Variation>
<Variation_Id>0</Variation_Id>
<Qty>1</Qty>
</Product>
Instead of using xsl:copy-of
, use xsl:apply-templates
, then have a template matching name
to perform the relevant transformation, and the identity template to copy across all other nodes:
Try this XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<!-- Create an output file per order from the previous query -->
<xsl:for-each select="Orders/Order/Products/Product">
<xsl:variable name="InputFile" select="base-uri()"/>
<xsl:variable name="OutputFile" select="Line_Id"/>
<xsl:result-document href="{$OutputFile}.xml" method="xml">
<xsl:apply-templates select="current()"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Name">
<xsl:copy>
<xsl:value-of select="concat(., ' ', ../Product_Variation)" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it in action at http://xsltfiddle.liberty-development.net/gWvjQfn (although I have changed xsl:result-document
to just a normal result-document
element as xsltfiddle will disable the usage of xsl:result-document
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.