Home > Software design >  Insert new hierarchical tag in XML using XSLT
Insert new hierarchical tag in XML using XSLT

Time:12-13

Say I have an XML like this:

<person>
    <name>John</name>
    <age>25</age>
    <gender>male</gender>
</person>

And I want to introduce a new hierarchy like so:

<person>
    <details>
        <name>John</name>
        <age>25</age>
        <gender>male</gender>
    </details>
</person>

How can I do this using XSLT?

I am not sure how to even begin writing the XSLT to do this kind of "re-arrangement".

I can do this in java code, but have a need to use XSLT (if possible).

CodePudding user response:

This is a trivial problem (at least as asked):

XSLT 1.0

<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:template match="/person">
    <xsl:copy>
        <details>
            <xsl:copy-of select="*"/>
        </details>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
  • Related