简体   繁体   中英

XSLT remove nodes within nodes of the same name

I've got an XML document.

It's got loads of para nodes inside other para nodes.

I want to remove the ones inside and keep the inner text joining it to the other para nodes inner text.

Example XML

<document>
<head>
<front>The front page</front>
</head>
<h1>
<para id="1234">This is the inner text <para> it needs joining together</para> maybe with other text</para>
</h1>
</document>

Desired output

<document>
<head>
<front>The front page</front>
</head>
<h1>
<para id='1234'>This is the inner text it needs joining together maybe with other text</para>
</h1>
</document>

This is rather trivial:

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:strip-space elements="*"/>

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

<xsl:template match="para/para">
    <xsl:apply-templates/>
</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