[英]Pick up value from one XML tag and put in another tag with XSLT?
我有这样的XML
<?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>
我用XSLT这样分割XML。
<?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>
效果很好,我得到了这样的新XML文件
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>
但是我的问题是,当我分割XML时,我也想在标记中拾取值并将其最后放置在标记中,并在分割的XML文件中留一个空格。
拆分后的文件应如下所示(请参见名称和变体标签):
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>
与使用xsl:apply-templates
而不是xsl:copy-of
,使用具有name
匹配的模板来执行相关的转换,并使用标识模板来在所有其他节点之间进行复制:
试试这个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>
可以在http://xsltfiddle.liberty-development.net/gWvjQfn上查看它的运行情况(尽管我将xsl:result-document
更改为一个普通的result-document
元素,因为xsltfiddle将禁用xsl:result-document
的用法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.