XSLT transforming name value pairs to its correspo

2019-07-17 08:41发布

问题:

I am new to XSLT I am trying to transform a name value pair to its corresponding XML. This feature is primarily used in case of special extensions to a standard. The file I want to transform is the following. There are no spaces expected in any of the extNames.

<?xml version="1.0" encoding="UTF-8"?>
<extensionItems 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>
</extensionItems>

to the following:

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

回答1:

The following stylesheet produces the desired result:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="extensionItems">
        <ODEventNotificationExtraField>
            <xsl:apply-templates/>
        </ODEventNotificationExtraField>
    </xsl:template>
    <xsl:template match="extensionsItem">
        <xsl:element name="{extName}">
            <xsl:value-of select="extValue"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>


标签: xslt