xslt 將變量發送到另一個模板 (xslt send variable to another template)


問題描述

xslt 將變量發送到另一個模板 (xslt send variable to another template)

I have an xml example like this:

<p class="exer_header" style="display: none;">       
    <image‑input size="5" value="val1" />
</p>
<p class="exer_header" style="display: none;">       
    <image‑input size="5" value="val2" />
</p>
<answers‑img>
    <answer‑img class="imagednd_answer1" value="val1"/>
    <answer‑img class="imagednd_answer2" value="val2"/>
</answers‑img>

and XSLT ex. here:

<xsl:template match="image‑input">
    <xsl:variable name="id" select="generate‑id(.)"/>
    <xsl:element name="input">
        <xsl:attribute name="id"><xsl:value‑of select="$id"/></xsl:attribute>
        <xsl:attribute name="class">exer_input</xsl:attribute>
    </xsl:element>
</xsl:template>
<xsl:template match="answers‑img">
       <xsl:for‑each select="//image‑input"> 
            <xsl:element name="div">
                <xsl:element name="input">
                    <xsl:attribute name="class">ans_img_input</xsl:attribute>
                    <xsl:attribute name="type">hidden</xsl:attribute>
                    <xsl:attribute name="value">***{ID}***</xsl:attribute>
            </xsl:element>

                <xsl:apply‑templates select="//answers‑img/answer‑img"/>                
            </xsl:element> 
      </xsl:for‑each>
</xsl:template>

Question is next, How can I send a variable id from "input" template into another "answers‑img" template and change {ID}?  

UPD: In "answer‑img" I need the same id's that generates in "input‑img". First xslt generate code with "input‑img"(twice) and when somewhere another templates, not in "input‑img", call template "answer‑img". Maybe I can create global array variable?


參考解法

方法 1:

Use xsl:with‑param

I don't know where you want to call the template but it can be done like this:

<xsl:call‑template name="answers‑img"><xsl:with‑param name="id" select="$id" /></xsl:call‑template>

You must add this to the calling template:

<xsl:param name="id" />

方法 2:

For more details how to work with call‑template and param you may visit ‑http://www.w3schools.com/xsl/el_with‑param.asp

方法 3:

Looks like your answers‑img  template is looping over image‑input and you like to have the same id as is used in image‑input template. In this case you can create the id the same way as you did in image‑input template.

<xsl:for‑each select="//image‑input"> 
     <xsl:variable name="id" select="generate‑id(.)"/>
 .....
<xsl:attribute name="value"><xsl:value‑of select="$id"/></xsl:attribute>

This will work because generate‑id() will allways create the same value for the same element.

Update addressing the concrete question:

How can I send a variable id from "input" template into another "answers‑img" template and change {ID}?

Answer: You can't At least not in your example code. It is only possible to pass a value from an caller (template) to an called (template.)

(by imCapsPeter BartelsNavin Rawathr_117)

參考文件

  1. xslt send variable to another template (CC BY‑SA 3.0/4.0)

#Variables #templates #xslt






相關問題

使用 htaccess 從基本 URL 中刪除變量 (Remove variable from base URL with htaccess)

如何將解碼的數據包數據和標頭輸出組合到單個變量/字典中以進行字符串搜索 (How to combine decoded packet data and header output into a single variable/dictionary for string searches)

@Ruby on Rails 中的變量 (@ variables in Ruby on Rails)

xslt 將變量發送到另一個模板 (xslt send variable to another template)

變量被評估為任何東西 (Variable Being Evaluated as Anything)

獲取python函數的變量 (Get a variable of python function)

讀取 url JQuery 中的 GET 變量 (read the GET variables in url JQuery)

使用變量名作為數組索引 (Using variable name as array index)

Javascript PHP var數組將雙引號放入 (Javascript PHP var array puts double quotes in)

兩個不同定義之間的變量 (Variable between two different definitions)

Tkinter 組合框 Python (Tkinter Combobox Python)

有沒有辦法在 Python 中使用變量中的字符串調用方法? (Is there a way to call a method in Python using a string inside a variable?)







留言討論