2014-12-02 45 views
0

我有一些問題需要訪問我想要的節點。xsl選擇具有子值的節點並將其作爲參數傳遞

這裏是我的簡單的xml:

<chain> 
    <components> 
     <component> 
      <place>2</place> 
      <name>bbb</name> 
     </component> 
     <component> 
      <place>1</place> 
      <name>aaa</name> 
     </component> 
     <component> 
      <place>3</place> 
      <name>ccc</name> 
     </component> 
    </components> 
</chain> 

輸出我想要的:

aaa is connected to bbb 
bbb is connected to ccc 

和我的xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" indent="yes"/> 
<xsl:template match="text()"/> 

<xsl:template name="writeComponent"> 
    <xsl:param name="my_component"/> 
    <xsl:value-of select="$my_component/name"/> 
</xsl:template> 

<xsl:template match="/chain/components"> 
     <xsl:for-each select="component"> 
      <xsl:variable name="nextPlace" select="./place + 1"/> 

      <xsl:call-template name="writeComponent"> 
       <xsl:with-param name="my_component" select='component'/> 
      </xsl:call-template> 
      is connected to 
      <xsl:call-template name="writeComponent"> 
       <xsl:with-param name="my_component" select="component[place = '$nextPlace']"/> 
      </xsl:call-template> 

     </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

我需要調用twite不同相同的模板節點參數

得到任何幫助,感謝

編輯:我發現我的問題的解決方案

來電:

<xsl:call-template name="writeComponent"> 
    <xsl:with-param name="component_param" select="."/> 
</xsl:call-template> 
is connected to 
<xsl:call-template name="writeComponent"> 
    <xsl:with-param name="component_param" select="/chain/components/component[place = $nextPlace]"/> 
</xsl:call-template> 

,並在所謂的模板,你需要循環在<xsl:for-each select="$component_param">

+0

您能添加您的預期輸出嗎?問題不是很清楚,我認爲,當它已經在組件上循環時,只有1個呼叫模板就足夠了。 – 2014-12-02 09:59:03

+0

我添加了所需的輸出,而我的問題是我無法通過節點作爲參數 – 2014-12-02 11:06:24

+1

你能請求轉換背後的**邏輯**?我沒有看到'aaa是如何連接到bbb'的,除了兩個是兄弟姐妹(但爲什麼不是'aaa連接到ccc'?)。 – 2014-12-02 12:11:39

回答

0

select="component[place = '$nextPlace']中,您需要select="component[place = $nextPlace]才能正確引用變量。但是,由於for-each進程component元素,我看不到<xsl:with-param name="component" select='component'/>可以在您的示例XML中選擇任何東西。模板匹配,for-each和調用模板的整體組合似乎有問題,您可能想向我們展示您想要的輸入樣本的結果,然後我們可以希望提供一個更清晰和更簡單的XSLT方法。

+0

我添加了所需的輸出,我的問題是我無法傳遞一個節點作爲參數 – 2014-12-02 11:10:40