使用 XSLT 更改 XML 元素名称 [英] Change XML element name using XSLT

查看:27
本文介绍了使用 XSLT 更改 XML 元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to change XML node name but it doesn't allow me to do so. In my below code I I have two templates 1. Change Node name 2.Create parent node for DocumentReference. Please see my XML and 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>

推荐答案

然后我想你希望模板看起来像

Then I think you want the template to look like

<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>

这篇关于使用 XSLT 更改 XML 元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆