首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过XSL跳过给定孩子的内容来提取文本

通过XSL跳过给定孩子的内容来提取文本
EN

Stack Overflow用户
提问于 2014-01-29 18:33:27
回答 1查看 182关注 0票数 1

我正在尝试提取一个有趣节点的文本(这里是big-structured-text),但是在这个节点中有一些子节点我想跳过(这里是titlesubtitlecode)。那些“移除”节点可以有子节点。

样本数据:

代码语言:javascript
复制
<root>
    <big-structured-text>
        <section>
            <title>Introduction</title>
            In this part we describe Australian foreign policy....
            <subsection>
                <subtitle>Historical context</subtitle>
                After its independence...
                <meta>
                    <keyword>foreign policy</keyword>
                    <keyword>australia</keyword>
                    <code>
                        <value>XXHY-123</value>
                        <label>IRRN</label>
                    </code>
                </meta>
            </subsection>
        </section>
    </big-structured-text>
    <!-- ... -->
    <big-structured-text>
        <!-- ... -->
    </big-structured-text>
</root>

到目前为止,我已经尝试过:

代码语言:javascript
复制
<xsl:for-each
     select="//big-structured-text">
         <text>
             <xsl:value-of select=".//*[not(*)
                 and not(ancestor-or-self::code)
                 and not(ancestor-or-self::subtitle)
                 and not(ancestor-or-self::title)
                 ]" />
         </text>
</xsl:for-each>

但是,这只是使用没有任何子节点的节点,它将接受keyword,而不是在介绍标题后面的文本

我也试过:

代码语言:javascript
复制
<xsl:for-each
     select="//big-structured-text">
         <text>
             <xsl:value-of select=".//*[
                 not(ancestor-or-self::code)
                 and not(ancestor-or-self::subtitle)
                 and not(ancestor-or-self::title)
                 ]" />
         </text>
</xsl:for-each>

但这是多次重复感兴趣的文本,有时是无趣的文本(每个节点自己迭代一次,然后每个祖先迭代一次)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-29 18:50:33

而不是-每个人都可以使用模板来处理这个问题。将模板应用到元素节点时,默认行为只是递归地将它们应用到其所有子节点(其中包括文本节点以及其他元素),并让文本节点输出文本。因此,您所需要做的就是创建空的模板来压缩您不想要的元素,然后让默认的模板来完成其余的工作。

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="/root/big-structured-text" />
    </root>
  </xsl:template>

  <xsl:template match="big-structured-text">
    <text><xsl:apply-templates /></text>
  </xsl:template>

  <!-- empty template means anything inside any of these elements will be
       ignored -->
  <xsl:template match="title | subtitle | code" />
</xsl:stylesheet>

在您的示例输入上运行时,这将产生

代码语言:javascript
复制
<?xml version="1.0"?>
<root><text>


            In this part we describe Australian foreign policy....


                After its independence...

                    foreign policy
                    australia




    </text><text>

    </text></root>

您可能希望研究如何使用<xsl:strip-space>来消除一些无关的空白,但是对于混合内容,您必须小心不要删除太多内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21439630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档