2016-08-17 72 views
0

を変更するにはマップを使用して、私はXMLファイルがあります。各ENTRY要素についてXSLT:子要素

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <category id="Cat1" owner="Team1"> 
     <entry id="Ent1" owner="John"> 
      <title>This is Entry 1</title> 
     </entry> 
     <entry id="Ent2" owner="Matt"> 
      <title>This is Entry 2</title> 
     </entry> 
    </category> 
    <category id="Cat2" owner="Team2"> 
     <entry id="Ent3" owner="Arnold"> 
      <title>This is Entry 3</title> 
     </entry> 
     <entry id="Ent4" owner="Jim"> 
      <title>This is Entry 4</title> 
     </entry> 
    </category> 
</root> 

を、私はその@id属性に基づいてそのTITLE子要素の値を変更したいと思います。最初にマップを定義する次のXSLTを作成しました。各マップエントリのキーは、タイトルを変更したい要素の@idです。各マップエントリの値は、私は(ENTRYの子である)TITLE要素の値がなりたいものです。

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
        <xsl:strip-space elements="*"/> 
        <!-- set up the map --> 
        <xsl:variable name="map"> 
         <entry key="Ent1">Here is the first entry</entry> 
         <entry key="Ent2">Here is the second entry</entry> 
         <entry key="Ent3">Here is the third entry</entry> 
         <entry key="Ent4">Here is the fourth entry</entry> 
        </xsl:variable> 
        <!-- identity transform --> 
        <xsl:template match="@*|node()"> 
        <xsl:copy> 
         <xsl:apply-templates select="@*|node()"/> 
        </xsl:copy> 
        </xsl:template> 
    
        <!-- Problem area: use the key to set the attribute of the correct procedure --> 
        <xsl:template match="entry"> 
         <title><xsl:value-of select="$map/entry[@key = current()/@id]"/></title> 
        </xsl:template> 
    </xsl:stylesheet> 
    

    私の理解から、これは2段階のプロセスであります

  1. アイデンティティが
  2. TITLE要素

を変更するには、新しいテンプレートを作成します。しかし、これはTITLE elemenと私の全体のENTRY要素を交換したAA奇妙な出力を作成する変換を行いますts ...それはすべてが1ステップ高すぎるように実行されたかのようです。

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <category id="Cat1"> 
     <title>Here is the first entry</title> 
     <title>Here is the second entry</title> 
     <title>Here is the third entry</title> 
    </category> 
    <category id="Cat2"> 
     <title>Here is the fourth entry</title> 
     <title>Here is the fifth entry</title> 
     <title>Here is the sixth entry</title> 
    </category> 
</root> 

どういうわけか私のマップは間違っていますか?私は、恒等変換後に使用しなければならない新しいテンプレートを誤解していますか?

+1

あなたは、入力例を変換する期待される結果を投稿してもらえますか? –

答えて

1

title要素を変更する場合は、テンプレートはtitleと一致する必要があります。親要素ではないentryです。

<xsl:template match="title"> 
    <title> 
     <xsl:value-of select="$map/entry[@key = current()/../@id]"/> 
    </title> 
</xsl:template> 

また、あなたはtitle子に進む前entryの内容を再作成する必要があります:

<xsl:template match="entry"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <title> 
      <xsl:value-of select="$map/entry[@key = current()/@id]"/> 
     </title> 
    </xsl:copy> 
</xsl:template>