Change XML element name using XSLT

ぃ、小莉子 提交于 2019-12-20 06:47:17

问题


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.

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

Expected Result

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

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


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>


来源:https://stackoverflow.com/questions/22871731/change-xml-element-name-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!