xslt将xml转换为相应的名称值对

时间:2011-12-14 16:14:55

标签: xslt

我是XSLT的新手我试图将名称值对转换为相应的XML。此功能主要用于标准的特殊扩展。我想要转换的文件如下。

<ODEventNotificationExtraField>
<callCode>1</callCode>
<callbackType>All </callbackType>
<callbackEmail>me@mine.org </callbackEmail>
</ODEventNotificationExtraField>

以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<extensionList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="ExtensionItems.xsd">
<extensionsItem>
<extName> callCode</extName>
<extValue>1</extValue>
<extType>integer</extType>
</extensionsItem>
<extensionsItem>
<extName>callbackType</extName>
<extValue>All</extValue>
<extType>string</extType>
</extensionsItem>
<extensionsItem>
<extName>callbackEmail</extName>
<extValue>me@mine.org</extValue>
<extType>string</extType>
</extensionsItem>
</extensionsList>

基于字段名称,例如CallCode我会知道它是整数类型(它需要在xslt中硬编码)     传入数据的xmlschema定义为

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="CallbackType">
<xs:restriction base="xs:string">
<xs:enumeration value="Restoration"/>
<xs:enumeration value="Estimated"/>
<xs:enumeration value="All"/>
<xs:enumeration value="None"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="ODEventNotificationExtraField">
<xs:complexType>
<xs:sequence>
<xs:element name="callCode" type="xs:integer" minOccurs="0"/>
<xs:element name="callbackType" type="CallbackType" minOccurs="0"/>
<xs:element name="callbackEmail" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:1)

以下样式表产生预期结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <!-- element list -->
    <xs:element name="callCode" type="xs:integer"/>
    <xs:element name="callbackType" type="xs:string"/>
    <xs:element name="callbackEmail" type="xs:string"/>
    <xsl:template match="/">
        <extensionItems xs:noNamespaceSchemaLocation="ExtensionItems.xsd">
            <extensionsList>
                <xsl:apply-templates select="/ODEventNotificationExtraField/*"/>
            </extensionsList>
        </extensionItems>
    </xsl:template>
    <xsl:template match="*">
        <extensionsItem>
            <extName><xsl:value-of select="local-name()"/></extName>
            <extValue><xsl:value-of select="."/></extValue>
            <extType>
                <xsl:value-of 
                    select="substring-after(document('')/*/xs:element[
                        @name=current()/local-name()]/@type, ':')"/>
                </extType>
        </extensionsItem>
    </xsl:template>
</xsl:stylesheet>

注意元素类型的硬编码并使用document来检索它们。

输出:

<extensionItems xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
                xs:noNamespaceSchemaLocation="ExtensionItems.xsd">
   <extensionsList>
      <extensionsItem>
         <extName>callCode</extName>
         <extValue>1</extValue>
         <extType>integer</extType>
      </extensionsItem>
      <extensionsItem>
         <extName>callbackType</extName>
         <extValue>All</extValue>
         <extType>string</extType>
      </extensionsItem>
      <extensionsItem>
         <extName>callbackEmail</extName>
         <extValue>me@mine.org</extValue>
         <extType>string</extType>
      </extensionsItem>
   </extensionsList>
</extensionItems>