Create elements from a xml element with empty parent

Pseurriff

New Member
I'm trying to create a xml using xslt.This is the xml that is going to be transformed:\[code\] <?xml version="1.0" encoding="utf-8"?> <document> <document_head> <title>This is the title</title> <version>This is the title</version> </document_head> <document_body> <paragraph id="TDKR"> <text> Oh no!, you're not Silvia, you're one of the kung fu creatures on the rampage... Two! </text> <properties> <size>13px</size> <color>#000000</color> </properties> <author>Current user</author> </paragraph> <paragraph id="AAHD"> <properties></properties> </paragraph> </document_body> </document>\[/code\]This is what I need:\[code\] <?xml version="1.0" encoding="UTF-8"?> <document> <document_head> <title>This is the title</title> <version>This is the title</version> </document_head> <document_body> <paragraph> <properties id="TDKR"> <size>13px</size> <color>#000000</color> </properties> </paragraph> <paragraph> <properties id="AAHD"> <size></size> <color></color> </properties> </paragraph> </document_body> </document>\[/code\]This is the XSLT I have:\[code\] <?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" indent="yes" omit-xml-declaration="no" /> <!--START Identity transformation--> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!--END__ Identity transformation--> <!--START Copy of paragraph ID Attribute--> <xsl:template match="paragraph/properties" > <xsl:copy> <xsl:apply-templates select="../@id | @* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="paragraph"> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> <!--END__ Copy of paragraph ID Attribute--> <!--START Filter of undesired elements--> <xsl:template match="paragraph/text" /> <xsl:template match="paragraph/author" /> <!--END__ Filter of undesired elements--> <!--START Creation of empty elements--> <xsl:variable name="empty" select="''"/> <xsl:template match="*[not(node())]"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> <xsl:value-of select="$empty"/> </xsl:copy> </xsl:template> <!--END__ Creation of empty elements--> </xsl:stylesheet>\[/code\]This is what the transformation produces:\[code\] <?xml version="1.0" encoding="UTF-8"?> <document> <document_head> <title>This is the title</title> <version>This is the title</version> </document_head> <document_body> <paragraph> <properties id="TDKR"> <size>13px</size> <color>#000000</color> </properties> </paragraph> <paragraph> <properties/> </paragraph> </document_body> </document>\[/code\]Can anyone tell me what I can do to achieve this?
 
Back
Top