0

ルート ノードの名前空間を変更し、子要素ではなくルート要素のみに名前空間プレフィックスを追加する必要があります。

次の XML があります。

<?xml version="1.0" encoding="UTF-8"?> 
<Class xmlns="https://api.ladbrokes.com/v1/sportsbook-couchbase/SportsbookCouchbase.xsd">
<blurb >Test</blurb>
<channels >
<e >I</e>
<e >J</e>
<e >K</e>
</channels>
<classSortCode >Test</classSortCode>
<classStatus >Test</classStatus>
<creationDateTime >2013-03-21T22:29:01.58+05:30</creationDateTime>
<isActive >true</isActive>
<lastUpdatedDateTime >2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
<locale >Test</locale>
</Class>

そして、私はこれがなる必要があります

<?xml version="1.0" encoding="UTF-8"?> 
<ns0:Class xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
<blurb >Test</blurb>
<channels >
<e >I</e>
<e >J</e>
<e >K</e>
</channels>
<classSortCode >Test</classSortCode>
<classStatus >Test</classStatus>
<creationDateTime >2013-03-21T22:29:01.58+05:30</creationDateTime>
<isActive >true</isActive>
<lastUpdatedDateTime >2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
<locale >Test</locale>
</ns0:Class>

XSLTを使用してこれを達成できますか? この点で私を助けてください。

ありがとう、シヴァ

4

1 に答える 1

1

これは、ドキュメント要素に別の名前空間を与え、他のすべての要素を null 名前空間に移動する方法です。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

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

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

サンプル入力で実行すると、結果は次のようになります。

<ns0:Class xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
  <blurb>Test</blurb>
  <channels>
    <e>I</e>
    <e>J</e>
    <e>K</e>
  </channels>
  <classSortCode>Test</classSortCode>
  <classStatus>Test</classStatus>
  <creationDateTime>2013-03-21T22:29:01.58+05:30</creationDateTime>
  <isActive>true</isActive>
  <lastUpdatedDateTime>2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
  <locale>Test</locale>
</ns0:Class>

明確にするために、これはドキュメント要素の名前空間を変更し、他のすべてを既存の名前空間に残す方法です。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

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

そして、これがその結果です。上部の名前空間宣言の小さいながらも重要な違いに注意してください。

<ns0:Class xmlns:ns0="https://api.ladbrokes.com/v1/sportsbook-couchbase/Temp.xsd" 
     xmlns="https://api.ladbrokes.com/v1/sportsbook-couchbase/SportsbookCouchbase.xsd">
  <blurb>Test</blurb>
  <channels>
    <e>I</e>
    <e>J</e>
    <e>K</e>
  </channels>
  <classSortCode>Test</classSortCode>
  <classStatus>Test</classStatus>
  <creationDateTime>2013-03-21T22:29:01.58+05:30</creationDateTime>
  <isActive>true</isActive>
  <lastUpdatedDateTime>2013-03-21T22:29:01.58+05:30</lastUpdatedDateTime>
  <locale>Test</locale>
</ns0:Class>
于 2013-03-21T17:59:40.410 に答える