将属性值转换为元素 [英] Convert attribute value into element

查看:18
本文介绍了将属性值转换为元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换此 xml:

I'm trying to transform this xml:

<tokens>
 <token cle="a">
  <token cle="b">nomX</token>
  <token cle="c">prenomX</token>
  <token cle="d">villeX</token>
 </token>
 <token cle="a">
  <token cle="b">nomY</token>
  <token cle="c">prenomY</token>
  <token cle="d">villeY</token>
 </token>
 <token cle="e">nomZ</token>
</tokens>

进入这个xml:

<tokens>
 <a>
  <b>nomX</b>
  <c>prenomX</c>
  <d>villeX</d>
 </a>
 <a>
  <b>nomY</b>
  <c>prenomY</c>
  <d>villeY</d>
 </a>
 <e>nomZ</e>
</tokens>

因此将属性值转换为元素,但我需要保留整个结构和深度.

so convert the attribute value into an element , but i need to keep the whole structure and deph.

我已经尝试过使用 XSL,但我还没有成功.如果有人有想法,将不胜感激.

I've tried using XSL, but i didn't succeed yet. If anyone has an idea, it would be greatly appreciated.

谢谢.

推荐答案

所以 xslt 是我认为的正确方式:

so xslt is the right way I think:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="token">
        <xsl:element name="{@cle}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

这篇关于将属性值转换为元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆