如何使用 xslt 删除最外面的包装器? [英] How do I remove the outermost wrappers using xslt?

查看:21
本文介绍了如何使用 xslt 删除最外面的包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例 xml 是:

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

需要转化为:

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

推荐答案

解决方案 #1:smaccoun 的解决方案略有改进,可以保留c 元素(对于 XML 来说不是必需的):

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>

解决方案 #2 另一种利用 内置模板规则,为所有元素应用模板并复制所有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>

解决方案 #3: 修改后的身份转换:

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>

解决方案 #4 如果您知道自己想要什么,只需从根节点上的匹配项中复制它

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>

<小时>

如果您只想从输入中删除 <b> 元素,则应将修改后的身份转换与与 <b> 匹配的模板一起使用简单地将模板应用于其子元素的元素.


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>

这篇关于如何使用 xslt 删除最外面的包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆