1

我正在创建一个 XSLT,并且我想选择一个特定节点,前提是它的一个子元素的值在一个范围之间。范围将使用 xsl 文件中的参数指定。

XML文件就像

<root>
 <org>
  <name>foo</name>
  <chief>100</chief>
 </org>
 <org parent="foo">
  <name>foo2</name>
  <chief>106</chief>
 </org>
</root>

到目前为止的 XSLT 是

<xsl:param name="fromRange">99</xsl:param>
<xsl:param name="toRange">105</xsl:param>

<xsl:template match="/">
    <xsl:element name="orgo">
        <xsl:apply-templates select="//org[not(@parent)]"/>
    </xsl:element>
</xsl:template>

我想限制处理 < Chief > 节点的值不在范围内的 org 节点

4

2 回答 2

3

我想选择一个特定的节点,只有当它的一个子元素的值在一个范围之间时。范围将使用 xsl 文件中的参数指定。

我还希望节点不应具有parent 属性以及范围的限制

将此表达式用作 的select属性的值<xsl:apply-templates>

org[not(@parent) and chief >= $fromRange and not(chief > $toRange)]

在 XSLT 2.0 中,在匹配模式中包含变量/参数是合法的

因此,可以写:

<xsl:template match=
  "org[@parent or not(chief >= $fromRange ) or chief > $toRange]"/>

从而有效地将所有此类org元素排除在处理之外。

那么与文档节点匹配的模板就是

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

这比 XSLT 1.0 解决方案要好,因为它更“推式”。

于 2010-10-03T23:50:00.050 回答
0
//org[chief &lt; $fromRange and not(@parent)]
    |//org[chief > $toRange and not(@parent)]

fromRange此表达式将排除和指定范围内的所有节点toRange

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="fromRange">99</xsl:param>
  <xsl:param name="toRange">105</xsl:param>

  <xsl:template match="/">
    <xsl:element name="orgo">
      <xsl:apply-templates select="//org[chief &lt; $fromRange and not(@parent)]|//org[chief > $toRange and not(@parent)]"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>
于 2010-10-03T21:18:19.713 回答