0

I want to move the element id to Here My Input xml is

<COLLECTION>
<abc>
<id>1234</id>
</abc>

        <AddedParts NAME="AddedParts" TYPE="Unknown" STATUS="0">
            <Part>
            </Part>
            <Here>
            </Here>
        </AddedParts>
</COLLECTION>

Expected output is

<COLLECTION>
<abc>

</abc>

        <AddedParts NAME="AddedParts" TYPE="Unknown" STATUS="0">
            <Part>
            </Part>
            <Here>
            <id>1234</id>
            </Here>
        </AddedParts>
</COLLECTION>

The xsl i write is

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method= "xml" version= "1.0" encoding= "UTF-8" indent= "yes" />
    <xsl:strip-space elements= "*" />

     <xsl:output omit-xml-declaration="yes"/>

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



        <xsl:template match= "Here" >
        <xsl:copy>
            <xsl:apply-templates select= "node()|@*" />
            <!-- move nodes to here -->
            <xsl:apply-templates select= "../id " mode= "move" />
        </xsl:copy>
    </xsl:template >
< xsl:template match= "id" />

I am unable to achieve the expected output

2
  • this link might help you. Commented Aug 8, 2016 at 9:49
  • 1
    @greenPadawan Thanks for reply but it is not working Commented Aug 8, 2016 at 9:50

1 Answer 1

0

You've got two things wrong with your current XSLT

  1. You are trying to select ../id, but id is not a child of the current parent, but a child of the abc element under the current grand-parent. It should be ../../abc/id.
  2. You are applying a template with a mode of "move" but have no matching template with that mode, meaning XSLT's built-in templates will be used, which would output the text content of id, but not the element itself.

Try this XSLT

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method= "xml" version= "1.0" encoding= "UTF-8" indent= "yes" />
    <xsl:strip-space elements= "*" />

     <xsl:output omit-xml-declaration="yes"/>

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

        <xsl:template match="Here" >
        <xsl:copy>
            <xsl:apply-templates select= "node()|@*" />
            <!-- move nodes to here -->
            <xsl:apply-templates select= "../../abc/id" mode="move" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match= "id" />

    <xsl:template match="id" mode="move">
        <xsl:call-template name="identity" />
    </xsl:template>
</xsl:stylesheet>

If you don't intend to try to transform id in any way, you could simplify it to this...

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method= "xml" version= "1.0" encoding= "UTF-8" indent= "yes" />
    <xsl:strip-space elements= "*" />

     <xsl:output omit-xml-declaration="yes"/>

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

        <xsl:template match="Here" >
        <xsl:copy>
            <xsl:apply-templates select= "node()|@*" />
            <!-- move nodes to here -->
            <xsl:copy-of select= "../../abc/id"  />
        </xsl:copy>
    </xsl:template>

    <xsl:template match= "id" />
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer.
Is is possible to convert kg into lbs using xslt let say <main> <part> <weight>1</weight> <weightunits></weightunits> </part> </main> is my xml and i want to convert <weight> value to LBS and put the output into <weightunits>
Yes, it is possible. You would have a template matching weightunits and in that do the conversion in that. If you can't work it out, it really warrants a whole new question here on SO. It's perfectly fine to add a separate follow-up question. Thanks
Also note, I have amended my answer as I had previously said there was no "Here" element in your XSLT, when then clearly was! Sorry!

Your Answer

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