[英]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.