【问题标题】:how to accumulate the value of position() function in xslt如何在xslt中累积position()函数的值
【发布时间】:2011-10-24 17:05:55
【问题描述】:

我有这个 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 中还有其他更好的方法吗?提前非常感谢。

【问题讨论】:

    标签: xslt netcdf ncml


    【解决方案1】:

    您可以使用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() 测试。

    【讨论】:

    • 我试图这样做 但这种语法不起作用。你知道在一个 select 语句中选择两个节点的语法是什么吗?感谢回复
    • 对不起,我忘记了表达式中的括号。查看我编辑的答案并立即检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多