2

I have this xml file

<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="file:/dev/null" iosp="lasp.tss.iosp.ValueGeneratorIOSP" start="0" increment="1">
    <attribute name="title" value="Vector time series"/>
    <dimension name="time" length="100"/>
    <variable name="time" shape="time" type="double">
        <attribute name="units" type="String" value="seconds since 1970-01-01T00:00"/>
    </variable>
    <group name="Vector" tsdsType="Structure" shape="time">
        <variable name="x" shape="time" type="double"/>
        <variable name="y" shape="time" type="double"/>
        <variable name="z" shape="time" type="double"/>
    </group>
</netcdf>

and I need the xslt file which gives the output like this

1.time
2.Vector

which are the name attribute of two tags: variable and group. Currently I have the code like this

 <xsl:for-each select="document($path)//*[local-name()='variable']">
        <xsl:if test="string-length( @*[local-name()='name'] ) >1">
        <li>
         <xsl:value-of select="position()"/>
        <xsl:value-of select="@*[local-name()='name']"/>
        </li>
        </xsl:if>
      </xsl:for-each>
         <xsl:for-each select="document($path)//*[local-name()='group']">
        <li>
        <xsl:value-of select="position()"/>
        <xsl:value-of select="@*[local-name()='name']"/>
        </li>
      </xsl:for-each>

and it will give me

1.time
1.Vector

So how can I reach my goal by this position() function or there are any other better way to do this in XSLT? Thanks a lot in advance.

Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
user851380
  • 339
  • 1
  • 7
  • 10

1 Answers1

1

You can use position() but it should be used inside the same repetition instruction. Declare the namespace with prefix say x and use:

<xsl:for-each select="document($path)//x:netcdf/*
      [self::x:variable or self::x:group]"/>

Moreover I would use xsl:number like:

<xsl:number value="position()" format="1."/>

Consider also to declare the default namespace in your stylesheet so that you can get rid of local-name() tests.

Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
  • I tried to do this but this syntax doesn't work. do you know what's the syntax to chose two nodes at one select statement? Thanks for the reply – user851380 Aug 11 '11 at 07:44
  • sorry for the miscode, I forgot the parenthesis in the expression. See my edited answer and check it now. – Emiliano Poggi Aug 11 '11 at 07:48