2013-09-05 2 views
0

XSLT로 분기하려고합니다.XSLT에서 올바르게 분기하는 방법?

XML 파일을 탐색하고 Section2, Section3 및 Section4와 일치하는 값을 검색하고 있습니다 (섹션 1과 5는 처리 목적으로 만 사용됨). . 모든 값이 일치하면 해당 Section5를 인쇄 한 다음 처리를 완료합니다.

문제는 3 개의 일치하는 섹션을 찾지 못하는 것입니다. 예를 들어 섹션 2와 섹션 3을 찾았지만 섹션 4가 아니라면 섹션 4로 돌아가서 값 '#'이있는 섹션을 검색해야합니다. Section2가 아닌 Section3을 찾으면 Section3 검색을 통해 '#'을 검색 한 다음 Secton4로 가서 올바른 값을 검색합니다 (찾지 못하면 '#'을 찾습니다).

위의 기능을 달성하는 방법을 모르겠습니다. 현재 모든 코드는 일치하는 값을 찾는 작업을 수행하고 있지만 위에서 설명한 파운드 경우는 처리하지 않으므로 모든 도움이 크게 감사하겠습니다.

아래에 XSLT 및 XML 섹션을 포함 시켰습니다.

<xsl:template match="Section1"> 
    <xsl:param name="sec2"/> 
    <xsl:param name="sec3"/> 
    <xsl:param name="sec4"/> 
    <xsl:apply-templates select="Section2[./@value=$sec2]"> 
     <xsl:with-param name="sec3" select="$sec3"/> 
     <xsl:with-param name="sec4" select="$sec4"/> 
    </xsl:apply-templates> 
</xsl:template> 
<xsl:template match="Section2"> 
    <xsl:param name="sec3"/> 
    <xsl:param name="sec4"/> 
    <xsl:apply-templates select="Section3[./@value=$sec3]"> 
     <xsl:with-param name="sec4" select="$sec4"/> 
    </xsl:apply-templates> 
</xsl:template> 
<xsl:template match="Section3"> 
    <xsl:param name="sec4"/> 
    <xsl:apply-templates select="Section4[./@value=$sec4]"/> 
</xsl:template> 
<xsl:template match="Section4"> 
    <xsl:apply-templates select="Content"/> 
</xsl:template> 
<xsl:template match="Section5"> 
    <!--Value will be printed here--> 
</xsl:template> 

<Section1> 
<Section2 value="AP"> 
    <Section3 value="JP"> 
     <Section4 value="true"> 
      <Section5>Content #1</Section5> 
     </Section4> 
     <Section4 value="false"> 
      <Section5>Content #2</Section5> 
     </Section4> 
    </Section3> 
    <Section3 value="KO"> 
     <Section4 value="true"> 
      <Section5>Content #3</Section5> 
     </Section4> 
     <Section4 value="false"> 
      <Section5>Content #4</Section5> 
     </Section4> 
    </Section3> 
    <Section3 value="#"> 
     <Section4 value="true"> 
      <Section5>Content #5</Section5> 
     </Section4> 
     <Section4 value="false"> 
      <Section5>Content #6</Section5> 
     </Section4> 
    </Section3> 
</Section2> 
<Section2 value="LA"> 
    <Section3 value="#"> 
     <Section4 value="true"> 
      <Section5>Content #7</Section5> 
     </Section4> 
     <Section4 value="false"> 
      <Section5>Content #8</Section5> 
     </Section4> 
    </Section3> 
</Section2> 
<Section2 value="NA"> 
    <Section3 value="#"> 
     <Section4 value="true"> 
      <Section5>Content #9</Section5> 
     </Section4> 
     <Section4 value="false"> 
      <Section5>Content #10</Section5> 
     </Section4> 
    </Section3> 
</Section2> 
<Section2 value="E"> 
    <Section3 value="#"> 
     <Section4 value="#"> 
      <Section5>Content #11</Section5> 
     </Section4> 
    </Section3> 
</Section2> 

+1

예를 들어''와 같이 일치 항목에 또는를 추가 할 수 있습니다. 소스 XML 및 예상 결과가 없으면 쉽지 않습니다. 가장 좋은 regarsd, Peter – Peter

+0

# 또는 그와 동등한 것이 모두 선택되지는 않을 것입니다. 즉, 값을 찾으면 값을 반환하고 #을 계속 찾습니다. 궁금해서 도와 줘서 고마워! –

+1

당신이 맞습니다. 그것은'Sectin2'와'#'와 일치 할 것입니다. 예제 XML을 사용하길 원하십니까 - 당신이하려는 것을보기가 더 쉽습니다. 감사합니다, 피터 – Peter

답변

1

나는 내 자신의 질문에 대답했다! 기본적으로 변수의 각 섹션을 래핑해야합니다. 값이 반환되는지 테스트하고 인쇄 할 경우 #을 찾습니다. 코드는 다음과 같습니다.

<xsl:template match="Section1"> 
    <xsl:param name="sec2"/> 
    <xsl:param name="sec3"/> 
    <xsl:param name="sec4"/> 
    <xsl:variable name="content"> 
     <xsl:apply-templates select="Section2[./@value=$sec2]"> 
      <xsl:with-param name="sec3" select="$sec3"/> 
      <xsl:with-param name="sec4" select="$sec4"/> 
     </xsl:apply-templates> 
    </xsl:variable> 
    <xsl:choose> 
     <xsl:when test="$content = ''"> 
      <xsl:apply-templates select="Section2[./@value='#']"> 
       <xsl:with-param name="sec3" select="$sec3"/> 
       <xsl:with-param name="sec4" select="$sec4"/> 
      </xsl:apply-templates> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy-of select="$content"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
<xsl:template match="Section2"> 
    <xsl:param name="sec3"/> 
    <xsl:param name="sec4"/> 
    <xsl:variable name="content"> 
     <xsl:apply-templates select="Section3[./@value=$sec3]"> 
      <xsl:with-param name="sec4" select="$sec4"/> 
     </xsl:apply-templates> 
    </xsl:variable> 
    <xsl:choose> 
     <xsl:when test="$content = ''"> 
      <xsl:apply-templates select="Section3[./@value='#']"> 
       <xsl:with-param name="sec4" select="$sec4"/> 
      </xsl:apply-templates> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy-of select="$content"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
<xsl:template match="Section3"> 
    <xsl:param name="sec4"/> 
    <xsl:variable name="content"> 
     <xsl:apply-templates select="Section4[./@value=$sec4]"/> 
    </xsl:variable> 
    <xsl:choose> 
     <xsl:when test="$content = ''"> 
      <xsl:apply-templates select="Section4[./@value='*']"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy-of select="$content"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
<xsl:template match="Section4"> 
    <xsl:apply-templates select="Content"/> 
</xsl:template> 
<xsl:template match="Section5"> 
    <!--Value will be printed here--> 
</xsl:template> 

도움을 주셔서 감사합니다!

+0

+1; 안부 인사, 피터 – Peter

관련 문제