2011-08-11 36 views
1

我有这样如何在一个select语句

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

一个XML文件中选择两个节点,我想他的名字是可变或组节点的值,所以什么是应该做的正确的语法像?

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

在此先感谢

回答

1

使用空间(namespace前缀x声明):

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

请注意,XSLT 1.0 xsl:value-of总是返回发现第一元素的文本值。使用更好的xsl:copy-of来显示所有返回的元素。

+1

请参阅@empo,只有在XSLT 1.0中,您的最后陈述才是真实的。 –

+0

是的,正确的。我太习惯回答XSLT 1.0的问题,我忘了指定它。 –

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> 

做笔记<xsl:value-of>输出字符串值,而<xsl:copy-of>输出节点(s)。在你的情况下,任何一个元素的字符串值只在空白处为空,所以你可能需要这些元素本身。

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

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

(上面是在上面的转化中使用的),或:

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

这一个用途XPath 工会运营商/