2012-08-24 9 views
5

このコード例では、B1ノードをBノードの後に​​、ノードC、DおよびEの前に2つの課題があります。 2番目のKEYノードを/ ROOT/E/OTHER/DEAL/KEYS構造体に追加します。複数のテンプレート定義を適用してそれぞれが同じXML構造を変更する場合

このXMLサンプル:変換後の

 <ROOT> 
      <A>some A text</A> 
      <B>some B text</B> 
      <C>some C text</C> 
      <D>some D text</D> 
      <E> 
      <OTHER> 
       <DEAL> 
       <KEYS> 
        <KEY> 
        <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType> 
        <KeyValue>123456|1</KeyValue> 
        </KEY> 
       </KEYS> 
       </DEAL> 
      </OTHER> 
      </E> 
     </ROOT> 

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <xsl:strip-space elements="*"/> 
     <xsl:output method="xml" indent="yes"/> 

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

     <!-- Identifiers are added by the system. Need to pass parms from the calling program --> 
     <xsl:template match="ROOT" name="add-B1"> 
      <xsl:variable name="elements-after" select="C|D|E"/> 
      <xsl:copy> 
       <xsl:copy-of select="* except $elements-after"/> 
       <B1>some B1 text</B1> 
       <xsl:copy-of select="$elements-after"/> 
      </xsl:copy> 
     </xsl:template> 

     <!-- KEY is added by the system. Need to pass parms from the calling program --> 
     <xsl:template match="ROOT/E/OTHER/DEAL/KEYS" name="add-KEYS"> 
      <xsl:param name="KeyIdentifierTypeB">654321|1</xsl:param> 
      <xsl:copy> 
       <xsl:copy-of select="*"/> 
       <KEY> 
        <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType> 
        <KeyValue> 
         <xsl:value-of select="$KeyIdentifierTypeB"/> 
        </KeyValue> 
       </KEY> 
      </xsl:copy> 
     </xsl:template> 
    </xsl:stylesheet> 

になる:なぜ2番目のテンプレートの定義が完全に

 <?xml version="1.0" encoding="UTF-8"?> 
     <ROOT> 
      <A>some A text</A> 
      <B>some B text</B> 
      <B1 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">some B1 text</B1> 
      <C>some C text</C> 
      <D>some D text</D> 
      <E> 
       <OTHER> 
        <DEAL> 
         <KEYS> 
          <KEY> 
           <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType> 
           <KeyValue>123456|1</KeyValue> 
          </KEY> 
         </KEYS> 
        </DEAL> 
       </OTHER> 
      </E> 
     </ROOT> 

を無視されてきましたか?

最初のコードカレッジが解決されました。B1ノードがBノードの後、ノードC、DおよびEの前、またはB1ノードが設定されていて、その後の場所でなければならないノードがC、DおよびE 。
/ROOT/E/OTHER/DEAL/KEYS構造体に2番目のKEYノードを追加するために、2番目のチャレンジパートを満たす2番目のテンプレートmatch = "ROOT/E/OTHER/DEAL/KEYS"完全に。 この事実に加えて、最初のテンプレートマッチをROOTノードにコメントすると、2番目のテンプレートmatch = "ROOT/E/OTHER/DEAL/KEYS"が正しく動作しますが、実際には追加キーが追加されますが、最初のテンプレートの一致が常に2番目のテンプレートの一致を上書きする理由を知りません。私はxsl:template match = "ROOT/E/OTHER/DEAL/KEYS ...とxsl:for-each select = ...とxsl:call-template name =" add-KEYS "を試してみましたが、何も役に立たなかった

実際にはapply-templatesというノードテンプレートは、より高い優先順位を持つ上位構造のテンプレートと一致します。XSLTファイル内のテンプレートの場所を変更しても、影響はありません。 一致するテンプレートごとに "apply-templates"がXML構造に変更を加えると暗黙のうちに "for-each"が作成されますが、 "advice" 2番目のテンプレートマッチが最初のテンプレートから触れられたことのない別のXPathの場所を探しているので、構造体が変更された2番目のテンプレートと、なぜそれを行うべきですか?私の場合はg?...そしてそれを行う方法のベストプラクティスは何ですか?

期待される結果:

 <?xml version="1.0" encoding="UTF-8"?> 
     <ROOT> 
      <A>some A text</A> 
      <B>some B text</B> 
      <B1>some B1 text</B1> 
      <C>some C text</C> 
      <D>some D text</D> 
      <E> 
       <OTHER> 
        <DEAL> 
         <KEYS> 
          <KEY> 
           <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType> 
           <KeyValue>123456|1</KeyValue> 
          </KEY> 
          <KEY> 
           <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType> 
           <KeyValue>654321|1</KeyValue> 
          </KEY> 
         </KEYS> 
        </DEAL> 
       </OTHER> 
      </E> 
     </ROOT> 

答えて

3

第2チャレンジの部分を満足させなければならない2番目のテンプレートmatch="ROOT/E/OTHER/DEAL/KEYS"/ROOT/E/OTHER/DEAL/KEYS構造に2つ目のKEYノードを追加するために、完全に無視されてきました。

テンプレートマッチングROOTに問題があります。xsl:apply-templatesはありません。テンプレートはxsl:apply-templates命令の結果でのみ実行のために選択されます。あなたのコードにはxsl:apply-templatesがありません。したがって、XSLTプロセッサはテンプレートをそれ以上適用せず、実行を終了します。

xsl:copy-ofxsl:apply-templatesに変更した場合は異なります。ここで

を行う方法である。この:指名手配、正しい結果がを生産している

<ROOT> 
    <A>some A text</A> 
    <B>some B text</B> 
    <C>some C text</C> 
    <D>some D text</D> 
    <E> 
     <OTHER> 
      <DEAL> 
       <KEYS> 
        <KEY> 
         <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType> 
         <KeyValue>123456|1</KeyValue> 
        </KEY> 
       </KEYS> 
      </DEAL> 
     </OTHER> 
    </E> 
</ROOT> 

:この変換が提供されるXML文書に適用され

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="/*"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|B/preceding-sibling::node()"/> 
    <xsl:apply-templates select="B"/> 
    <B1>some B1 text</B1> 
    <xsl:apply-templates select="B/following-sibling::node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="KEY"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    <KEY> 
    <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType> 
    <KeyValue> 
    <xsl:value-of select="'654321|1'"/> 
    </KeyValue> 
    </KEY> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

<ROOT> 
    <A>some A text</A> 
    <B>some B text</B> 
    <B1>some B1 text</B1> 
    <C>some C text</C> 
    <D>some D text</D> 
    <E> 
     <OTHER> 
     <DEAL> 
      <KEYS> 
       <KEY> 
        <KeyIdentifierType>KeyIdentifierTypeA</KeyIdentifierType> 
        <KeyValue>123456|1</KeyValue> 
        <KEY> 
        <KeyIdentifierType>KeyIdentifierTypeB</KeyIdentifierType> 
        <KeyValue>654321|1</KeyValue> 
        </KEY> 
       </KEY> 
      </KEYS> 
     </DEAL> 
     </OTHER> 
    </E> 
</ROOT> 
+0

ありがとうございました! –

+0

@ user1619586、ようこそ。 –