如何将特定属性转换为复杂 XML 中的元素 [英] How to convert specific attribute into element in complex XML
本文介绍了如何将特定属性转换为复杂 XML 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我之前的问题中,我询问了如何转换特定的属性到简单 XML 中的元素.现在我有更复杂的输入.我需要将属性 'query' 转换为一个元素.复杂输入:
In my previous question I asked how to convert specific attribute to element in simple XML. Now I have more complex input. I need to transform the attribute 'query' into an element. Complex input:
<?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:
I use the following 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 文件.
The transformation works, but it seems to me to complicated. Note, that I use if/else logic because my input can contain "old" (not transformed) and "new" (transformed) XML files.
请指教.提前致谢.
推荐答案
这里有一个简短的解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="parameters">
<xsl:apply-templates select="/filter/@query" mode="build"/>
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/filter/@query"/>
<xsl:template match="/filter/@query" mode="build">
<query><xsl:value-of select="."/></query>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<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>
产生想要的、正确的结果:
<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>
这篇关于如何将特定属性转换为复杂 XML 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文