Convert all node attributes to child nodes

Is there a way to convert the attributes of all nodes to child nodes using XSLT 1.0 ? It should work flawlessly with PHP xsltProcessor . Attributes should be removed (if possible).

Input Example:

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

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> 

Thanks!

+1
source share
1 answer

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 the use of an identity template for element nodes and a template that converts attributes to elements.

+4
source

Source: https://habr.com/ru/post/916055/


All Articles