1

以下のようなxmlがあります。以下の xml を変換して、すべての名前空間と "Return" タグを削除するには、あなたの助けが必要です。誰かが私に正しい xsl を提供してくれたら感謝します。

いろいろ試して疲れました。

元の XML:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Body>
<ns:Response xmlns:ns="http://demo.test.classes.com">
   <ns:return>
      <ns:person>
         <ns:personName></ns:personName>
         <ns:personAge></ns:personAge>
         <ns:personAddress>
            <ns:addressType>official</ns:addressType>
            <ns:addressLine1>official address line 1</ns:addressLine1>
         </ns:personAddress>
         <ns:personAddress>
            <ns:addressType>residence</ns:addressType>
            <ns:addressLine1>residence address line 1</ns:addressLine1>
         </ns:personAddress>         
      </ns:person>
   </ns:return>
</ns:Response>
</soapenv:Body>
</soapenv:Envelope>  

変換後に予想される XML:

<Response>
      <person>
         <personName></personName>
         <personAge></personAge>
         <personAddress>
            <addressType>official</addressType>
            <addressLine1>official address line 1</addressLine1>
         </personAddress>
         <personAddress>
            <addressType>residence</addressType>
            <addressLine1>residence address line 1</addressLine1>
         </personAddress>         
      </person>
</Response>  

これは、私が現在使用している XSLT です。しかし、これは私が必要とするxmlを生成しません。

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/|comment()|processing-instruction()">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@*|node()" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
    </xsl:template>

    <xsl:template match="Response/return" />

</xsl:stylesheet>
4

2 に答える 2

1

SOAP は名前空間属性を使用しないため、少し単純化する余裕があることに注意してください。この非常に短い XSLT 1.0 スタイルシートでうまくいきます。

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
    xmlns:ns="http://demo.test.classes.com"
    exclude-result-prefixes="xsl soapenv ns">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="*">
 <xsl:element name="{local-name()}">
  <xsl:apply-templates select="@*|node()" />
 </xsl:element>
</xsl:template>

<xsl:template match="ns:return|soapenv:Envelope|soapenv:Body">
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

これは SOAP ドキュメントであるため、コメントや PI に関心があるとは考えにくいでしょう。ただし、本当に元に戻したい場合は、調整するのは簡単なことです。

于 2012-09-03T14:17:55.993 に答える
0

それ以外の

<xsl:template match="Response/return" />

あなたが必要

<xsl:template xmlns:ns="http://demo.test.classes.com" match="ns:return">
  <xsl:apply-templates/>
</xsl:template>

このようにして、の子と子孫は、ns:return作成した他のテンプレートによって処理されます。

于 2012-09-03T14:01:21.553 に答える