检查祖先属性值是否为特定值

时间:2015-05-27 19:00:38

标签: xml xslt xpath

我有一种简单的布局,如下所示:

<SubSection filter="filter14, filter13, filter12">

   <SubSection>
     <para> test </para>
   </SubSection>

    <SubSection filter="filter1">
      <Reviewer>John Smith</Reviewer>
    </SubSection>
</SubSection>

我正在使用DocBook配置文件xsl的一部分来提取,在这种情况下,SubSection元素有filter ='filter14',但是,我还想要提取任何具有filter ='filter1'的元素,如果它们是具有filter ='filter14'值的元素的祖先。

我正在使用以下测试,该测试适用于'filter1'值的测试:

<!-- Copy all non-element nodes -->
 <xsl:template match="@*|text()|comment()|processing-instruction()" node="profile">
  <xsl:copy/>
</xsl:template>

 <!-- Profile elements based on input parameters -->
 <xsl:template match="*" mode="profile">

<!-- This is my input for a filter attribute RU -->
<xsl:variable name="filter.content">

<xsl:if test="@filter">
  <xsl:call-template name="cross.compare">
      <!-- <xsl:with-param name="a" select="$profile.filter"/> -->
    <xsl:with-param name="a" select="'filter14'"/>
    <xsl:with-param name="b" select="@filter"/>
  </xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:variable name="filter.ok" select="not(@filter) or 
   $filter.content != '' or @filter = '' or (@filter = 'filter1' and contains(ancestor::SubSection[@filter], 'filter14'))"/>

<xsl:if  test="$filter.ok">
    <xsl:copy>
      <xsl:apply-templates mode="profile" select="@*"/>

      <xsl:apply-templates select="node()" mode="profile"/>
    </xsl:copy>

</xsl:if>
</xsl:template>

cross.compare是:

 <!-- Returns non-empty string if list in $b contains one ore more values from list $a -->
 <xsl:template name="cross.compare">
 <xsl:param name="a"/>
 <xsl:param name="b"/>
 <xsl:param name="sep" select="$profile.separator"/>

 <xsl:variable name="head" select="substring-before($b, $a)"/>

 <xsl:variable name="tail" select="substring-after($b, $a)"/>
 <xsl:if test="contains($b, $a)">1</xsl:if>

 </xsl:template>

似乎这只是找到正确的xpath来检查当前元素过滤器属性='filter1'和祖先SubSection元素过滤器属性是否包含'filter14'。我认为这会奏效,但事实并非如此。

我仍然使用'filter14'过滤器属性值获取所有内容,以及任何没有过滤器属性或过滤器属性值为''的元素,但不具有过滤器属性值为'filter1'的元素。< / p>

有人能建议这里发生了什么吗?

谢谢,

拉​​斯

1 个答案:

答案 0 :(得分:2)

看起来这就是你出错的地方:

contains(ancestor::SubSection[@filter], 'filter14')

这说明&#34;找到具有SubSection属性的第一个@filter祖先,如果它包含&#39; filter14&#39;`,则生成true

你需要的是:

ancestor::SubSection[contains(@filter, 'filter14')]

稍微强大的版本:

ancestor::SubSection[contains(concat('|', normalize-string(@filter), '|'), '|filter14|')]