3

私はXSLTなどに非常に慣れていないため、それが可能かどうかはわかりませんが、ここで私を助けてくれる人がいるかもしれません。それは少しトリッキーです、そして私はインターネット上でそれのようなものを見つけませんでした:

問題は、名前空間が宣言された入力xmlがあり、すべてにわずかな変更を加えるだけでよいことです(属性の追加または削除、または他の場所へのシフト)。しかし同時に、ドキュメントのドキュメントタグの名前空間参照を更新する必要があります。したがって、たとえば、入力xmlは次のようになります。

<order
  xmlns="some.url.01"
  xmlns:ns2="some.other.url"
  xmlns:ns3="another.one"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <timestamp>timestamp</timestamp>
      <requestedDocuments>
        <ns2:document>orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

結果のxmlは次のようになります。

<order
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <!-- deleted timestamp for example -->
      <requestedDocuments>
        <ns2:document>orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

しかし、私が得る唯一のものは:

<order
  xmlns="some.url.02"
>
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <!-- deleted timestamp for example -->
      <requestedDocuments>
        <ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>

おそらく1人か2人にとってはそれほど大したことではないかもしれませんが、要求された変更(名前空間の変更と削除)を除いて、出力ドキュメントは入力ドキュメントと1対1で同じように見える必要があるという制限があります。 。

私のXSLTは次のようになります。

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="name(.) != 'timestamp'">
        <xsl:element name="{node-name(.)}">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

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

誰か助けてもらえますか?名前空間には注意が必要です:(

PS:私のエントリを編集した人:ありがとう:)

4

4 に答える 4

2

名前空間属性を使用して、出力要素に名前空間を設定できます。

<xsl:element name="{node-name(.)}" namespace="http://www.bar.org">
  // ...
</xsl:element>

名前空間はURIである必要があることに注意してください。これはご存知だと思いますが、例ではURIを使用することをお勧めします。

例を示した優れたZVONチュートリアルへのリンクは次のとおりです 。http ://www.zvon.org/xxl/XSLTreference/Output/xslt_element_namespace.html

名前空間には注意が必要です。ご存知のように、プレフィックスは意味的に無関係ですが、多くのシステムでは、美的理由からプレフィックスを選択できます。Saxon(http://saxon.sourceforge.net/)もご覧ください。

編集私はあなたがここであなたの答えを見つけると思います: 要素属性名前空間の代わりにXSLTルートタグ名前空間

于 2009-10-22T07:51:25.470 に答える
1
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:ns1_src="some.url.01"
  xmlns:ns2_src="some.other.url"
  xmlns:ns3_src="another.one"
  xmlns="some.url.02"
  xmlns:ns2="some.other.url.02"
  xmlns:ns3="another.one.02"
>
  <!-- 
    Note that all the source namespaces got their own new "*_src" prefix. 
    The target namespaces take over the original prefixes. 
    "some.url.02" is the new global namespace.
  -->

  <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- the identity template to copy everything, unless 
       it has been declared otherwise -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

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

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

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

  <!-- three templates to handle attributes -->
  <xsl:template match="@ns1_src:*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="." />
    </xsl:attribute>
  </xsl:template>

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

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

  <!-- timestamps will be ignored -->
  <xsl:template match="ns1_src:timestamp" />

</xsl:stylesheet>

出力:

<order xmlns="some.url.02">
  <orderEntry>
    <orderControl>
      <mandant>test</mandant>
      <businessUnit>test</businessUnit>
      <inboundChannel>test</inboundChannel>
      <requestedDocuments>
        <ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
      </requestedDocuments>
    </orderControl>
  </orderEntry>
</order>
于 2009-10-22T09:28:25.587 に答える
0
<xsl:template match="a:*">
  <xsl:element name="{local-name()}"
               namespace="http://example.com/B">
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>

プレフィックスが付いた名前空間内の要素を検索し、名前空間とa同じ名前の要素に置き換えますhttp://example.com/B。すべての属性が「そのまま」コピーされ、すべての子が評価されます。

必要に応じて、その中または周辺にカスタム処理を追加します。

于 2009-10-22T07:51:37.230 に答える
0

AntのXSLTタスクを使用して変換を行っていますか?

答えが「はい」の場合は、SunJDK1.5以降に付属しているデフォルトのXSLTエンジンから切り替えることをお勧めします。これを読んでください。

また、XSLTの名前空間に関するこの記事を読んでください

于 2009-10-22T17:12:29.540 に答える