【发布时间】:2012-01-03 13:48:22
【问题描述】:
在我之前的question 中,我询问了如何将特定属性转换为简单 XML 中的元素。现在我有更复杂的输入。 我需要将属性“查询”转换为元素。 复杂输入:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<filter query="select" name="hello" description="world">
<certification>WFA</certification>
<uuid>fd5d9f15-f6d9-4e71-aaf4-024aaaa627f2</uuid>
<parameters>
<parameter type="STRING" name="name" label="name">
<description>Some name</description>
</parameter>
</parameters>
<returned-attributes>
<returned-attribute>id</returned-attribute>
</returned-attributes>
</filter>
我的愿望输出如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<filter name="hello" description="world">
<certification>WFA</certification>
<uuid>fd5d9f15-f6d9-4e71-aaf4-024aaaa627f2</uuid>
<query>select<query/>
<parameters>
<parameter type="STRING" name="name" label="name">
<description>Some name</description>
</parameter>
</parameters>
<returned-attributes>
<returned-attribute>id</returned-attribute>
</returned-attributes>
</filter>
我使用以下 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match the filter element -->
<xsl:template match="filter">
<xsl:choose>
<xsl:when test="/filter/@query">
<!-- output a filter element -->
<xsl:element name="filter">
<!-- add the name attribute, using the source name attribute value -->
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<!-- add the description attribute (if found), using the source name attribute value -->
<xsl:choose>
<xsl:when test="/filter/@description">
<xsl:attribute name="description">
<xsl:value-of select="@description"/>
</xsl:attribute>
</xsl:when>
</xsl:choose>
<!-- add the query as child element, using the source query attribute value -->
<xsl:element name="query">
<xsl:value-of select="@query"/>
</xsl:element>
<!-- add all common elements -->
<xsl:element name="certification">
<xsl:value-of select="certification"/>
</xsl:element>
<xsl:element name="uuid">
<xsl:value-of select="uuid"/>
</xsl:element>
<!-- copy parameters -->
<xsl:copy>
<xsl:apply-templates select="/filter/parameters"/>
</xsl:copy>
<!-- copy attributes -->
<xsl:copy>
<xsl:apply-templates select="/filter/returned-attributes"/>
</xsl:copy>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
转换有效,但在我看来很复杂。 请注意,我使用 if/else 逻辑,因为我的输入可以包含“旧”(未转换)和 “新”(转换后的)XML 文件。
请指教。提前致谢。
【问题讨论】: