首页 > 解决方案 > 如何获取值类型

问题描述

请教我。

有没有办法获取您指定的变量的类型?例如,我正在考虑使用获取类型并将其输出到消息中,或者比较类型以生成如下条件。

或者有可能做到吗?

<!-- example -->
<xsl:message select="[get_type_function]([any_variable_as_string])" />

<!-- message -->
xs:string

此致。

标签: xsltxpath

解决方案


你期望变量的类型是什么,如果是声明<xsl:variable name="var1" as="item()*" select="1"/>,是你想要的类型,然后是声明的item()*或者是它xs:integer还是xs:integer*?在 XSLT 1 和 EXSLT 的上下文中,有http://exslt.org/exsl/functions/object-type/index.html来获取基本类型'string', 'number', 'boolean', 'node-set', 'RTF' or 'external',但是 XSLT/XPath 1 有一个相当简单的类型系统并且没有无论如何声明类型,在 XSLT/XPath 2 或 3 中,类型系统要复杂得多。

在 Saxon 9 EE 或 PE 的上下文中,有http://saxonica.com/html/documentation/functions/saxon/type.html所以对于上面的示例

    <xsl:variable name="var1" as="item()*" select="1"/>
    <xsl:message select="saxon:type($var1)('name')" xmlns:saxon="http://saxon.sf.net/"/>
    <xsl:variable name="var2" as="item()*" select="'foo'"/>
    <xsl:message select="saxon:type($var2)('name')" xmlns:saxon="http://saxon.sf.net/"/>

你得到

integer
string

但我不确定您将如何获得适合异构序列的类型

    <xsl:variable name="var3" as="item()*" select="1, 'foo'"/>
    <xsl:message select="saxon:type($var3)('name')" xmlns:saxon="http://saxon.sf.net/"/>

也简单地给出整数。


推荐阅读