2013-02-09 91 views
2

我正處於構建xslt文檔以將xml數據導入libre辦公室電子表格的早期階段。 xml數據表示xgmml格式的圖(帶有節點和邊)。在這個階段,我簡單地(?)嘗試選擇圖中的所有節點,提取「標籤」屬性,並將每個標籤放置在電子表格中單個行的單元格中。使用xslt將xml文檔導入到開放式辦公室使用xslt

我可以在libre-office中安裝.xslt並打開.xgmml文件。

我能夠使用.xslt從.xgmml提取一些數據,並把它添加到電子表格

但是,出於某種原因,我無法選擇節點,並提取標籤。

有人可以指出我的(無疑)愚蠢的錯誤?

引用....

的.xgmml .xslt和文件的.odt如下:

XGMML -------------------------------------------

<?xml version="1.0" encoding="UTF-8"?> 
<graph directed="1" label="Graph:-1" xmlns="http://www.cs.rpi.edu/XGMML/" xmlns:cy="http://www.cytoscape.org/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#/" xmlns:xlink="http://www.w3.org/1999/xlink/" 
xmlns:dc="http://purl.org/dc/elements/1.1/"> 
    <node id="204g-1" label="204g-1" start="441940"> 
     <att name="RTC" type="real" value="12.45 " start="443028"/> 
     <att name="LTD" type="real" value="1.53 " start="443495"/> 
     <att name="LTD" type="real" value="1.63 " start="721167"/> 
     <att name="LOD" type="real" value="1.23 " start="721200"/> 
     <att name="LOD" type="real" value="1.32 " start="104941642"/> 
     <att name="RTC" type="real" value="7.31 " start="106070988"/> 
     <att name="LTD" type="real" value="1.76 " start="106071018"/> 
     <att name="LTD" type="real" value="1.81 " start="107270843"/> 
     <att name="LOD" type="real" value="1.20 " start="107270868"/> 
    </node> 
    <edge source="204g-1" target="202g-1" label="0e0" start="443681"> 
     <att name="ITB" type="integer" value="256" start="445896"/> 
     <att name="LTD" type="real" value="1.84 " start="445934"/> 
     <att name="ITB" type="integer" value="256" start="721217"/> 
     <att name="LTD" type="real" value="1.60 " start="721250"/> 
     <att name="ITB" type="integer" value="256" start="1047946"/> 
     <att name="LTD" type="real" value="1.59 " start="1048117"/> 
     <att name="ITB" type="integer" value="256" start="1611675"/> 
     <att name="LTD" type="real" value="1.57 " start="1611810"/> 
    </edge> 
    <node id="202g-1" label="202g-1" start="443660"/> 
    <edge source="204g-1" target="84g-1" label="1e1" start="445960" end="9443711"> 
     <att name="ITB" type="integer" value="148" start="445968"/> 
     <att name="LTD" type="real" value="1.98 " start="721301"/> 
     <att name="LTD" type="real" value="2.35 " start="1612024"/> 
     <att name="LTD" type="real" value="1.99 " start="4159942"/> 
     <att name="LTD" type="real" value="1.91 " start="6158290"/> 
     <att name="ITB" type="integer" value="3" start="7201361"/> 
     <att name="LTD" type="real" value="2.67 " start="8339315"/> 
    </edge> 
    <node id="84g-1" label="84g-1" start="445959" end="9443711"/> 
    <edge source="204g-1" target="27g-1" label="2e2" start="446016" end="876899"> 
     <att name="ITB" type="integer" value="148" start="446021"/> 
     <att name="LTD" type="real" value="1.91 " start="446043"/> 
     <att name="ITB" type="integer" value="163" start="721316"/> 
     <att name="LTD" type="real" value="1.76 " start="721348"/> 
    </edge> 
    <node id="27g-1" label="27g-1" start="446015"/> 
    <edge source="204g-1" target="17g-1" label="3e3" start="446066" end="2926490"> 
     <att name="ITB" type="integer" value="148" start="446072"/> 
     <att name="LTD" type="real" value="1.71 " start="721396"/> 
     <att name="ITB" type="integer" value="168" start="1612329"/> 
     <att name="ITB" type="integer" value="218" start="2344316"/> 
     <att name="LTD" type="real" value="2.00 " start="2344594"/> 
    </edge> 
</graph> 

XSLT ------------------------------------------ --------------------

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 

<office:document-content 
    xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
    xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
    xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
    xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
    xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
    xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
    xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
    xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
    xmlns:math="http://www.w3.org/1998/Math/MathML" 
    xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" 
    xmlns:ooo="http://openoffice.org/2004/office" 
    xmlns:ooow="http://openoffice.org/2004/writer" 
    xmlns:oooc="http://openoffice.org/2004/calc" 
    xmlns:dom="http://www.w3.org/2001/xml-events" 
    xmlns:xforms="http://www.w3.org/2002/xforms" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0"> 

    <office:body> 
    <office:spreadsheet> 
    <table:table table:name="nodes"> 

     <!-- Insert column labels, --> 
     <table:table-row> 
     <xsl:for-each select="graph/node"> 
     <table:table-cell> 
     <text:p><xsl:value-of select="@label"/></text:p> 
     </table:table-cell> 
     </xsl:for-each> 
     </table:table-row>  

     <table:table-row> 
     <table:table-cell> 
     <text:p>the column labels are missing above</text:p> 
     </table:table-cell> 
     </table:table-row>  

     <table:table-row> 
     <table:table-cell> 
     <text:p>but this works</text:p> 
     </table:table-cell> 
     </table:table-row>  

     <table:table-row> 
     <xsl:for-each select="//@label"> 
     <table:table-cell> 
     <text:p><xsl:value-of select="."/></text:p> 
     </table:table-cell> 
     </xsl:for-each> 
     </table:table-row>  

     <table:table-row> 
     <table:table-cell> 
     <text:p>not very useful unfortunately .....</text:p> 
     </table:table-cell> 
     </table:table-row>  

    </table:table> 
    </office:spreadsheet> 
    </office:body> 
    </office:document-content> 
</xsl:template> 
</xsl:stylesheet> 

ODT ----------------- desired ---- --------------------------------------------

A  B  C  D  E  F 
1 204g1 202g-1 84g-1 
2 
3 
+1

你需要採取命名空間的考慮?您的XGMML文檔具有默認名稱空間:xmlns =「http://www.cs.rpi.edu/XGMML/」。有關使用名稱空間的詳細信息,請參閱http://stackoverflow.com/questions/1730875/xslt-transform-xml-with-namespaces。 – Steve 2013-02-09 23:21:50

+0

謝謝史蒂夫 - 是的,我忽略了命名空間,而你的問題使我找到了解決方案。 – nhoj 2013-02-10 19:10:51

回答

1

由於您的元素有xmlns="http://www.cs.rpi.edu/XGMML/",源XML中的所有前綴不固定的元素都位於此名稱空間中,但在XPath表達式中,前綴不匹配的元素匹配不是在命名空間中。因此,你需要將http://www.cs.rpi.edu/XGMML/命名空間URI綁定到一個前綴,在你的樣式表,並使用該前綴的XPath:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:g="http://www.cs.rpi.edu/XGMML/" exclude-result-prefixes="g"> 

    <xsl:template match="/"> 
    <!-- ... --> 
    <xsl:for-each select="g:graph/g:node"> 
    <table:table-cell> 
     <text:p><xsl:value-of select="@label"/></text:p> 
    </table:table-cell> 
    </xsl:for-each> 
    <!-- ... --> 
    </xsl:template> 
</xsl:stylesheet> 
+0

謝謝伊恩 - 是的,這完全是解決方案。 – nhoj 2013-02-10 19:12:05