[英]How do I transform an XML element name into an attribute value using XSLT?
Given the XML snippet: 鉴于XML片段:
<transactions>
<tran id="1">
<E8>
<datestamp>2012-05-17T15:16:57Z</datestamp>
</E8>
</tran>
</transactions>
How do I transform the element <E8>
into <event type="E8">
using XSLT? 如何使用XSLT将元素
<E8>
转换为<event type="E8">
?
Edit: Expected output: 编辑:预期输出:
<transactions>
<tran id="1">
<event type="E8">
<datestamp>2012-05-17T15:16:57Z</datestamp>
</event>
</tran>
</transactions>
This transformation : 这种转变 :
<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="tran/E8">
<event type="E8">
<xsl:apply-templates/>
</event>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document: 当应用于提供的XML文档时:
<transactions>
<tran id="1">
<E8>
<datestamp>2012-05-17T15:16:57Z</datestamp>
</E8>
</tran>
</transactions>
produces the wanted, correct result: 产生想要的,正确的结果:
<transactions>
<tran id="1">
<event type="E8">
<datestamp>2012-05-17T15:16:57Z</datestamp>
</event>
</tran>
</transactions>
Use: 使用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="tran/*">
<event type="{name()}">
<xsl:value-of select="."/>
</event>
</xsl:template>
</xsl:stylesheet>
Output: 输出:
<transactions>
<tran id="1">
<event type="E8">2012-05-17T15:16:57Z</event>
</tran>
</transactions>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.