1

このような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"/>

前もって感謝します

4

2 に答える 2

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>出力し、ノードを出力します。あなたの場合、いずれかの要素の文字列値は空白のみで空であるため、おそらく要素自体が必要です。

これは実際には XPath に関する質問であり、さまざまな解決策が考えられます。

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

(上記は上記の変換で使用されます)、または:

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

これはXPathユニオン演算子を使用しています /

于 2011-08-11T12:38:15.487 に答える
1

使用 ( prefix で宣言された名前空間x):

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

XSLT 1.0 は、最初に見つかった要素xsl:value-ofのテキスト値を常に返すことに注意してください。返されたすべての要素を表示するには、better を使用します。xsl:copy-of

于 2011-08-11T08:02:52.803 に答える