尝试匹配模板时出现混乱错误

时间:2014-06-04 14:33:48

标签: xslt

我有以下XML代码行。

<entry colname="col3" align="left" valign="top"><para>grandchild, cousin, <content-style font-style="italic">etc</content-style>., shall be described as “lawful” and “one of the next-of-kin” or “only next-of-kin”.</para></entry>

以下XSL

<xsl:template match="entry" name="entry">
  <xsl:choose>
    <xsl:when test="./@namest">
      <xsl:variable name="namest" select="@namest" />
      <xsl:variable name="nameend" select="@nameend" />
      <xsl:variable name="namestPos" select="count(ancestor::tgroup/colspec[@colname=$namest]/preceding-sibling::colspec)" />
      <xsl:variable name="nameendPos" select="count(ancestor::tgroup/colspec[@colname=$nameend]/preceding-sibling::colspec)" />
      <td colspan="{$nameendPos - $namestPos + 1}" align="{@align}">
        <xsl:apply-templates select="child::node()[not(self::page)]" />
      </td>
    </xsl:when>
    <xsl:otherwise>
      <td>
        <xsl:if test="./@morerows">
          <xsl:attribute name="rowspan">
            <xsl:value-of select="number(./@morerows)+1" />
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="./@align">
          <xsl:attribute name="align">
            <xsl:value-of select="@align" />
          </xsl:attribute>
        </xsl:if>
        <xsl:if test="./@valign">
          <xsl:attribute name="valign">
            <xsl:value-of select="@valign" />
          </xsl:attribute>
        </xsl:if>
        <xsl:for-each select="para">
          <div class="para">
            <xsl:choose>
              <xsl:when test="../@colname='col3' and contains(./text(),'.')">
                <xsl:variable name="strl">
                  <xsl:value-of select="fn:string-length(fn:substring-before(.,'.'))" />
                </xsl:variable>
                <xsl:choose>
                  <xsl:when test="$strl &lt; '6'">
                    <a href="{concat('er:#SCP_ORD_',//chapter/@num,'/','P',translate(./text(),'.','-'))}">
                      <xsl:value-of select="./text()" />
                    </a>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:apply-templates />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates />
              </xsl:otherwise>
            </xsl:choose>
          </div>
        </xsl:for-each>
      </td>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

当我在上面提到的行(示例中给出的XML)中运行我的XML时,它给我一个错误。错误如下。

Wrong occurrence to match required sequence type -   Details: -     XPTY0004: The supplied sequence ('2' item(s)) has the wrong occurrence to match the sequence type xs:string ('zero or one')

这里我实际想要实现的是,我有另一个表(基本上是TOC),其中需要一些链接,下面是这样的样本条目。

<entry colname="col3" align="right" valign="top"><para>A.1</para></entry>
<entry colname="col3" align="right" valign="top"><para>A.2</para></entry>

此处我正在搜索colname是否为col3且其中是否有.,并且上述两种情况正在通过并且已成功关联,在顶部提到的情况下抛出错误,有人可以建议一些更好的方法来区分这两种情况,我使用XSLT 2.0。

由于

1 个答案:

答案 0 :(得分:1)

问题是

contains(./text(),'.')

./text()不是“当前元素的文本”,而是当前元素的所有文本节点 children 序列。在

的情况下
<para>grandchild, cousin, <content-style font-style="italic">etc</content-style>., shall be described as “lawful” and “one of the next-of-kin” or “only next-of-kin”.</para>

两个这样的节点,其中一个包含<para><content-style>标签之间的所有内容(“孙子,堂兄”,包括尾随空格),另一个包含所有内容在</content-style></para>之间。但是contains函数期望它的第一个参数是单个字符串,而不是两个节点的序列。

您可能只需要测试./text()

,而不是测试.
contains(., '.')

被解释为整个para元素的字符串值,即由所有后代文本节点的串联组成的单个字符串(所以应该描述整个文本“孙子,堂兄等”) ...“)。

相关问题