簡體   English   中英

使用XSL變量獲取值(XPath)-xslt

[英]Get Value(XPath) using XSL Variable - xslt

嘗試解析以下xml

<root>
<SelectValue>One</SelectValue> <!-- Tends to Vary -->
<SubRoot>  <!-- Iterate elements inside it using [ SelectValue] -->
    <One>One</One>
    <Two>Two</Two>
    <Three>Three</Three> 
</SubRoot>
</root> 

低於xsl

    <xsl:template match="/root">
        <xsl:variable name="columns" select="SelectValue"/>
        <xsl:for-each select="SubRoot"> -->
                        <tr>
                            <td>
                                <xsl:value-of select="SubRoot/@*[local-name()=$columns]"/>
                            </td>
                        </tr>

  </xsl:for-each>

檢索空的html-tags而我期望如下所示,

 <table>
   <thead>
   <th>One</th>
   </thead>
   <tbody>
    <tr>
      <td>one</td>
    </tr>
   <tbody>
</table>

我試圖在<SelectValue>內部傳遞值以獲取<SubRoot>內部的節點

這里有什么想法嗎?

我無法識別您引用的錯誤消息(它甚至似乎都不是英語),但是在嘗試運行您的代碼時確實出現了錯誤。 原因是您的變量名無效; 您需要更改:

<xsl:variable name="$columns" select="..."/>

至:

<xsl:variable name="columns" select="..."/>

引用變量時使用$前綴,而不是在定義變量時使用。


還要注意,以/開頭的XPath表達式是絕對路徑 ,從根節點開始。 因此,除了/root外,所有select表達式都不會選擇任何內容。 我猜您正在嘗試執行以下操作:

<xsl:template match="/root">
    <xsl:variable name="columns" select="SelectValue"/>
    <tr>
        <td>
            <xsl:value-of select="SubRoot/*[local-name()=$columns]"/>
        </td>
    </tr>
</xsl:template>

給定您的輸入示例將返回:

<tr>
  <td>One</td>
</tr>

在xsl:for-each select =“ SubRoot”中,SubRoot是上下文節點,因此您不應選擇其他SubRoot。 所以你要

<xsl:for-each select="SubRoot">
     <tr>
          <td>
              <xsl:value-of select="@*[local-name()=$columns]"/>
          </td>
    </tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM