1

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>
4
  • You can't have two templates with match="PurchaseOrderLine". And you can't create attributes once you have created a child node. Commented Apr 4, 2014 at 19:54
  • How can perform both the task in order to achieve goal? Any suggestion Commented Apr 4, 2014 at 20:07
  • In one template you have a literal result element Kiran which I do not see anywhere in your wanted result so it is hard to tell what you want to achieve. Please clarify. Commented Apr 4, 2014 at 20:19
  • I have two objectives 1. Rename PurchaseOrderLine to POL 2. Add new parent tag Kiran only for DocumentReference. Commented Apr 4, 2014 at 20:27

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.