如何将特定属性转换为元素 [英] How to convert specific attribute into element
本文介绍了如何将特定属性转换为元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将特定的 XML 属性转换为 XML 元素.输入 XML:
I need to convert specific XML attribute into XML element. The input XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<filter query="select" name="some name"/>
我的愿望输出如下:
<?xml version="1.0" encoding="UTF-8"?>
<filter name="some name">
<query>select</query>
</filter>
我正在使用以下 XSLT:
I'm using the following XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/filter/@query">
<xsl:element name="{name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但是,当我将此 XSLT 应用于提供的示例时,名称属性消失了:
However when I apply this XSLT to the provided example the name attribute disappears:
<?xml version="1.0" encoding="UTF-8"?>
<filter>
<query>select</query>
</filter>
如果我更改属性顺序,即在查询"之前放置名称",则一切正常.我试图解决它,但我的 XSLT 知识非常有限.请帮忙.提前致谢.
If I change the attributes ordering, i.e. put 'name' before 'query' everything works perfectly. I try to solve it but my XSLT knowledge is very limited. Please help. Thanks in advance.
推荐答案
这应该会给你所需的输出:
This should give you the required output:
<?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">
<!-- 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 query child element, using the source query attribute value -->
<xsl:element name="query">
<xsl:value-of select="@query"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这篇关于如何将特定属性转换为元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文