识别 XSL 中每个元素的数据类型 [英] Identifying data type for every element inside a XSL

查看:29
本文介绍了识别 XSL 中每个元素的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为这个答案的后续,有没有办法确定 XML 值的数据类型?Java 中的 XSL 转换文档

As a follow up to this answer, is there a way I can figure out if the data type of the XML values? XSL transformation doc in java

例如:

<root>
<test>
  true
</test>
<test1>
  1
</test1>
<test2>
  "abc"
</test2>
</root>

我的想法:

<xsl:template match="/">
    <object>
        <xsl:for-each select="//*[normalize-space(.)]">
                <xsl:choose>        
                    <xsl:when test="contains(., 3)">//  want to check for Integer
                        <Integer name="{name()}">
                            <xsl:value-of select="."/>
                        </Integer>
                    </xsl:when>
                    <xsl:otherwise>
                        //check for string, boolean
                    </xsl:otherwise>
                </xsl:choose>
        </xsl:for-each>
    </object>
</xsl:template>

我想知道如何在 XSL 中将 1、"abc"、true 的数据类型识别为整数、字符串和布尔值?如果有人可以帮助我了解 normalize-space(.) 的工作原理,那将非常有帮助 (:

I want to know how I would be able to identify in XSL the data type of 1,"abc",true as integer,string and boolean? If someone can help me understand how normalize-space(.) works it would be very helpful (:

任何帮助将不胜感激.

推荐答案

你可以使用 castable as 在 XSLT 2.0 中检查元素的值是否可转换为 xs:integerxs:boolean (或任何其他 xs 数据类型).

You could use castable as in XSLT 2.0 to check if the value of the element is castable as either xs:integer or xs:boolean (or any of the other xs datatypes).

示例...

XML 输入

<root>
    <test>
        true
    </test>
    <test1>
        1
    </test1>
    <test2>
        "abc"
    </test2>
</root>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="test|test1|test2">
        <xsl:variable name="type">
            <xsl:choose>
                <xsl:when test="normalize-space() castable as xs:integer">
                    <xsl:text>Integer</xsl:text>
                </xsl:when>
                <xsl:when test="normalize-space() castable as xs:boolean">
                    <xsl:text>Boolean</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>String</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:element name="{$type}">
            <xsl:attribute name="name" select="name()"/>
            <xsl:value-of select="normalize-space()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

XML 输出

<root>
   <Boolean name="test">true</Boolean>
   <Integer name="test1">1</Integer>
   <String name="test2">"abc"</String>
</root>

如果您的 XML 使用架构并且您的处理器支持架构,您还可以使用 实例检查类型.

If your XML is using a schema and your processor is schema aware, you could also use instance of to check the type.

此外,如果您觉得更容易阅读,您可以使用 if 语句代替 xsl:choose...

Also, you could use an if statement instead of the xsl:choose if you find it easier to read...

<xsl:variable name="type" select="
    if (normalize-space() castable as xs:integer) then 
        'Integer' 
    else if (normalize-space() castable as xs:boolean) then 
        'Boolean' 
    else 
        'String'
"/>

这篇关于识别 XSL 中每个元素的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆