首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在一条select语句中选择两个节点

如何在一条select语句中选择两个节点
EN

Stack Overflow用户
提问于 2011-08-11 15:52:45
回答 2查看 175关注 0票数 1

我有一个xml文件,如下所示

代码语言:javascript
复制
<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或group的节点的值,那么正确的语法是什么呢?

代码语言:javascript
复制
<xsl:value-of select="/netcdf/variable or /netcdf/group"/>

提前感谢

EN

回答 2

Stack Overflow用户

发布于 2011-08-11 16:02:53

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

代码语言:javascript
复制
"/x:netcdf/*[self::x:variable or self::x:group]"

请注意,XSLT1.0 xsl:value-of将始终返回找到的第一个元素的文本值。使用更好的xsl:copy-of来显示所有返回的元素。

票数 1
EN

Stack Overflow用户

发布于 2011-08-11 20:38:15

此转换

代码语言:javascript
复制
<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文档上应用时的

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
<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>

注意:<xsl:value-of>输出字符串值,而<xsl:copy-of>输出节点。在本例中,这两个元素的字符串值仅在空格上为空,因此您可能需要这些元素本身。

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

代码语言:javascript
复制
/*/*[self::d:variable or self::d:group]

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

代码语言:javascript
复制
/*/d:variable | /*d:group

此应用程序使用XPath operator /

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7022555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档