XSLT transform source xml elements into multiple different target xml elements -
the title of post not explain trying do. think example explain best. have source xml document follows:
<x> <a>some data 1</a> <a>some data 2</a> <a>some data 3</a> <a>some not needed data 4</a> <a>some not needed data 5</a> </x>
i need transform <a>
elements (in example let's ones value "some data x") have put 1 section (starting section), since section can hold 2 elements, rest have put section (ending section). in between these 2 sections there other unrelated elements. example:
<starting> <a>some data 1</a> <a>some data 2</a> </starting> <someotherxmlelements/> <somemoreotherxmlelements/> <ending> <a>some data 3</a> </ending>
i have logic in template figures out elements (again in example, ones value "some data x") of whole set need pull. problem when processing in element, there no way me tell elements in element. piece of cake if update variables, unfortunately xslt not allow that. suggestions? output scheme cannot changed way.
this isn't terribly elegant, job. uses xslt version 1.0. tested msxsl.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0" > <xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/> <xsl:template match="/"> <root> <starting> <xsl:for-each select="/x/a[substring(text(), 1, 9)='some data']"> <xsl:if test="position() = 1 or position() = 2"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:if> </xsl:for-each> </starting> <someotherxmlelements/> <somemoreotherxmlelements/> <ending> <xsl:for-each select="/x/a[substring(text(), 1, 9)='some data']"> <xsl:if test="position() > 2"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:if> </xsl:for-each> </ending> </root> </xsl:template> </xsl:stylesheet>
i got output:
<root> <starting> <a>some data 1</a> <a>some data 2</a> </starting> <someotherxmlelements /> <somemoreotherxmlelements /> <ending> <a>some data 3</a> </ending> </root>
Comments
Post a Comment