将所有节点的属性转换为子节点 [英] Convert all node's attributes into child nodes

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

问题描述

是否可以使用XSLT 1.0将所有节点的属性转换为子节点? 它必须在PHP的xsltProcessor上完美运行.必须删除属性(如果可能).

is there a way to convert all nodes' attributes into child Nodes using XSLT 1.0 ? It must run flawlessly with PHP's xsltProcessor. The attributes must be removed (if possible).

示例输入:

<root aaa="111" bbb="222" ccc="333">
    <bob ddd="444" />

    <data eee="555">
        <steve>bar1</steve>
        <john>bar2</john>
        <peter fff="666">bar3</peter>
    </data>

    <greg ggg="777" />
</root>

所需结果:

<root>
    <aaa>111</aaa>
    <bbb>222</bbb>
    <ccc>333</ccc>
    <bob>
        <ddd>444</ddd>
    </bob>
    <data>
        <eee>555</eee>
        <steve>bar1</steve>
        <john>bar2</john>
        <peter>
            <fff>666</fff>
            bar3
        </peter>
    </data>
    <greg>
        <ggg>777</ggg>
    </greg>
</root>

谢谢!

推荐答案

使用Saxon6.5在Oxygen/XML上进行了测试:

Tested on Oxygen/XML using Saxon6.5:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:element name="{name()}"><xsl:value-of select="."/></xsl:element>
  </xsl:template>

</xsl:stylesheet>

这基于对元素节点使用身份模板和将属性转换为元素的模板.

This is based on using an identity template for element nodes and a template that converts attributes to elements.

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

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