How do I remove the outermost wrappers using xslt?

2019-08-07 11:36发布

Example xml is:

<a amp="a"><b><c>this is the text</c></b></a>

Needs to be transformed to:

<a amp="a"><c>this is the text</c></a>

标签: xml xslt
2条回答
聊天终结者
2楼-- · 2019-08-07 11:45

Apply the template on <c> and then just use a copy design pattern.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match='c'>
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
查看更多
祖国的老花朵
3楼-- · 2019-08-07 11:52

Solution #1: A slight improvement to smaccoun's solution that would preserve any attributes on the c element (not necessary for example XML):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="c">
        <xsl:copy-of select="." />
    </xsl:template>
</xsl:stylesheet>

Solution #2 Another alternative that leverages the built-in template rules, which apply-templates for all elements and copy all text():

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!--identity template for the c element, it's decendant nodes, 
        and attributes (which will only get applied from c or 
        descendant elements)-->
    <xsl:template match="@*|c//node()|c">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Solution #3: A modified identity transform:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!--identity template, copies all content by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--don't generate content for these matched elements, 
        just apply-templates to it's children-->
    <xsl:template match="a|b">
        <xsl:apply-templates/>
    </xsl:template>     
</xsl:stylesheet>

Solution #4 If you know what you want, just copy it from a match on the root node

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="a/b/c" />
    </xsl:template>
</xsl:stylesheet>

If you want to simply remove the <b> element from your input, then a modified identity transform should be used with a template matching the <b> element that simply applies templates to it's children.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <!--identity template, copies all content by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--don't generate content for the <b>, just apply-templates to it's children-->
    <xsl:template match="b">
        <xsl:apply-templates/>
    </xsl:template>     
</xsl:stylesheet>
查看更多
登录 后发表回答