2013-09-05 26 views
0

我想在XSLT中進行分支。下面是我試圖做的事情:如何在XSLT中正確分支?

我遍歷XML文件並搜索與第2節,第3節和第4節匹配的值(第1節和第5節僅用於處理目的) 。如果所有的值都匹配,我會打印出相應的Section5,然後進行處理。

我遇到的問題是,如果我找不到三個匹配的部分,例如,如果我找到Section2和Section3,但不是Section4,我需要返回到Section4並搜索值爲'#'的部分。如果我找到第2部分而不是第3部分,我將回到第3部分搜索'#',然後我將去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

+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

相關問題