как выбрать два узла в одном операторе select - PullRequest
1 голос
/ 11 августа 2011

У меня есть такой 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"/>

Заранее спасибо

Ответы [ 2 ]

1 голос
/ 11 августа 2011

Это преобразование :

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

Do note : <xsl:value-of> выводит строковое значение, в то время как <xsl:copy-of> выводит узлы.В вашем случае строковое значение любого элемента является пустым только в пустом пространстве, поэтому вам, вероятно, нужны сами элементы.

Это действительно вопрос XPath , и существуют различные возможные решения:

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

(приведенное выше используется в приведенном выше преобразовании) или:

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

В этом операторе XPath union используется /

1 голос
/ 11 августа 2011

Использование (пространство имен объявлено с префиксом x):

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

Обратите внимание, что XSLT 1.0 xsl:value-of всегда будет возвращать текстовое значение найденного элемента first . используйте лучше xsl:copy-of, чтобы показать все возвращенные элементы.

...