2016-08-17 3 views
0

자식 요소 변경 : 각 항목 요소에 대해XSLT :지도를 사용하여 내가 XML 파일이

<?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 특성을 기반으로 제목 자식 요소의 값을 변경하고 싶습니다. 먼저지도를 정의하는 다음 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 요소

을 변경하는 새 템플릿을 생성 변환 수행하지만이 제목 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

당신이 입력 예 변환의 예상 결과를 게시 할 수 없습니다 :

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

또는, title 아이로 진행하기 전에 entry의 내용을 다시해야 할 것? –

답변

1

title 요소를 수정하려는 경우 템플릿은 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>