how to select two nodes at one select statement

273 Views Asked by At

I have a xml file like this

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

And I want to the value of the nodes whose name is either variable or group, so what's the right syntax to do things like?

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

Thanks in advance

2

There are 2 best solutions below

0
On

This transformation:

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

when applied on the provided XML document:

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

produces (what I guess is) the wanted result:

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

Do note: <xsl:value-of> outputs the string value, while <xsl:copy-of> outputs the node(s). In your case the string value of either element is empty on white-space only, so you probably want the elements themselves.

This is really an XPath question and there are different possible solutions:

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

(the above is used in the transformation above), or:

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

This one uses the XPath union operator /

2
On

Use (namespace declared with prefix x):

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

Do note that XSLT 1.0 xsl:value-of will return always the text value of the first element found. use better xsl:copy-of to show all returned elements.