Skip to content

Instantly share code, notes, and snippets.

@lazyval
Created May 22, 2011 14:57
Show Gist options
  • Save lazyval/985545 to your computer and use it in GitHub Desktop.
Save lazyval/985545 to your computer and use it in GitHub Desktop.
XSL template that converts xml formatted tree into GraphML language representation
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root" >
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="label" for="all" attr.name="label" attr.type="string"/>
<key id="weight" for="node" attr.name="weight" attr.type="int"/>
<graph id="G" edgedefault="undirected">
<xsl:call-template name="childs">
<xsl:with-param name="nodeName"></xsl:with-param>
</xsl:call-template>
</graph>
</graphml>
</xsl:template>
<xsl:template name="childs" match="leftChild | rightChild">
<xsl:param name="nodeName"/>
<xsl:variable name="leftNode">
<xsl:value-of select="concat($nodeName,0)"/>
</xsl:variable>
<xsl:variable name="rightNode">
<xsl:value-of select="concat($nodeName,1)"/>
</xsl:variable>
<xsl:variable name="nodeID">
<xsl:value-of select="concat('n', $nodeName)"/>
</xsl:variable>
<xsl:variable name ="leftChildEdge">
<xsl:value-of select="concat('e', $leftNode)"/>
</xsl:variable>
<xsl:variable name="leftTarget">
<xsl:value-of select="concat('n', $leftNode)"/>
</xsl:variable>
<xsl:variable name ="rightChildEdge">
<xsl:value-of select="concat('e', $rightNode)"/>
</xsl:variable>
<xsl:variable name="rightTarget">
<xsl:value-of select="concat('n', $rightNode)"/>
</xsl:variable>
<xsl:if test ="not(./outcome)">
<!-- if left one is innernode -->
<xsl:if test="not(./leftChild/outcome)">
<edge directed="true"
id="{$leftChildEdge}"
source="{$nodeID}"
target="{$leftTarget}">
<data key="label">
<xsl:value-of select="concat('col',
scoreFunction/n/node()[1]+1,
' >= ',
./node()[4]/node()[1])"
/>
</data>
</edge>
</xsl:if>
<!-- if right one is innernode (not leaf) -->
<xsl:if test="not(./rightChild/outcome)">
<edge directed="true"
id="{$rightChildEdge}"
source="{$nodeID}"
target="{$rightTarget}">
<data key="label">
<xsl:value-of select="concat('col',
scoreFunction/n/node()[1]+1,
' =&lt; ',
./node()[4]/node()[1])"
/>
</data>
</edge>
</xsl:if>
<node id="{$nodeID}">
<data key="label">
<xsl:value-of select="concat('col',
scoreFunction/n/node()[1]+1,
' >= ',
./node()[4]/node()[1])"
/>
</data>
<data key="weight">
<xsl:value-of select="./throughput"/>
</data>
</node>
<xsl:apply-templates select="leftChild">
<xsl:with-param name="nodeName" select="$leftNode"/>
</xsl:apply-templates>
<xsl:apply-templates select="rightChild">
<xsl:with-param name="nodeName" select="$rightNode"/>
</xsl:apply-templates>
</xsl:if>
<!--</xsl:if>-->
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root" >
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="label" for="all" attr.name="label" attr.type="string"/>
<key id="weight" for="node" attr.name="weight" attr.type="int"/>
<graph id="G" edgedefault="undirected">
<xsl:call-template name="childs">
<xsl:with-param name="nodeName"></xsl:with-param>
</xsl:call-template>
</graph>
</graphml>
</xsl:template>
<xsl:template name="childs" match="leftChild | rightChild">
<xsl:param name="nodeName"/>
<xsl:variable name="leftNode">
<xsl:value-of select="concat($nodeName,0)"/>
</xsl:variable>
<xsl:variable name="rightNode">
<xsl:value-of select="concat($nodeName,1)"/>
</xsl:variable>
<xsl:variable name="nodeID">
<xsl:value-of select="concat('n', $nodeName)"/>
</xsl:variable>
<xsl:variable name ="leftChildEdge">
<xsl:value-of select="concat('e', $leftNode)"/>
</xsl:variable>
<xsl:variable name="leftTarget">
<xsl:value-of select="concat('n', $leftNode)"/>
</xsl:variable>
<xsl:variable name ="rightChildEdge">
<xsl:value-of select="concat('e', $rightNode)"/>
</xsl:variable>
<xsl:variable name="rightTarget">
<xsl:value-of select="concat('n', $rightNode)"/>
</xsl:variable>
<xsl:if test ="not(./outcome)">
<!-- if left one is innernode -->
<xsl:if test="not(./leftChild/outcome)">
<edge directed="true"
id="{$leftChildEdge}"
source="{$nodeID}"
target="{$leftTarget}">
<data key="label">
<xsl:value-of select="concat('col',
scoreFunction/n/node()[1]+1,
' >= ',
./node()[4]/node()[1])"
/>
</data>
</edge>
</xsl:if>
<!-- if right one is innernode (not leaf) -->
<xsl:if test="not(./rightChild/outcome)">
<edge directed="true"
id="{$rightChildEdge}"
source="{$nodeID}"
target="{$rightTarget}">
<data key="label">
<xsl:value-of select="concat('col',
scoreFunction/n/node()[1]+1,
' =&lt; ',
./node()[4]/node()[1])"
/>
</data>
</edge>
</xsl:if>
<node id="{$nodeID}">
<data key="label">
<xsl:value-of select="concat('col',
scoreFunction/n/node()[1]+1,
' >= ',
./node()[4]/node()[1])"
/>
</data>
<data key="weight">
<xsl:value-of select="./throughput"/>
</data>
</node>
<xsl:apply-templates select="leftChild">
<xsl:with-param name="nodeName" select="$leftNode"/>
</xsl:apply-templates>
<xsl:apply-templates select="rightChild">
<xsl:with-param name="nodeName" select="$rightNode"/>
</xsl:apply-templates>
</xsl:if>
<!--</xsl:if>-->
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment