如何添加属性“类型"XSLT 转换中元素的默认数据类型 [英] How to add an attribute "type" for default data type of elements in XSLT transformation

查看:30
本文介绍了如何添加属性“类型"XSLT 转换中元素的默认数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

<employee>
     <name>Ragu</name>
     <city>Chennai</city>
     <age>25</age>
</employee>

上述请求 xml 元素的数据类型已在请求架构文件中定义.name 是 string,city 是 string,age 是 int 数据类型.

Data type for above request xml element has been defined in request Schema file. name is string, city is string and age is int data type.

 <employee>
      <name type="string">Ragu</name>
      <city type="string">Chennai</city>
      <age type="int">25</age>
   </employee>

请任何人提供此转换的解决方案.提前致谢

Please anyone give the solution for this transformation. Thanks in Advance

推荐答案

您可以使用 document() 函数来读取你的模式,例如:

You can use document() function to read your schema, e.g.:

输入 XML:

<employee>
    <name>Ragu</name>
    <city>Chennai</city>
    <age>25</age>
</employee>

架构:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="employee">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string" />
                <xs:element name="city" type="xs:string" />
                <xs:element name="age" type="xs:int" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="node()">

        <xsl:variable name="type" select="substring-after(document('schema.xsd')
                              //xs:element[@name = name(current())]/@type, ':')"/>
        <xsl:copy>
            <xsl:if test="$type">
                <xsl:attribute name="type">
                    <xsl:value-of select="$type"/>
                </xsl:attribute>
            </xsl:if>            
            <xsl:apply-templates/>
        </xsl:copy>

    </xsl:template>

</xsl:stylesheet>

生成所需的输出 XML:

Produces desired Output XML:

<employee>
    <name type="string">Ragu</name>
    <city type="string">Chennai</city>
    <age type="int">25</age>
</employee>

这篇关于如何添加属性“类型"XSLT 转换中元素的默认数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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