XSLT 函数 Return 类型

XSLT Function Return Type

最初:**如何将 XPath 查询应用于类型为 element() 的 XML 变量* **

我希望将 XPath 查询应用于传递给 XSLT 2.0 中函数的变量。

Saxon returns 这个错误:

Type error at char 6 in xsl:value-of/@select on line 13 column 50 of Whosebug_test.xslt:
  XTTE0780: Required item type of result of call to f:test is element(); supplied value has item type text()

这个程序的框架被简化了,但在其开发结束时,它意味着将一个元素树传递给多个 XSLT 函数。每个函数都将提取某些统计数据并从树中创建报告。

当我说应用 XPath 查询时,我的意思是我希望查询考虑变量中的基本元素......如果你愿意......就好像我可以写 {count(doc("My XSLT tree/element variable")/a[1])}。

使用 Saxon HE 9.7.0.5.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="f:f">
  <xsl:template match="/root">
    <xsl:variable name="first" as="element()*">
      <xsl:copy-of select="(./a[1])" />
    </xsl:variable>
    <html>
      <xsl:copy-of select="f:test($first)" />
    </html>
  </xsl:template>
  <xsl:function name="f:test" as="element()*">
    <xsl:param name="frstElem" as="element()*" />
    <xsl:value-of select="count($frstElem/a)" />
    <!-- or any XPath expression -->
  </xsl:function>
</xsl:stylesheet>

一些示例数据

<root>
    <a>
        <b>
            <c>hi</c>
        </b>
    </a>
    <a>
        <b>
            <c>hi</c>
        </b>
    </a>
</root>

可能相关的问题:How to apply xpath in xsl:param on xml passed as input to xml

你所做的是完全正确的,除了你已经将一个 a 元素传递给函数,并且函数正在寻找这个元素的 a 子元素,以及你的示例data 这将 return 一个空序列。

如果你想要 f:test() 到 return 序列中 a 元素的数量即 $frstElem 的值,你可以使用类似

<xsl:value-of select="count($frstElem/self::a)" />

而不是使用(隐含的)child:: 轴。