简体   繁体   中英

Insert new hierarchical tag in XML using XSLT

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM