给定一个元素,如何使用 XSLT 确定其类型?

发布于 2024-12-09 14:58:30 字数 310 浏览 0 评论 0原文

我有一个包含以下类型元素的 XML 架构:

<xs:simpleType name="value">
  <xs:union memberTypes="xs:boolean xs:int xs:double xs:string"/>
</xs:simpleType>

示例 XML 片段如下:

<value>42</value>

在 XSLT 转换中,如何确定值的类型,即它是布尔值、整数、双精度值还是字符串?

I have an XML Schema that contains the following type element:

<xs:simpleType name="value">
  <xs:union memberTypes="xs:boolean xs:int xs:double xs:string"/>
</xs:simpleType>

A sample XML fragment would be:

<value>42</value>

In an XSLT transform, how do I determine which type the value has, i.e., is it a boolean, an integer, a double, or a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-12-16 14:58:30

在 XSLT 转换中,如何确定值的类型,
即,它是布尔值、整数、双精度值还是字符串?

如果没有与 XML 文档关联的架构,则答案是类型始终为 xs:string 并且这个问题没有太大意义。

但是,正确的问题是:这与哪些类型兼容(可转换为)?

此转换显示了如何找出这一点。它还说明了 的优雅和强大功能:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()[. castable as xs:integer]">
     <xsl:sequence select="., ' is castable as xs:integer. '"/>
     <xsl:next-match/>
 </xsl:template>

 <xsl:template match="text()[. castable as xs:boolean]">
     <xsl:sequence select="., ' is castable as xs:boolean. '"/>
     <xsl:next-match/>
 </xsl:template>

 <xsl:template match="text()[. castable as xs:string]">
     <xsl:sequence select="., ' is castable as xs:string. '"/>
     <xsl:next-match/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<value>42</value>

想要的、正确的产生结果

42 is castable as xs:string. 42 is castable as xs:integer. 

In an XSLT transform, how do I determine which type the value has,
i.e., is it a boolean, an integer, a double, or a string?

If there isn't a schema associated with the XML document, the answer is that the type is always xs:string and the question isn't too meaningful.

However, the correct question is: With which of those types is this compatible (castable as) ?

This transformation shows how this can be found out. It also illustrates the elegance and power of <xsl:next-match>:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()[. castable as xs:integer]">
     <xsl:sequence select="., ' is castable as xs:integer. '"/>
     <xsl:next-match/>
 </xsl:template>

 <xsl:template match="text()[. castable as xs:boolean]">
     <xsl:sequence select="., ' is castable as xs:boolean. '"/>
     <xsl:next-match/>
 </xsl:template>

 <xsl:template match="text()[. castable as xs:string]">
     <xsl:sequence select="., ' is castable as xs:string. '"/>
     <xsl:next-match/>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XML document:

<value>42</value>

the wanted, correct result is produced:

42 is castable as xs:string. 42 is castable as xs:integer. 
傲娇萝莉攻 2024-12-16 14:58:30

如果您使用架构感知转换,则此值元素的类型将是 xs:int - 实例有效的联合的第一个成员类型。

如果您想测试它是什么类型,请尝试以下操作:

<xsl:choose>
  <xsl:when test=". instance of element(*, xs:int)">int</xsl:when>
  <xsl:when test=". instance of element(*, xs:boolean)">boolean</xsl:when>
  etc
<xsl:choose>

If you are using a schema-aware transform, then this value element will be of type xs:int - the first of the member types of the union against which the instance is valid.

If you want to test which type it is, try something like this:

<xsl:choose>
  <xsl:when test=". instance of element(*, xs:int)">int</xsl:when>
  <xsl:when test=". instance of element(*, xs:boolean)">boolean</xsl:when>
  etc
<xsl:choose>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文