【问题标题】:how to select two nodes at one select statement如何在一个选择语句中选择两个节点
【发布时间】:2011-10-24 18:03:58
【问题描述】:

我有一个这样的 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>

我想要名称为变量或组的节点的值,那么正确的语法是什么?

<xsl:value-of select="/netcdf/variable or /netcdf/group"/>

提前致谢

【问题讨论】:

    标签: xslt netcdf ncml


    【解决方案1】:

    这种转变:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:d="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
         <xsl:copy-of select="/*/*[self::d:variable or self::d:group]"/>
     </xsl:template>
    
    </xsl:stylesheet>
    

    应用于提供的 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>
    

    产生(我猜是)想要的结果:

    <variable xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" name="time" shape="time" type="double">
       <attribute name="units" type="String" value="seconds since 1970-01-01T00:00"/>
    </variable>
    <group xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" 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>
    

    注意:&lt;xsl:value-of&gt; 输出字符串值,而&lt;xsl:copy-of&gt; 输出节点。在您的情况下,任一元素的字符串值仅在空白处为空,因此您可能需要元素本身。

    这确实是一个 XPath 问题,有不同的可能解决方案:

    /*/*[self::d:variable or self::d:group]
    

    (上面的转换用在上面),或者:

    /*/d:variable | /*d:group
    

    这个使用 XPath union 运算符 /

    【讨论】:

      【解决方案2】:

      使用(使用前缀x声明的命名空间):

      "/x:netcdf/*[self::x:variable or self::x:group]"
      

      请注意,XSLT 1.0 xsl:value-of 将始终返回找到的 first 元素的文本值。使用更好的xsl:copy-of 来显示所有返回的元素。

      【讨论】:

      • 看,@empo,你最后的陈述只在 XSLT 1.0 中是正确的。
      • 是的,正确的。我太习惯于回答 XSLT 1.0 的问题了,我忘记指定了。
      猜你喜欢
      • 1970-01-01
      • 2011-01-03
      • 2011-08-27
      • 1970-01-01
      • 2018-05-10
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多