xsl:尝试在xsl:when / following-sibling语句中使用“ not”

时间:2018-06-26 18:58:52

标签: xslt

我正在尝试查找标题标签(head1)后没有正文标签的情况。

这是我要搜索的代码段,“ head1”后跟“ body”:

<title role="head1">Third-Party Notices and/or Licenses</title>
<para role="body">Required notices for open source software products or components used by Cloud are identified in the following table along with the applicable licensing information.</para>

这是我正在尝试的代码(空的head1代码可以正常工作):

<xsl:template match="w:p[w:pPr/w:pStyle/@w:val='ahead1']">
    <xsl:variable name="elementValue">
        <xsl:apply-templates mode="title.text.only" />
    </xsl:variable>
    <xsl:choose>
        <xsl:when test="$elementValue = ''">
            <xsl:message>ERROR: Encountered an head 1 paragraph with no text content.</xsl:message>
            <xsl:call-template name="revealDocPosition"/>
        </xsl:when>
        <!-- Code to check to see that a head1 is directly followed by a body tag -->
        <xsl:when test="[$elementValue]and not[following-sibling::*[1][self::atgbody]]"> 
        <!-- <xsl:when test="not[$elementValue][following-sibling::*[1][self::atgbody]]"> -->
            <xsl:message>ERROR: Encountered an atg head 1 paragraph that was not follwed by an atgbody.</xsl:message>
            <xsl:call-template name="revealDocPosition"/>
        </xsl:when>

它在我的构建中失败,因为它不喜欢语法:

 [java] Error at xsl:when on line 30 column 84 of headings.xsl:
 [java]   XPST0003: XPath syntax error at char 0 on line 30 in {[$}:
 [java]     Unexpected token "[" in path expression
 [java] Failed to compile stylesheet. 1 error detected.

我尝试了不同的语法,但它要么失败,要么运行,但找不到错误。 想法?

2 个答案:

答案 0 :(得分:1)

方括号是predicate,用于过滤在应用谓词过滤器时未评估为true的项目。

表达式[$elementValue]无需测试和过滤。

假设您要测试$elementValue是否“真实”且具有内容,则需要将其移至谓词之外,然后仅测试变量。

表达式not[following-sibling::*[1][self::atgbody]]正在测试是否有一个名为not的子元素,并应用一个谓词过滤器,因为您没有任何not元素,所以它永远不会匹配-因此测试永远不会是真的。您需要将[]更改为()才能调用not()函数。

<xsl:when test="$elementValue and not(following-sibling::*[1][self::atgbody])"> 

答案 1 :(得分:0)

test="[$elementValue]and not[following-sibling::*[1][self::atgbody]]"

您似乎在逐步完善自己。猜测语法并不会帮助您。

我不知道变量$elementValue中包含什么,因为您没有充分显示代码。您的代码引用了一个atgBody元素,该元素未出现在您的源文档片段中,并且匹配模式还引用了不在您的源代码片段中的节点。这意味着很难看到您的代码试图达到什么目的,从而很难准确地告诉您错误的地方。

语法错误是[$elementValue]不是合法表达式。不知道您的想法意味着什么,就不可能告诉您如何纠正它。