我正在尝试使用xsl样式表将XML
文件转换为GraphMl
格式(见下文),链接中的from
和to
属性是用作source
文件中的target
和GraphMl
属性。我的代码运行但输出文件为空,我该如何修复样式表?
示例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>
答案 0 :(得分:1)
for-each
执行此操作,则需要从network
开始。{}
。试试这个:
<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>