使用XSLT转换graphml的正确标记名称的正确方法是什么?

时间:2017-05-23 17:24:00

标签: xslt xslt-1.0 xslt-2.0

我在使用XSLT将graphml转换为HTML时遇到问题。 我试图做的转变很简单但是我无法理解我现在做错了什么。

这是我要转换的graphml:

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key for="node" id="d0" yfiles.type="nodegraphics"/>
  <key for="edge" id="d1" yfiles.type="edgegraphics"/>

  <graph id="dependencies" edgedefault="directed">

    <node id="2086673744">
      <data key="d0">
        <y:ShapeNode>
          <y:NodeLabel>com.quadreal.mulesoft.services:qr-identitymgmt-services:mule:3.0.0-SNAPSHOT</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>

    <node id="1296670053">
      <data key="d0">
        <y:ShapeNode>
          <y:NodeLabel>com.quadreal.mulesoft.context:quadreal-runtime-context:jar:1.0.0-SNAPSHOT:compile</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>

    <edge source="2086673744" target="1296670053">
      <data key="d1">
        <y:PolyLineEdge>
          <y:EdgeLabel>compile</y:EdgeLabel>
        </y:PolyLineEdge>
      </data>
    </edge>

    <node id="826245889">
      <data key="d0">
        <y:ShapeNode>
          <y:NodeLabel>com.quadreal.mulesoft.library:qr-common-error-library:jar:2.0.0-SNAPSHOT:compile</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>

    <node id="1556730832">
      <data key="d0">
        <y:ShapeNode>
          <y:NodeLabel>com.quadreal.mulesoft.notification:quadreal-utility-common-domains:jar:3.0.0-SNAPSHOT:compile</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>

    <edge source="826245889" target="1556730832">
      <data key="d1">
        <y:PolyLineEdge>
          <y:EdgeLabel>compile</y:EdgeLabel>
        </y:PolyLineEdge>
      </data>
    </edge>

    <edge source="2086673744" target="826245889">
      <data key="d1">
        <y:PolyLineEdge>
          <y:EdgeLabel>compile</y:EdgeLabel>
        </y:PolyLineEdge>
      </data>
    </edge>

  </graph>
</graphml>

这是我将用于转换graphml的XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
      <html>
        <body>
          <h1>Dependencies:</h1>
          <table border="1" width="300">
            <tr><th>Package Name</th><th>Dependencies</th></tr>
            <xsl:apply-templates select="/graphml/graph/*"/>
          </table>
        </body>
      </html>
    </xsl:template>

    <xsl:template match="node">
      <tr><td><xsl:value-of select="data/ShapeNode/NodeLabel"/></td><td>TBD</td></tr>
    </xsl:template>

</xsl:stylesheet>

这是我得到的输出:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><META http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><div>
<h1>Dependencies:</h1>
<table border="1" width="300"><tr>
<th>Package Name</th>
<th>Dependencies</th>
</tr></table>
</div>
</body></html>

我尝试了许多不同的方法来使这个工作,但他们没有工作。 为什么它不匹配并应用节点模板来正确添加行?

谢谢!

1 个答案:

答案 0 :(得分:0)

它不匹配,因为node位于命名空间xmlns="http://graphml.graphdrawing.org/xmlns"

在样式表中声明相同的命名空间(带有非空前缀)并在xpath表达式中使用它

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:g="http://graphml.graphdrawing.org/xmlns"
    xmlns:y="http://www.yworks.com/xml/graphml">

    ...
    <xsl:apply-templates select="/g:graphml/g:graph/*"/>
    ...
    <xsl:template match="g:node">
        <tr><td><xsl:value-of select="g:data/y:ShapeNode/y:NodeLabel"/></td><td>TBD</td></tr>
    </xsl:template>
    ...