使用样式表将XML转换为GrapML

时间:2017-11-15 13:57:21

标签: xml xslt

我正在尝试使用xsl样式表将XML文件转换为GraphMl格式(见下文),链接中的fromto属性是用作source文件中的targetGraphMl属性。我的代码运行但输出文件为空,我该如何修复样式表?

示例XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE network SYSTEM "http://www.matsim.org/files/dtd/network_v2.dtd">
<network>

    <!-- ====================================================================== -->

        <nodes>
            <node id="MyCiTi_1" x="-53196.450154726146" y="-3755010.0058102254" >
            </node>
            <node id="MyCiTi_10" x="-54879.37761845079" y="-3753903.660850382" >
            </node>
            <node id="MyCiTi_100" x="-46659.23389528884" y="-3749500.821686937" >
            </node>
        </nodes>

    <!-- ====================================================================== -->

        <links capperiod="01:00:00" effectivecellsize="7.5" effectivelanewidth="3.75">
            <link id="MyCiTi_0" from="MyCiTi_180" to="MyCiTi_180" length="50.0" freespeed="8.333333333333334" capacity="500.0" >
            </link>
            <link id="MyCiTi_1" from="MyCiTi_180" to="MyCiTi_58" length="608.4643035761809" freespeed="8.333333333333334" capacity="500.0"  >
            </link>
            <link id="MyCiTi_10" from="MyCiTi_176" to="MyCiTi_192" length="868.3169964115151" freespeed="8.333333333333334" capacity="500.0" >
            </link>
        </links>

</network>

xsl sheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
        <key id="nodes" for="node" attr.name="id" attr.type="string"/>
        <key id="edges" for="edge" attr.name="id" attr.type="string"/>
        <graph id="g" edgedefault="undirected">
                <xsl:for-each select="node">
                    <node>
                        <data key="node">
                            <xsl:value-of select="@x"/>
                            <xsl:value-of select="@y"/>
                        </data>
                    </node>
                </xsl:for-each>
                <xsl:for-each select="link">
                    <edge source="@from" target="@to">
                        <data key="link">
                            <xsl:value-of select="@length"/>
                        </data>
                    </edge>
                </xsl:for-each>
        </graph>
    </graphml>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

  1. 路径无法正常工作。如果您想使用for-each执行此操作,则需要从network开始。
  2. 要将信息直接传输到其他属性,您只需将这些属性放入{}
  3. 我认为你应该在坐标之间插入一个空格字符。
  4. 节点也应该有ID,对吧?
  5. 试试这个:

    <graph id="g" edgedefault="undirected">
    <xsl:for-each select="network/nodes/node">
        <node id="{@id}">
            <data key="node">
                <xsl:value-of select="@x"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="@y"/>
            </data>
        </node>
        </xsl:for-each>
        <xsl:for-each select="network/links/link">
            <edge source="{@from}" target="{@to}">
                <data key="link">
                   <xsl:value-of select="@length"/>
                </data>
            </edge>
        </xsl:for-each>
    </graph>