从一个XML标记中提取价值,然后使用XSLT放入另一个标记中?

时间:2019-04-02 06:34:14

标签: xml xslt split

我有这样的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文件

文件_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>

文件_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文件中留一个空格。

分割后的文件应如下所示(请参见“名称和变体”标记):

文件_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>

文件_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>  

1 个答案:

答案 0 :(得分:3)

不要使用xsl:copy-of,而要使用xsl:apply-templates,然后使用匹配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的使用)。

相关问题