使用XSLT更改XML元素名称

时间:2014-04-04 19:42:27

标签: xslt-2.0

我正在尝试更改XML节点名称,但它不允许我这样做。在我的下面的代码中,我有两个模板1.更改节点名称2.为DocumentReference创建父节点。请参阅我的XML和XSLT。

我的XML

  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <DataArea>
    <PurchaseOrder>
        <PurchaseOrderLine>
            <DocumentReference>
                <DocumentID>
                    <ID>23423</ID>
                </DocumentID>
            </DocumentReference>
            <DocumentReference>
                <DocumentID>
                    <ID>23424</ID>
                </DocumentID>
            </DocumentReference>
            <Item>
                <CustomerItemID>
                    <!-- ArtNr -->
                    <ID>444</ID>
                </CustomerItemID>
            </Item>
            <Quantity unitCode="PCE">17.3</Quantity>
        </PurchaseOrderLine>
    </PurchaseOrder>
  </DataArea>

预期结果

  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <DataArea>
    <PurchaseOrder>
        <POL>
            <DocumentReference>
                <DocumentID>
                    <ID>23423</ID>
                </DocumentID>
            </DocumentReference>
            <DocumentReference>
                <DocumentID>
                    <ID>23424</ID>
                </DocumentID>
            </DocumentReference>
            <Item>
                <CustomerItemID>
                    <!-- ArtNr -->
                    <ID>444</ID>
                </CustomerItemID>
            </Item>
            <Quantity unitCode="PCE">17.3</Quantity>
        </POL>
    </PurchaseOrder>
  </DataArea>

我的XSLT

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
   </xsl:template>   

   <xsl:template match="PurchaseOrderLine">
      <POL>
        <xsl:apply-templates />
      </POL>
  </xsl:template>  

  <xsl:template match="PurchaseOrderLine">
        <xsl:copy>
        <Kiran>
            <xsl:apply-templates select="@*|DocumentReference"/>
        </Kiran>
        <xsl:apply-templates select="@*|Item|Quantity"/>
    </xsl:copy>
  </xsl:template>   

  </xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

然后我认为您希望模板看起来像

<xsl:template match="PurchaseOrderLine"> 
  <POL> 
    <xsl:apply-templates select="@*"/>
    <Kiran>
      <xsl:apply-templates select="DocumentReference"/>
    </Kiran>
    <xsl:apply-templates select="node() except DocumentReference" />
  </POL> 
</xsl:template>