xslt - Wrapping and moving child nodes -
could please me transforming following snippet?
<root> <topic> <!-- first topic --> <p>content1</p> <topic> <!-- second topic --> <p>content2</p> </topic> <topic> <!-- third topic --> <p>content3</p> </topic> </topic> </root>
i need cut second topic , third topic off first topic, wrap them in new/empty topic , attach new topic children root node.
<root> <topic> <!-- first topic --> <p>content1</p> </topic> <topic> <!-- new topic --> <topic> <!-- second topic --> <p>content2</p> </topic> <topic> <!-- third topic --> <p>content3</p> </topic> </topic> </root>
maybe there simple solution. tried prefix second/suffix third topic tag (looks dirty solution me), fail moving them.
<xsl:if test="position() = 1"> <![cdata[ <topic> ]]> ... </xsl:if> <xsl:if test="position() = last()"> ... <![cdata[ </topic> ]]> </xsl:if>
update 1 more complex , detailed example:
source - before transformation
<?xml version="1.0" encoding="utf-8"?> <root> <!-- root has title , multiple child topics. no other child elements. --> <title>root</title> <topic id="topic_1"> <!-- allowed child elements of 'topic' listed in dita spec.: http://bit.ly/1ruybdq --> <title>first topic - first level</title> </topic> <topic id="topic_2"> <title>second topic - first level</title> <!-- main problem. topic must not contain child topics , other child elements after transformation. if topic has child topic , other child elements, topics have extracted. --> <topic id="topic_3"> <title>third topic - second level</title> </topic> <topic id="topic_4"> <!-- number of topics not limited. --> <title>fourth topic - second level</title> <topic id="topic_5"> <!-- third level topics have moved second hierarchy level. no topic may reside on third level after transformation. --> <title>fifth topic - third level</title> </topic> </topic> </topic> </root>
result - after transformation
<?xml version="1.0" encoding="utf-8"?> <root> <title>root</title> <topic id="topic_1"> <title>first topic - first level</title> </topic> <topic id="topic_2"> <title>second topic - first level</title> </topic> <!-- third , fourth topic have been moved extracted second topic. both (could number) have been wrapped dummy 'topic' element. --> <topic> <!-- second level topics have been wrapped "dummy" topic element. --> <topic id="topic_3"> <title>fourth topic - second level</title> </topic> <topic id="topic_4"> <title>fifth topic - second level</title> </topic> <topic id="topic_5"> <!-- third level topic has been moved second hierarchy level. --> <title>sixth topic - third level</title> </topic> </topic> </root>
maybe there simple solution.
maybe there is, it's difficult see what's given here , what's example. simple solution work you?
xslt 1.0
<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:strip-space elements="*"/> <xsl:template match="/root"> <xsl:copy> <topic> <xsl:copy-of select="topic/p"/> </topic> <topic> <xsl:copy-of select="topic/topic"/> </topic> </xsl:copy> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment