如何在xslt中累加position()函数的值

发布于 2024-11-28 22:11:09 字数 1659 浏览 4 评论 0原文

我有这个 xml 文件

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

,我需要 xslt 文件,它给出这样的输出

1.time
2.Vector

,它们是两个标签的名称属性:变量和组。目前我有这样的代码

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

,它会给我

1.time
1.Vector

那么我如何通过这个position()函数达到我的目标,或者有其他更好的方法在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="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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2024-12-05 22:11:09

您可以使用 position() 但它应该在同一重复指令内使用。声明带有前缀 x 的命名空间并使用:

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

此外,我会使用 xsl:number ,例如:

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

还可以考虑在样式表中声明默认命名空间,以便您可以摆脱local-name() 测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文