简体   繁体   English

XSLT:排除子节点

[英]XSLT: Excluding child nodes

We are using XSLT to transform XML into HTML. 我们正在使用XSLT将XML转换为HTML。 In one portion of this we have a list of elements and we need to extract the string data, minus any XML markup, for use in another location. 在其中的一部分中,我们有一个元素列表,我们需要提取字符串数据,减去任何XML标记,以便在另一个位置使用。

The current XSL is simply: 目前的XSL很简单:

<xsl:variable name="label_Text" select=".//column_header_1"/>
...
<xsl:value-of select="$label_Text"/>


This works fine in most cases, but some of the column_header elements contain certain child nodes that should be excluded. 这在大多数情况下都可以正常工作,但有些column_header元素包含应排除的某些子节点。 Take this XML snippet for example: 以此XML片段为例:

<column_header_1> <column_header_1>
Foo <textstyle_bold>Bar</textstyle_bold> <editor_note>note from an editor</editor_note> Foo <textstyle_bold> Bar </ textstyle_bold> <editor_note>来自编辑器的注释</ editor_note>
</column_header_1> </ column_header_1>

The current XSL returns "Foo Bar note from an editor" but we do not want the editor_note node included in this location. 当前的XSL返回“来自编辑器的Foo Bar note”,但我们不希望editor_note节点包含在此位置。 The desired result from the above snippet would be "Foo Bar". 上述片段的期望结果将是“Foo Bar”。


I feel like I should be able to use something like 我觉得我应该可以使用类似的东西

<xsl:value-of select="$label_Text[not(editor_note)]"/>

but so far I have not been able to get that to work. 但到目前为止,我还没有能够实现这一目标。

Note that the editor_note element should not be excluded from the entire document, it should only be excluded from this specific value, which seems to preclude using an empty template to remove the node. 请注意,不应从整个文档中排除editor_note元素,只应将其从此特定值中排除,这似乎排除了使用空模板删除节点。

Use a modal form of applying templates. 使用模式形式的应用模板。

Within the column header, invoke xsl:apply-templates in a "non-editor-note" mode that suppresses such notes. 在列标题中,以“非编辑器注释”模式调用xsl:apply-templates以抑制此类注释。

<xsl:apply-templates select="." mode="non-editor-note"/>

Then set up some extractors that work only in "non-editor-note" mode: 然后设置一些仅在“非编辑注释”模式下工作的提取器:

  <xsl:template match="editor_note" mode="non-editor-note"/>

  <xsl:template match="*" mode="non-editor-note">
    <xsl:value-of select="."/>
  </xsl:template>

You may have to do some tweaking of the value-of element, but that should do almost all of what you want. 您可能需要对元素的value-of进行一些调整,但这应该可以完成您想要的所有内容。

Edited/corrected version of answer from Bob Dalgleish: Bob Dalgleish的答案的编辑/更正版本:

Use a modal form of applying templates. 使用模式形式的应用模板。

Within the column header, invoke xsl:apply-templates in a "non-editor-note" mode that suppresses such notes. 在列标题中,以“非编辑器注释”模式调用xsl:apply-templates以抑制此类注释。

<xsl:apply-templates select="." mode="non-editor-note"/>

There only needs to be one specific rule in "non-editor-note" mode: “非编辑注释”模式中只需要一个特定的规则:

  <xsl:template match="editor_note" mode="non-editor-note"/>

For all other nodes, the default template rules do exactly what you want: ignore markup and recurse down to the text nodes, then copy the text nodes. 对于所有其他节点,默认模板规则完全按照您的要求执行:忽略标记并递归到文本节点,然后复制文本节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM