Xml 通过跳过给定子级中的内容,通过XSL提取文本
我试图提取一个感兴趣的节点的文本(这里是Xml 通过跳过给定子级中的内容,通过XSL提取文本,xml,xslt,xpath,Xml,Xslt,Xpath,我试图提取一个感兴趣的节点的文本(这里是大结构文本),但是在这个节点中有一些我想跳过的子节点(这里是标题,副标题,和代码)。那些“要删除”的节点可以有子节点 样本数据: <root> <big-structured-text> <section> <title>Introduction</title> In this part we describe Austral
大结构文本
),但是在这个节点中有一些我想跳过的子节点(这里是标题
,副标题
,和代码
)。那些“要删除”的节点可以有子节点
样本数据:
<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: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>
在这一部分中,我们将介绍澳大利亚的外交政策。。。。
独立后。。。
外交政策
澳大利亚
您可能希望研究使用
来消除一些无关的空白,但对于混合内容,您必须始终小心,不要删除太多内容。您能在代码中详细说明哪些部分要求复制不属于给定标记的内容吗?@AsTeR我的代码中没有任何明确的内容可以这样做-这是我在第一行链接的默认规则的结果。这些默认规则本质上等同于
和
。使其工作的原因是显式的match=“title | subtitle | code”
模板覆盖了默认的“*”
模板。好的,谢谢您的解释,我会在睡个好觉后再尝试;)好的,这是有效的,您可以解释一下xsl:template和xsl:apply模板是如何工作的,或者指向相关链接吗?我仍然不太明白如何使用它们。@AsTeR那么你肯定应该学习:-)-模板匹配是XSLT真正强大的来源。使用模板,您可以将处理哪些节点以及如何处理每个节点这两个问题分开。定义模板规则,但这是一个有点枯燥的阅读,或者我敢肯定有无数其他在网上(只是避免任何来自W3学校)。
<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>