我正在尝试提取一个有趣节点的文本(这里是big-structured-text),但是在这个节点中有一些子节点我想跳过(这里是title、subtitle和code)。那些“移除”节点可以有子节点。
样本数据:
<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>到目前为止,我已经尝试过:
<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,而不是在介绍标题后面的文本
我也试过:
<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>但这是多次重复感兴趣的文本,有时是无趣的文本(每个节点自己迭代一次,然后每个祖先迭代一次)。
发布于 2014-01-29 18:50:33
而不是-每个人都可以使用模板来处理这个问题。将模板应用到元素节点时,默认行为只是递归地将它们应用到其所有子节点(其中包括文本节点以及其他元素),并让文本节点输出文本。因此,您所需要做的就是创建空的模板来压缩您不想要的元素,然后让默认的模板来完成其余的工作。
<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>在您的示例输入上运行时,这将产生
<?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>来消除一些无关的空白,但是对于混合内容,您必须小心不要删除太多内容。
https://stackoverflow.com/questions/21439630
复制相似问题