0

I am trying to use a java code I modified from here to extract all the attributes in the "node" elements in the sample xml data below. The output is a blank csv with only the header I defined in the stylesheet. Since the code works I suspect the stylesheet is defined incorrectly but I don't how to fix it. How can I correct it?

sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE network SYSTEM "http://www.matsim.org/files/dtd/network_v2.dtd">
<network>
    <nodes>
        <node id="1" x="-53196.450154726146" y="-3755010.0058102254" >
        </node>
        <node id="10" x="-54879.37761845079" y="-3753903.660850382" >
        </node>
        <node id="100" x="-46659.23389528884" y="-3749500.821686937" >
        </node>
        <node id="101" x="-54624.44957950422" y="-3757195.8898357535" >
        </node>
    </nodes>
</network>

style.xsl

?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
    <xsl:template match="/">
        id,x,y
        <xsl:for-each select="//node">
            <xsl:value-of select="node/@*"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Nobi
  • 1,113
  • 4
  • 23
  • 41

1 Answers1

2

You have two problems here. Both occurring in the same statement

 <xsl:value-of select="node/@*"/>

Firstly, you are already positioned on a node element, so this will look for a child element called node of which there is none. It should be this....

 <xsl:value-of select="@*"/>

However, this leads on to the second problem. In XSLT 1.0,xsl:value-of will only return the value of the first node if given a node-set, so although @* will select all nodes, doing <xsl:value-of select="@*"/> will only output the first one.

Now, you could do this...

<xsl:for-each select="@*">
    <xsl:value-of select="." />
</xsl:for-each>

But attributes are not ordered in XML, so there is no guarantee they will be returned in the order you expect. So, to be sure, you would have to explicitly select the attributes

 <xsl:value-of select="@id" />
 <xsl:value-of select="@x" />
 <xsl:value-of select="@y" />
Tim C
  • 70,053
  • 14
  • 74
  • 93