我有一个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或group的节点的值,那么正确的语法是什么呢?
<xsl:value-of select="/netcdf/variable or /netcdf/group"/>提前感谢
发布于 2011-08-11 16:02:53
Use (使用前缀x声明的命名空间):
"/x:netcdf/*[self::x:variable or self::x:group]"请注意,XSLT1.0 xsl:value-of将始终返回找到的第一个元素的文本值。使用更好的xsl:copy-of来显示所有返回的元素。
发布于 2011-08-11 20:38:15
此转换
<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>输出节点。在本例中,这两个元素的字符串值仅在空格上为空,因此您可能需要这些元素本身。
这实际上是一个XPath问题,有不同的可能的解决方案:
/*/*[self::d:variable or self::d:group](上面的转换用在上面的转换中),或者:
/*/d:variable | /*d:group此应用程序使用XPath operator /
https://stackoverflow.com/questions/7022555
复制相似问题