XSLT 从选择中排除一个特定节点

标签 xslt select apply-templates

我四处寻找这个问题的答案,并尝试了数百种排列,但没有任何效果。

我正在尝试处理一个简单 XML 文档的所有节点,除了第一个 <title>节点。基本上,我试图找到一个 xslt 指令来完成精确的逆运算

<xsl:apply-templates select="/topic/title"/>

这是我的源 XML:

<topic>
    <title>Name of the Document</title>
    <p>Document body</p>
    <topic>
        <title>First Document Subtopic</title>
        <p>Body text for first document subtopic</p>
    </topic>
    <p>Body text continued for document</p>
    <topic>
        <title>Second Document Subtopic</title>
        <p>Body text for document subtopic</p>
        <topic>
            <title>First Document Sub-subtopic</title>
            <p>Body text for first document sub-subtopic</p>
        </topic>
    </topic>
    <p>Body text continued a second time for document</p>
</topic>

这是我的 XSLT(我最初认为应该在调用 apply-templates 时使用):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8" method="xml"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <BOX>
            <TEXTFLOW>
                <xsl:apply-templates select="*[not(/topic/title)]"/>
            </TEXTFLOW>
        </BOX>
    </xsl:template>

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

    <xsl:template match="title">
        <PARA STYLE="Title"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="p">
        <PARA STYLE="BodyText"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="text()">
        <FORMAT>
            <xsl:value-of select="."/>
        </FORMAT>
    </xsl:template>
</xsl:stylesheet>

这就是我想要看到的内容(注意缺少第一个 <title> 事件):

<BOX>
    <TEXTFLOW>
        <PARA STYLE="BodyText"/>
        <FORMAT>Document body</FORMAT>
        <PARA STYLE="Title"/>
        <FORMAT>First Document Subtopic</FORMAT>
        <PARA STYLE="BodyText"/>
        <FORMAT>Body text for first document subtopic</FORMAT>
        <PARA STYLE="BodyText"/>
        <FORMAT>Body text continued for document</FORMAT>
        <PARA STYLE="Title"/>
        <FORMAT>Second Document Subtopic</FORMAT>
        <PARA STYLE="BodyText"/>
        <FORMAT>Body text for document subtopic</FORMAT>
        <PARA STYLE="Title"/>
        <FORMAT>First Document Sub-subtopic</FORMAT>
        <PARA STYLE="BodyText"/>
        <FORMAT>Body text for first document sub-subtopic</FORMAT>
        <PARA STYLE="BodyText"/>
        <FORMAT>Body text continued a second time for document</FORMAT>
    </TEXTFLOW>
</BOX>

我可以使用什么选择表达式来完成此操作?

最佳答案

就这么简单:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="/topic/title"/>

    <xsl:template match="title">
        <PARA STYLE="Title"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="p">
        <PARA STYLE="BodyText"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="text()">
        <FORMAT>
            <xsl:value-of select="."/>
        </FORMAT>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<topic>
    <title>Name of the Document</title>
    <p>Document body</p>
    <topic>
        <title>First Document Subtopic</title>
        <p>Body text for first document subtopic</p>
    </topic>
    <p>Body text continued for document</p>
    <topic>
        <title>Second Document Subtopic</title>
        <p>Body text for document subtopic</p>
        <topic>
            <title>First Document Sub-subtopic</title>
            <p>Body text for first document sub-subtopic</p>
        </topic>
    </topic>
    <p>Body text continued a second time for document</p>
</topic>

产生了想要的正确结果:

<BOX>
   <TEXTFLOW>
      <PARA STYLE="BodyText"/>
      <FORMAT>Document body</FORMAT>
      <PARA STYLE="Title"/>
      <FORMAT>First Document Subtopic</FORMAT>
      <PARA STYLE="BodyText"/>
      <FORMAT>Body text for first document subtopic</FORMAT>
      <PARA STYLE="BodyText"/>
      <FORMAT>Body text continued for document</FORMAT>
      <PARA STYLE="Title"/>
      <FORMAT>Second Document Subtopic</FORMAT>
      <PARA STYLE="BodyText"/>
      <FORMAT>Body text for document subtopic</FORMAT>
      <PARA STYLE="Title"/>
      <FORMAT>First Document Sub-subtopic</FORMAT>
      <PARA STYLE="BodyText"/>
      <FORMAT>Body text for first document sub-subtopic</FORMAT>
      <PARA STYLE="BodyText"/>
      <FORMAT>Body text continued a second time for document</FORMAT>
   </TEXTFLOW>
</BOX>

说明:

为了从处理中排除所需的元素,我们使用与它们匹配且没有正文的模板:

    <xsl:template match="/topic/title"/>
<小时/>

二.更新

不推荐以下替代解决方案,因为它是“拉式”的示例,不像上面的“推式”解决方案那么灵活、优雅和可维护,但是OP坚持认为他需要这样的劣质解决方案:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
      <BOX>
       <TEXTFLOW>
        <xsl:apply-templates
        select="*/*[not(self::title)]"/>
       </TEXTFLOW>
      </BOX>
    </xsl:template>

    <xsl:template match="title">
        <PARA STYLE="Title"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="p">
        <PARA STYLE="BodyText"/>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="text()">
        <FORMAT>
            <xsl:value-of select="."/>
        </FORMAT>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档(上面)时,它会再次生成所需的正确结果

关于XSLT 从选择中排除一个特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014797/

相关文章:

XSLT 应用模板递归性的厄运!

C#将xml属性转换为元素

xslt - 在 Google Search Appliance 中更改结果标题

MySQL 区分两行

sql - SQL 中投影的语法

c - Ubuntu 中 timeval 结构返回的时间是错误的

xml - 如何比较xsl中的日期

xslt - 使用 XSLT 进行位置分组

xml - 将模板应用于 XSLT 中的子字符串

xml - 在同一级别为同一 XML 元素呈现不同的模板