简体   繁体   中英

import xml document into open office using xslt

I am at the early stages of building an xslt document to import xml data into a libre office spreadsheet. The xml data represents a graph (with nodes and edges) in xgmml format. At this stage I am simply(?) trying to select all of the nodes in the graph, extract the "label" attribute, and place each label in a cell on a single row in the spreadsheet.

I am able to install the .xslt in libre-office and open .xgmml files.

I am able to use the .xslt to extract some data from the .xgmml and add it to the spreadsheet

But, For some reason I am unable to select the nodes and extract the label.

Can some-one please point out my (undoubtedly) silly error?

references....

The .xgmml .xslt and .odt documents that I am working are as follows:

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

Because your graph element has xmlns="http://www.cs.rpi.edu/XGMML/" , all the unprefixed elements in your source XML are in this namespace, but in XPath expressions, an unprefixed name matches elements that are not in a namespace. Therefore you need to bind the http://www.cs.rpi.edu/XGMML/ namespace URI to a prefix in your stylesheet and use that prefix in the XPaths:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM