剥离标签但保留其内容

问题描述 投票:0回答:1

如何使用XSLT 1.0去除标记name(如果存在)保留所有内容?

input.xml中:

<authors>
    <author>
        Author 1
    </author>
    <author>
        <sup>*</sup>Author 2
    </author>
    <author>
        <name>Author 3</name><sup>1</sup>
    </author>
    <author>
        <sup>**</sup><name>Author 4</name><sup>2</sup>
    </author>
</authors>

desired_output.xml:

<authors>
    <author>
        Author 1
    </author>
    <author>
        <sup>*</sup>Author 2
    </author>
    <author>
        Author 3<sup>1</sup>
    </author>
    <author>
        <sup>**</sup>Author 4<sup>2</sup>
    </author>
</authors>
xslt xslt-1.0
1个回答
1
投票

仅用于仅跳过元素名称:

<xsl:template match="name">
    <xsl:apply-templates/>
</xsl:template>

以下是完整的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">

    <xsl:output indent="yes"/>

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


    <xsl:template match="name">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.