2009-11-19 85 views
0

我正在嘗試從我的xml文檔中提取特定節點,但繼續從所有節點獲取內容。我正在測試一個特定的jNumber,通過將該數字傳遞給使用片段調用($ jNumber)設置的變量中的xslt。僅從一個節點提取內容

<xsl:if test="products/product/jNumber[. = $jNumber]"> 
    <div class="floatLeft padTop10 margLeft10" style="width:185px;"> 
     <xsl:for-each select="products/product/topicFocus"> 
      <div id="tab{./@tab}focus" class="linksUnit padBtm3" style="display:block;"><xsl:text>» </xsl:text><a href="#" class="bold" target="_blank"><em><xsl:value-of select="./@name" /></em></a></div> 
      <div id="tab{./@tab}ready" class="linksUnit padBtm3" style="display:none;"><xsl:text>» </xsl:text><a href="#" target="_blank"><xsl:value-of select="./@name" /></a></div> 
     </xsl:for-each> 
     <xsl:for-each select="products/product/topicLeft"> 
      <div id="tab{./@tab}focus" class="linksUnit padBtm3" style="display:none;"><xsl:text>» </xsl:text> <a href="#" class="bold" target="_blank"><em><xsl:value-of select="./@name" /></em></a></div> 
      <div id="tab{./@tab}ready" class="linksUnit padBtm3" style="display:block;"><xsl:text>» </xsl:text><a href="#" target="_blank"><xsl:value-of select="./@name" /></a></div> 
     </xsl:for-each> 
    </div> 
</xsl:if> 

這是我的理解是,if語句只會導致測試真正的節點被顯示。真的嗎?

<products> 
    <product> 
     <jNumber>1234</jNumber> 
     <name>Product #1</name> 
     <numberoftabs>12</numberoftabs> 
     <!-- Links at top of the page --> 
     <topicFocus tab="1" name="Accessories"></topicFocus> 
     <!--topicLeft tab="2" name="Configuration examples"></topicLeft--> 
     <topicLeft tab="2" name="Resources (white papers, datasheets, etc.)"></topicLeft> 
     <topicLeft tab="3" name="Download software"></topicLeft> 
     <topicLeft tab="4" name="FAQs"></topicLeft> 
     <topicLeft tab="5" name="Interoperability"></topicLeft> 
     <!--topicLeft tab="6" name="MIBs"></topicLeft--> 
     <topicRight tab="6" name="Technical documentation"> 
      <subCategory tab="7">Management and Configuration</subCategory> 
      <subCategory tab="8">Archived</subCategory> 
     </topicRight> 
     <topicRight tab="9" name="Related links"></topicRight> 
     <topicRight tab="10" name="Support form"></topicRight> 
    </product> 
</products>  

回答

2

的的xsl:for-每個嵌套在xsl:如果,但換每個不被如果限制。

我沒有看到jNumber在您提供的XML中,但是您可以將for-each方面設置爲與封閉方式相同的限制,以選擇是否顯示該區域。上述

<xsl:for-each select="products/product[jNumber = $jNumber]/topicFocus"> 

行說: 開始與當前範圍看產品 - 具有產品 - 其中有等於$ jNumber一個jNumber - 然後看看topicFocus節點。

1

Jason Aller的答案肯定是正確的,但我認爲你有更多的機會來改進XSLT。無論你擁有的XPath,如:

select="products/product/topicFocus" 

你正在尋找所有products元素是上下文節點的子項(即您的模板正在改變,或者說你的for-each循環正在位於元素),然後他們的product兒童,以及其後的任何topicFocus兒童。這要麼找到很多節點(如果上下文節點是您的products元素的父節點),要麼根本沒有節點(如果上下文節點是product元素,除非您的product元素有products子元素)。

如果您使用模板作爲主要工具,使用XSLT會更容易。模板將節點轉換爲結果。在你的情況下,你想要將product節點轉換成div。您還希望將topicFocustopicLeft元素轉換爲div s對。

如果你的模板的設計反映了,它看起來像這樣:

<!-- 
    this, somewhere in the main template, finds the specific product 
    element you're looking for and transforms it. 
--> 
<xsl:apply-templates select="products/product[jNumber = $jNumber]/> 

<xsl:template match="product"> 
    <div class="floatLeft padTop10 margLeft10" style="width:185px;"> 
     <xsl:apply-templates select="topicFocus"/> 
     <xsl:apply-templates select="topicLeft"/> 
    </div> 
</xsl:template> 

<xsl:template match="topicFocus | topicLeft"> 
    <xsl:variable name="display1"> 
    <xsl:choose> 
     <xsl:when test="name() = 'topicFocus'>none</xsl:when> 
     <xsl:otherwise>block</xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 
    <xsl:variable name="display2"> 
    <xsl:choose> 
     <xsl:when test="name() = 'topicFocus'>block</xsl:when> 
     <xsl:otherwise>none</xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 
    <div id="tab{@tab}focus" class="linksUnit padBtm3" style="display:{$display1};"> 
    <xsl:text>» </xsl:text> 
    <a href="#" class="bold" target="_blank"> 
     <em> 
     <xsl:value-of select="@name" /> 
     </em> 
    </a> 
    </div> 
    <div id="tab{@tab}ready" class="linksUnit padBtm3" style="display:{$display2};"> 
    <xsl:text>» </xsl:text> 
    <a href="#" target="_blank"> 
     <xsl:value-of select="@name" /> 
    </a> 
    </div>           
</xsl:template> 

我已經簡化了幾個其他的東西在這裏,太:

  • 因爲在我看來好像除了設置display樣式外,您的轉換所發送的HTML元素topicFocustopicLeft是相同的,我爲它們創建了單個模板。
  • 我在模板中使用了空格和縮進,以便HTML的結構很明顯;這將使維護它變得更容易,並且不會影響變換如何產生輸出。
  • 我已經刪除了所有屬性前的冗餘./