Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

php - Convert all node's attributes into child nodes

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).

Example input :

<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>

The desired result :

<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>

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...