1

出力を生成する 1 つの xsl ファイルと 2 つの入力 xml ファイルがあります。1 つの XML ファイルでは、xsl ファイルにインポートされる名前空間がいくつかあります。しかし、他の XML ファイルには名前空間が存在しません。XSL は両方の入力 XML ファイルに共通であるため、両方の XML ファイルを処理できるように XSL を処理する必要があります。提案してください。

最初の XML ファイルの名前空間 ->

<TreeList xmlns="urn:sfti:documents:BasicTreeList:1:0"    
xmlns:cac="urn:sfti:CommonAggregateComponents:1:0"  
xmlns:cbc="urn:oasis:names:tc:ubl:CommonBasicComponents:1:0" 
xmlns:ccts="urn:oasis:names:tc:ubl:CoreComponentParameters:1:0" 
xmlns:cur="urn:oasis:names:tc:ubl:codelist:CurrencyCode:1:0" 
xmlns:sdt="urn:oasis:names:tc:ubl:SpecializedDatatypes:1:0" 
xmlns:udt="urn:oasis:names:tc:ubl:UnspecializedDatatypes:1:0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

2 番目の XML ファイルの名前空間 ->

<TreeList xmlns:cac="urn:sfti:CommonAggregateComponents:1:0"  
xmlns:cbc="urn:oasis:names:tc:ubl:CommonBasicComponents:1:0" 
xmlns:ccts="urn:oasis:names:tc:ubl:CoreComponentParameters:1:0" 
xmlns:cur="urn:oasis:names:tc:ubl:codelist:CurrencyCode:1:0" 
xmlns:sdt="urn:oasis:names:tc:ubl:SpecializedDatatypes:1:0" 
xmlns:udt="urn:oasis:names:tc:ubl:UnspecializedDatatypes:1:0"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

XSL ファイルの名前空間 ->

  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    xmlns:abc="urn:sfti:documents:BasicTreeList:1:0" 
    xmlns:cac="urn:sfti:CommonAggregateComponents:1:0" 
    xmlns:cbc="urn:oasis:names:tc:ubl:CommonBasicComponents:1:0">

XSL ファイルを実行すると、最初の XML で正常に実行されます。ただし、名前空間の問題により、2 番目の XML ファイルではうまく機能しません。

4

1 に答える 1

1

同じ XSLT コードを使用して、ボキャブラリで同じローカル名を使用しているが名前空間が異なるソース ドキュメントを処理する場合、ファイルの 1 つを前処理して名前空間を変更するのが最善の解決策です。これは、次のようなテンプレートを含むスタイルシートを使用して実行できます。

<xsl:template match="*[namespace-uri()='old-uri']">
  <xsl:element name="{local-name()}" namespace="new-uri">
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="*"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*[namespace-uri()='old-uri']">
  <xsl:attribute name="{local-name()}" namespace="new-uri">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

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

<xsl:template match="@*">
  <xsl:copy/>
</xsl:template>
于 2012-07-05T07:40:42.263 に答える