0

次のいずれかのような XML があります。

// #1
<A>
     <B>... stuff ...</B>
</A>

// #2
<B>... stuff ...</B>

これらを、両方のインスタンスで同じように見える応答ノードに変換する必要があります。このような並べ替え:

<fooMethodResponse>
    ... one thing from A if A was root ...
    ... stuff from B ...
</fooMethodResponse>

自分自身を繰り返さずにこれを最も簡単に行うにはどうすればよいですか? 私は今これをやった:

<xsl:template match="/A">
        <fooMethodResponse>
            <xsl:apply-templates select="B" mode="get-B" />
        <xsl:element name="processId">
            <xsl:value-of select="@id" />
        </xsl:element>
    </fooMethodResponse>
</xsl:template>

<xsl:template match="/B">
    <fooMethodResponse>
        <xsl:apply-templates select="." mode="get-B" />
    </fooMethodResponse>
</xsl:template>

<xsl:template match="B" mode="get-B"></xsl:template>

ここでの問題は、応答ラッパーを繰り返していることです。それを 1 か所だけにしたいのです。私はこのようなことができると考えました:

<xsl:template match="/">
    <fooMethodResponse>
        <xsl:choose>
            <xsl:when test="node name is A">
            <xsl:when test="node name is B">
        </xsl:choose>
    </fooMethodResponse>
</xsl:template>

しかし、ルート要素のノード名を確認するテストの書き方がわかりません。ルート要素はどういうわけか異なる方法で処理されますか?


より正確な例を挙げたいと思いますが、そこにはかなりのビジネス関連のものがあるので、要約してみました :p

4

2 に答える 2

0

何をしたいのかわかりません。より正確な入力と出力のサンプルが必要です。それでも、次のXSLT(1.0)が問題を解決するためのベースになる可能性があります。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
        <fooMethodResponse>
            <xsl:apply-templates/>
        </fooMethodResponse>
    </xsl:template>
    <xsl:template match="A">
        <xsl:text>... one thing from A if A was root ...</xsl:text>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="B">
        <xsl:text>... stuff from B ...</xsl:text>
    </xsl:template>
</xsl:stylesheet>

入力#1の場合:

<A>
     <B>... stuff ...</B>
</A>

結果#1は:

<fooMethodResponse>... one thing from A if A was root ...
    ... stuff from B ...
</fooMethodResponse>

入力#2の場合:

<B>... stuff ...</B>

結果#2は:

<fooMethodResponse>... stuff ...</fooMethodResponse>

お役に立てれば!

于 2012-08-27T12:16:52.723 に答える
0

あなたができることは、|演算子とパターンを組み合わせることです。

<xsl:template match="/A[B] | /B">
  <fooMethodResponse>...</fooMethodResponse>
</xsl:template>

fooMethodResponseそれが理にかなっているのか、あなたの場合は単純化されているのか、2つの異なる要素の中に何を入れたいのかわからないのでわかりません。あなたが投稿した可能性のある入力サンプルごとに各結果サンプルを詳しく説明することを検討してください。現在の単一の結果サンプルは私にはわかりません。

于 2012-08-27T12:16:59.250 に答える