1

XML出力を生成しようとしていますが、それを実行するXSLTを作成しました。ただし、ルートノードに名前の間隔がありません。XML構造のルート要素に名前空間を追加するにはどうすればよいですか。これは私が使用しているXSLTです:

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:doc="urn:sapcom:document:sap:rfc:functions" xmlns:r="http://www.castiron.com/response" exclude-result-prefixes="r">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:element name="imageScene7Request">
            <xsl:element name="productIds">
                <xsl:for-each select="r:productGetAllByIdsResponse/r:payload/r:products">
                    <xsl:value-of select="r:id"/>
                    <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

ルートに追加したい名前空間http://www.castiron.com/response

入力XML

<?xml version="1.0" encoding="UTF-8"?>
<productGetAllByIdsResponse xmlns="http://www.castiron.com/response">
    <rcode>0</rcode>
    <rmessage>Success</rmessage>
    <payload>
        <products>
            <id>4022280</id>
        </products>
        <products>
            <id>4022280</id>
        </products>
    </payload>
</productGetAllByIdsResponse>

実行すると、次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<imageScene7Request>
    <productIds>4022280,4022280</productIds>
</imageScene7Request>

しかし、私はこれが欲しいです:

<?xml version="1.0" encoding="utf-8"?>
<imageScene7Request xmlns="http://www.castiron.com/response">
    <productIds>4022280,4022280</productIds>
</imageScene7Request>

返信@dbaseman

それは機能しましたが、次に示すように、2番目のタグにnull名前空間を与えました。

<?xml version="1.0" encoding="utf-8"?>
<imageScene7Request xmlns="http://www.castiron.com/response">
    <productIds xmlns="">4022280,4022280</productIds>
</imageScene7Request>

それを削除する方法はありますか?

4

3 に答える 3

3

結果要素の名前が静的にわかっているので、xsl:elementよりもリテラル結果要素を使用する方がはるかに優れています。

 <xsl:template match="/">
    <imageScene7Request xmlns="http://www.castiron.com/response">
        <productIds>
            <xsl:for-each select="r:productGetAllByIdsResponse/r:payload/r:products">
                <xsl:value-of select="r:id"/>
                <xsl:if test="position() != last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </productIds>
    </imageScene7Request>
</xsl:template>

xsl:elementを使用する場合は、必ずnamespace属性を使用して、要素が正しい名前空間にあることを確認する必要があります。

于 2012-05-30T13:10:46.273 に答える
2

スタイルシートで名前空間を明示的に指定する必要があると思います。

<xsl:element name="imageScene7Request" namespace="http://www.castiron.com/response">
    <xsl:element name="productIds">
       ...
    </xsl:element>
</xsl:element>
于 2012-05-30T09:10:17.903 に答える
1

これは機能しますか?

<xsl:stylesheet xmlns="http://www.castiron.com/response" ...>

これにより、XSLT 内のすべての要素の名前空間がhttp://www.castiron.com/response

于 2012-05-30T13:24:39.810 に答える