xml - XSLT transformation: flattening nested node based on its attribute value -
i want flatten xml file using xslt. example (there number of node
, edge
nodes):
input:
<?xml version="1.0" encoding="utf-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns"> <graph> <node id="0"> <data key="label">a</data> <data key="tag1">0</data> <data key="tag2">0</data> </node> <edge id="0" source="0" target="1"> <data key="label">referenced_to</data> </edge> </graph> </graphml>
desired output:
<?xml version="1.0" encoding="utf-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns"> <graph> <node id="0" label="a"> <data key="tag1">0</data> <data key="tag2">0</data> </node> <edge id="0" source="1" target="0" label="referenced_to"/> </graph> </graphml>
how can flatten data
tags have key
attribute set "label"
?
how can flatten data tags have key attribute set "label"?
how about:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:gml="http://graphml.graphdrawing.org/xmlns"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <!-- identity transform --> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- add label attribute --> <xsl:template match="gml:node | gml:edge"> <xsl:copy> <xsl:if test="gml:data[@key='label']"> <xsl:attribute name="label"><xsl:value-of select="gml:data[@key='label']"/></xsl:attribute> </xsl:if> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <!-- suppress label element --> <xsl:template match="gml:data[@key='label']"/> </xsl:stylesheet>
Comments
Post a Comment