how to accumulate the value of position() function in xslt
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="positi开发者_运维技巧on()"/>
<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.
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.
精彩评论