0

いくつかの要素の値を取得する必要があり、次に要素に応じていくつかのテキストを追加する必要があります。次に例を示します。

<elementType>a</elementType> 
<otherElementType>b</otherElementType> 
<elementType>c</elementType> 
<elementType>d</elementType>

$doc//*[self::elementType or self::otherElementType]/string()

"a"
"b"
"c"
"d"

これは値を返しますが、次のような値を取得する方法がわかりません:

"a is an elementType"
"b is an otherElementType"
"c is an elementType"
"d is an elementType"
4

2 に答える 2

0

使用

//*[self::elementType or self::otherElementType]
              /concat(., ' is a ', name(), '&#xA;')

この式が次のXMLドキュメント(提供された不正な形式のフラグメント-修正済み)に対して評価される場合:

<t>
    <elementType>a</elementType>
    <otherElementType>b</otherElementType>
    <elementType>c</elementType>
    <elementType>d</elementType>
</t>

必要な正しい結果が生成されます:

a is a elementType
 b is a otherElementType
 c is a elementType
 d is a elementType
于 2012-09-03T04:05:18.103 に答える
0

ルールベースの処理を備えた XSLT を検討することをお勧めします。これは、要件について考えている方法と一致しているようです。

使用する

<xsl:apply-templates select="*"/>

すべての要素を処理し、テンプレート ルールを分離して、各種類の要素をどのように処理するかを指定します。

<xsl:template match="elementType">
  do one thing
</xsl:template>

<xsl:template match="otherElementType">
  do a different thing
</xsl:template>
于 2012-09-03T07:57:56.910 に答える