我正在研究網路圖的 3D 可視化,3D 可視化的輸入是一個包含節點和邊的 XML 檔案。XML 檔案是沒有適當的 3d 可視化功能的網路分析工具的輸出。不幸的是,這個 XML 檔案無法在我進行 3D 可視化的工具中正確讀取。我不是 XML 方面的專家,但知道應該將檔案轉換成什么,以使其成為我的 3D 可視化工具的可讀 XML。我已經嘗試了 2-3 天來使用 XSLT 轉換 XML 但無法解決。希望有人能提供幫助。XML 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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="desc" for="node" attr.name="Description" attr.type="string"/>
<key id="x" for="node" attr.name="x" attr.type="float"/>
<key id="y" for="node" attr.name="y" attr.type="float"/>
<key id="z" for="node" attr.name="z" attr.type="float"/>
<key id="d1" for="node" attr.name="Node Component Identifier" attr.type="string"/>
<key id="d2" for="node" attr.name="Node Degree" attr.type="int"/>
<graph edgedefault="directed">
<node id="n0">
<data key="d1">Component 1</data>
<data key="d2">10</data>
<data key="desc">Person Alpha</data>
<data key="x">17.0119</data>
<data key="y">-1.36144</data>
<data key="z">2.80569</data>
</node>
<node id="n1">
<data key="d1">Component 1</data>
<data key="d2">10</data>
<data key="desc">Person Beta</data>
<data key="x">11.091</data>
<data key="y">1.183</data>
<data key="z">-3.55</data>
</node>
</graph>
</graphml>
作為輸出,我需要一個更簡單的格式,如下所示(洗掉鍵,洗掉 d2):
<graphml>
<graph egedefault="directed">
<node id="n0">
<component>Component 1</component>
<desc>Person Alpha</desc>
<x>17.0119</x>
<y>-1.36144</y>
<z>2.80569</z>
</node>
<node id="n1">
<component>Component 1</component>
<desc>Person Beta</desc>
<x>11.091</x>
<y>1.183</y>
<z>-3.55</z>
</node>
</graph>
</graphml>
有沒有人可以幫助我使用 XSLT 轉換實作我的目標?
我嘗試了一些技巧,包括:
- 如何使用帶有樣式表和 xsltproc 的 xslt 從 xml 中洗掉元素?
- 使用 XSLT 洗掉 xml 標簽
- https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)
它們都與我的例子有點不同,因此沒有得到正確的最終結果。
uj5u.com熱心網友回復:
這是一個 XSLT 1.0 解決方案。(一些元素需要重命名并且您的輸出不在任何命名空間中。)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:graph="http://graphml.graphdrawing.org/xmlns"
exclude-result-prefixes="graph"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="graph:graphml">
<graphml>
<xsl:apply-templates select="*"/>
</graphml>
</xsl:template>
<xsl:template match="graph:key"/>
<xsl:template match="graph:graph">
<graph>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*"/>
</graph>
</xsl:template>
<xsl:template match="graph:node">
<node>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*"/>
</node>
</xsl:template>
<xsl:template match="graph:data[@key='d1']">
<component>
<xsl:value-of select="."/>
</component>
</xsl:template>
<xsl:template match="graph:data[@key='d2']"/>
<xsl:template match="graph:data[@key='desc']">
<desc>
<xsl:value-of select="."/>
</desc>
</xsl:template>
<xsl:template match="graph:data[@key='x']">
<x>
<xsl:value-of select="."/>
</x>
</xsl:template>
<xsl:template match="graph:data[@key='y']">
<y>
<xsl:value-of select="."/>
</y>
</xsl:template>
<xsl:template match="graph:data[@key='z']">
<z>
<xsl:value-of select="."/>
</z>
</xsl:template>
</xsl:stylesheet>
看到它在這里作業:https ://xsltfiddle.liberty-development.net/nbL5Ydc
uj5u.com熱心網友回復:
這是一個 XSLT 2.0 解決方案
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:b="http://graphml.graphdrawing.org/xmlns"
exclude-result-prefixes="xs b"
version="2.0">
<xsl:output method="xml" indent="yes"></xsl:output>
<xsl:template match="/b:graphml/b:graph">
<graphml>
<graph egedefault="{@edgedefault}">
<xsl:for-each select="b:node">
<node id="{@id}">
<component><xsl:value-of select="b:data[@key='d1']"/></component>
<desc><xsl:value-of select="b:data[@key='desc']"/></desc>
<x><xsl:value-of select="b:data[@key='x']"/></x>
<y><xsl:value-of select="b:data[@key='y']"/></y>
<z><xsl:value-of select="b:data[@key='z']"/></z>
</node>
</xsl:for-each>
</graph>
</graphml>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/534580.html
標籤:XMLxslt图形
上一篇:如何將變數傳遞給XML檔案(通過Terraform)?
下一篇:VBA展望-讀取XML檔案
