Building Tree Structure using XSLT with inclusion of counters -
i have below xml , need need convert expected output xml using xslt have acheived
in input xml have referenced topmost node has subnodes follows:
<?xml version="1.0"?> <referenced> <name>poctree/poctree.services:getreferencesforservices</name> <lock_status>3</lock_status> <type> <svc_type>flow</svc_type> <svc_subtype>default</svc_subtype> </type> <path/> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>reference</status> <reference> <name>wmroot/wm.server.ns.dependency:getreferenced</name> <lock_status>2</lock_status> <type> <svc_type>java</svc_type> <svc_subtype>unknown</svc_subtype> </type> <path>/flow path;1.0/invoke;0</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>resolved</status> </reference> <reference> <name>wmpublic/pub.xml:documenttoxmlstring</name> <lock_status>2</lock_status> <type> <svc_type>java</svc_type> <svc_subtype>unknown</svc_subtype> </type> <path>/flow path;1.0/invoke;1</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>resolved</status> </reference> <reference> <name>poctree/poctree.services:jkl</name> <lock_status>3</lock_status> <type> <svc_type>xsltservice</svc_type> <svc_subtype>unknown</svc_subtype> </type> <path>/flow path;1.0/invoke;2</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>resolved</status> </reference> <reference> <name>wmpublic/pub.xml:xmlstringtoxmlnode</name> <lock_status>2</lock_status> <type> <svc_type>java</svc_type> <svc_subtype>unknown</svc_subtype> </type> <path>/flow path;1.0/invoke;3</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>resolved</status> </reference> <reference> <name>wmpublic/pub.xml:xmlnodetodocument</name> <lock_status>2</lock_status> <type> <svc_type>java</svc_type> <svc_subtype>unknown</svc_subtype> </type> <path>/flow path;1.0/invoke;4</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>resolved</status> </reference> <reference> <name>poctree/poctree.services:ghi</name> <lock_status>4</lock_status> <type> <svc_type>xsltservice</svc_type> <svc_subtype>unknown</svc_subtype> </type> <path>/flow path;1.0/invoke;5</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>resolved</status> </reference> <reference> <name>wxpackagetree/wxpackagetree.doc:nodeinfo</name> <lock_status>2</lock_status> <type> <type_name>record</type_name> </type> <path>/service path;1.0/signature;2;subpath;"/nodes;4;0;wxpackagetree.doc:nodeinfo"</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>reference</status> <reference> <name>wxpackagetree/wxpackagetree.doc:nodeinfo</name> <lock_status>2</lock_status> <type> <type_name>record</type_name> </type> <path>/children;4;1;wxpackagetree.doc:nodeinfo</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>recursive</status> </reference> <reference> <name>poctree/poctree.docs:nodeinfo</name> <lock_status>4</lock_status> <type> <type_name>record</type_name> </type> <path>/flow path;1.0/map;6/mapcopy;1/to_field;-2;subpath;"/nodeinfo;4;0;poctree.docs:nodeinfo"</path> <path>/flow path;1.0/map;6/mapcopy;2/to_field;-2;subpath;"/nodeinfo;4;0;poctree.docs:nodeinfo"</path> <path>/flow path;1.0/map;6/mapcopy;3/to_field;-2;subpath;"/nodeinfo;4;0;poctree.docs:nodeinfo"</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>reference</status> <reference> <name>poctree/poctree.docs:nodeinfo</name> <lock_status>4</lock_status> <type> <type_name>record</type_name> </type> <path>/children;4;1;poctree.docs:nodeinfo</path> <ispub>false</ispub> <isnotification>false</isnotification> <isflowservicedisabled>false</isflowservicedisabled> <status>recursive</status> </reference> </reference> </reference> </referenced>
expected output xml have obtained:
<?xml version="1.0" encoding="utf-8"?><nodeinfo> <name>poctree/poctree.services:getreferencesforservices</name> <type>flow</type> <ispub>false</ispub> <children> <name>wmroot/wm.server.ns.dependency:getreferenced</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>wmpublic/pub.xml:documenttoxmlstring</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>psutilities2_3/psutilities.file:writetofile</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>poctree/poctree.services:jkl</name> <type>xsltservice</type> <ispub>false</ispub> </children> <children> <name>wmpublic/pub.xml:xmlstringtoxmlnode</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>wmpublic/pub.xml:xmlnodetodocument</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>poctree/poctree.services:ghi</name> <type>xsltservice</type> <ispub>false</ispub> </children> <children> <name>poctree/poctree.services:recursiveloop</name> <type>flow</type> <ispub>false</ispub> <children> <name>wmpublic/pub.list:appendtodocumentlist</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>wmpublic/pub.xml:documenttoxmlstring</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>poctree/poctree.services:ghi</name> <type>xsltservice</type> <ispub>false</ispub> </children> <children> <name>wmpublic/pub.list:appendtodocumentlist</name> <type>java</type> <ispub>false</ispub> </children> <children> <name>poctree/poctree.services:recursiveloop</name> <type>flow</type> <ispub>false</ispub> </children> <children> <name>poctree/poctree.services:recursiveloop</name> <type>flow</type> <ispub>false</ispub> </children> </children> </nodeinfo>
including xslt written achieve above output xml structure
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="referenced"> <nodeinfo> <xsl:apply-templates /> </nodeinfo> </xsl:template> <xsl:template match="reference"> <children> <xsl:apply-templates /> </children> </xsl:template> <xsl:template match="type[svc_type]"> <type> <xsl:value-of select="svc_type" /> </type> </xsl:template> <xsl:template match="type[type_name]"> <type> <xsl:value-of select="type_name" /> </type> </xsl:template> <xsl:template match="type[trigger_type]"> <type> <xsl:value-of select="trigger_type" /> </type> </xsl:template> <xsl:template match="reference[type/svc_type[not(contains(., 'flow') or contains(., 'java') or contains(., 'xsltservice') or contains(., 'spec'))]]" /> <xsl:template match="reference[type/type_name[not(contains(., 'connectiondata') or contains(., 'webservicedescriptor'))]]" priority="2" /> <xsl:template match="reference[type/trigger_type[not(contains(., 'jms-trigger') or contains(., 'broker-trigger'))]]" priority="2" /> <xsl:template match="lock_status" /> <xsl:template match="path" /> <xsl:template match="isnotification" /> <xsl:template match="isflowservicedisabled" /> <xsl:template match="status" /> </xsl:stylesheet>
now have requirement every node , subnode should have 2 more additional tags called <rowid>
, <parentnodeid>
.
hence topmost node in output xml <nodeinfo>
have tags <rowid>1</rowid>
, <parentnodeid>0<parentnodeid>
, , subnodes of <children>
again should have these tags called <parentid>1<parentid>
, <rowid>2<rowid>
, on representing hierarchy parent - child relationship . <parentid>
topmost node starting '0' , start 1 , increment 1 traverses down recursive document.
e.g. xml looks looks below
<referenced> - - <reference> - - </reference> <reference> - - </reference> <reference> - - <reference> - - </reference> <reference> - - </reference> </reference> <reference> - - <reference> - - </reference> <reference> - - <reference> - - </reference> </reference> </reference> </referenced>
after xslt applying , should appear
<nodeinfo> <rowid>1</rowid> <parentnodeid>0</parentnodeid> <childen> - - <rowid>2</rowid> <parentnodeid>1</parentnodeid> </childen> <childen> - - <rowid>3</rowid> <parentnodeid>1</parentnodeid> </childen> <childen> - - <rowid>4</rowid> <parentnodeid>1</parentnodeid> <childen> - - <rowid>5</rowid> <parentnodeid>4</parentnodeid> </childen> <childen> - - <rowid>6</rowid> <parentnodeid>4</parentnodeid> </childen> </childen> <childen> - - <rowid>7</rowid> <parentnodeid>1</parentnodeid> <childen> - - <rowid>8</rowid> <parentnodeid>7</parentnodeid> </childen> <childen> - - <rowid>9</rowid> <parentnodeid>7</parentnodeid> <childen> - - <rowid>10</rowid> <parentnodeid>9</parentnodeid> </childen> </childen> </childen> </nodeinfo>
or else expected output after traversing through input xml
<nodeinfo> - - <rowid>1</rowid> <parentnodeid>0</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>2</rowid> <parentnodeid>1</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>3</rowid> <parentnodeid>1</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>4</rowid> <parentnodeid>1</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>5</rowid> <parentnodeid>4</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>6</rowid> <parentnodeid>4</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>7</rowid> <parentnodeid>1</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>8</rowid> <parentnodeid>7</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>9</rowid> <parentnodeid>7</parentnodeid> </nodeinfo> <nodeinfo> - - <rowid>10</rowid> <parentnodeid>9</parentnodeid> </nodeinfo>
appreciate on topic .
after valuable inputs , , came xslt - let me know if optimized way of doing ?
<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="x"/> <xsl:template match="/"> <root> <xsl:apply-templates select="referenced"/> </root> </xsl:template> <xsl:template match="referenced | reference"> <nodeinfo> <xsl:copy-of select="name"/> <rowid> <xsl:value-of select="generate-id()"/> </rowid> <parentid> <xsl:value-of select="generate-id(..)"/> </parentid> <xsl:apply-templates select="type"/> </nodeinfo> <xsl:apply-templates select="reference"/> </xsl:template> <xsl:template match="type[svc_type]"> <type> <xsl:value-of select="svc_type" /> </type> </xsl:template> <xsl:template match="type[type_name]"> <type> <xsl:value-of select="type_name" /> </type> </xsl:template> <xsl:template match="type[trigger_type]"> <type> <xsl:value-of select="trigger_type" /> </type> </xsl:template> <xsl:template match="reference[type/svc_type[not(contains(., 'flow') or contains(., 'java') or contains(., 'xsltservice'))]]" /> <xsl:template match="reference[type/type_name[not(contains(., 'connectiondata') or contains(., 'webservicedescriptor'))]]" priority="2" /> <xsl:template match="reference[type/trigger_type[not(contains(., 'jms-trigger') or contains(., 'broker-trigger'))]]" priority="2" /> </xsl:stylesheet>
how important have ids numbered consecutively? suspect want have unique id each node - can achieved using generate-id() function:
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="x"/> <xsl:template match="/"> <root> <xsl:apply-templates select="referenced"/> </root> </xsl:template> <xsl:template match="referenced | reference"> <nodeinfo> <xsl:copy-of select="name"/> <rowid> <xsl:value-of select="generate-id()"/> </rowid> <parentnodeid> <xsl:value-of select="generate-id(..)"/> </parentnodeid> <type> <xsl:value-of select="type/svc_type | type/type_name | type/trigger_type" /> </type> </nodeinfo> <xsl:apply-templates select="reference"/> </xsl:template> </xsl:stylesheet>
the id values processor-dependent; example, using libxslt apply above stylesheet example input, result be:
<?xml version="1.0" encoding="utf-8"?> <root> <nodeinfo> <name>poctree/poctree.services:getreferencesforservices</name> <rowid>idp129115840</rowid> <parentnodeid>idp0</parentnodeid> <type>flow</type> </nodeinfo> <nodeinfo> <name>wmroot/wm.server.ns.dependency:getreferenced</name> <rowid>idp129507904</rowid> <parentnodeid>idp129115840</parentnodeid> <type>java</type> </nodeinfo> <nodeinfo> <name>wmpublic/pub.xml:documenttoxmlstring</name> <rowid>idp49200</rowid> <parentnodeid>idp129115840</parentnodeid> <type>java</type> </nodeinfo> <nodeinfo> <name>poctree/poctree.services:jkl</name> <rowid>idp129175728</rowid> <parentnodeid>idp129115840</parentnodeid> <type>xsltservice</type> </nodeinfo> <nodeinfo> <name>wmpublic/pub.xml:xmlstringtoxmlnode</name> <rowid>idp129088080</rowid> <parentnodeid>idp129115840</parentnodeid> <type>java</type> </nodeinfo> <nodeinfo> <name>wmpublic/pub.xml:xmlnodetodocument</name> <rowid>idp129333520</rowid> <parentnodeid>idp129115840</parentnodeid> <type>java</type> </nodeinfo> <nodeinfo> <name>poctree/poctree.services:ghi</name> <rowid>idm2160</rowid> <parentnodeid>idp129115840</parentnodeid> <type>xsltservice</type> </nodeinfo> <nodeinfo> <name>wxpackagetree/wxpackagetree.doc:nodeinfo</name> <rowid>idp129139968</rowid> <parentnodeid>idp129115840</parentnodeid> <type>record</type> </nodeinfo> <nodeinfo> <name>wxpackagetree/wxpackagetree.doc:nodeinfo</name> <rowid>idp129142464</rowid> <parentnodeid>idp129139968</parentnodeid> <type>record</type> </nodeinfo> <nodeinfo> <name>poctree/poctree.docs:nodeinfo</name> <rowid>idp129145152</rowid> <parentnodeid>idp129139968</parentnodeid> <type>record</type> </nodeinfo> <nodeinfo> <name>poctree/poctree.docs:nodeinfo</name> <rowid>idp129148144</rowid> <parentnodeid>idp129145152</parentnodeid> <type>record</type> </nodeinfo> </root>
Comments
Post a Comment