2012-03-20 60 views
2

如果有一個大的'xsl:choose'塊,我需要在不同的元素上設置一些定義的屬性集。使用變量來存儲和輸出屬性

我真的不喜歡重複'選擇'的每個分支內的屬性集的定義。 所以我想使用包含這些屬性的變量。 很容易維護和減少出錯的空間...

到目前爲止我還沒有能夠調用屬性節點? 我以爲他們只是一個節點集,所以複製就可以做到。
但是,這並沒有給我輸出。
這是因爲屬性節點不是真的孩子嗎?
但XSLT 1.O不允許我直接解決這些問題...... <xsl:copy-of select="$attributes_body/@*/>返回一個錯誤

這裏是樣式表片段(由原來的減少)

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="list"> 
    <xsl:for-each select="figure"> 
    <xsl:variable name="attributes_body"> 
    <xsl:attribute name="chapter"><xsl:value-of select="@chapter"/></xsl:attribute> 
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute> 
    </xsl:variable> 
    <xsl:variable name="attributes_other"> 
     <xsl:attribute name="chapter"><xsl:value-of select="@book"/></xsl:attribute> 
     <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute> 
    </xsl:variable> 
    <xsl:choose> 
     <xsl:when test="ancestor::body"> 
      <xsl:element name="entry"> 
      <xsl:copy-of select="$attributes_body"/> 
      <xsl:text>Body fig</xsl:text> 
      </xsl:element> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:element name="entry"> 
      <xsl:copy-of select="$attributes_other"/> 
      <xsl:text>other fig</xsl:text> 
      </xsl:element> 
     </xsl:otherwise> 
    </xsl:choose>   
    </xsl:for-each>  
</xsl:template> 

如果不能完成XLST 1.0將能夠做到這一點?

+0

這不完全清楚,我試圖達到的目標。您是否希望將屬性中的輸入數據存儲(並稍後檢索)到變量中?如果是這樣,輸入XML/html是什麼?而且,如果是這樣,您可能只想使用xsl:變量而不是xsl:template。而你使用的嵌套xsl:變量是錯誤的。所以請添加輸入和期望輸出加上方法wrt變量存儲+檢索,那麼我們可以更好地爲您提供建議。 – Maestro13 2012-03-20 12:44:41

+0

@ Maestro13打算創建文檔中所有'figure'元素的列表。根據各種條件,列表將包含具有預設屬性集的元素。這些屬性的值將取決於源文檔中找到的每個「圖」元素。我想可能會讓你感到困惑的是我重新發布了另一個數字元素。我編輯了原始樣式表,以便發出具有不同名稱的元素。 – 2012-03-20 13:35:42

+0

@ Maestro13由於我不想重複一遍又一遍的屬性集,我想將這些屬性集存儲在不同的變量中。我希望它能讓你更清楚一點。 – 2012-03-20 13:39:09

回答

4
<xsl:variable name="attributes_body"> 
    <xsl:attribute name="chapter"><xsl:value-of select="@chapter"/></xsl:attribute> 
    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute> 
</xsl:variable> 

您需要選擇想要的屬性 - 不是它們的內容在變體複製。

記住:只要有可能,儘量始終處於xsl:variableselect屬性來指定一個XPath表達式 - 避免在它的身上覆制的內容。

解決方案

只需使用

<xsl:variable name="attributes_body" select="@chapter | @id"> 

下面是一個完整的例子

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <xsl:template match="x/a"> 
     <xsl:variable name="vAttribs" select="@m | @n"/> 

     <newEntry> 
     <xsl:copy-of select="$vAttribs"/> 
     <xsl:value-of select="."/> 
     </newEntry> 
    </xsl:template> 
</xsl:stylesheet> 

當此應用:

<x> 
<a m="1" n="2" p="3">zzz</a> 
</x> 

產生

<newEntry m="1" n="2">zzz</newEntry> 
+0

Novtchev:屬性集不會悲傷不會飛。存儲在屬性中的值必須從for-each循環中的選定節點讀取。由於屬性集是一個頂級元素,我不知道你會如何做到這一點。 以我的示例中的@id屬性爲例。它的值應該是for-each循環中所選'figure'節點的ID – 2012-03-20 13:41:25

+0

@HafLinger:我編輯了我的答案 - 這很簡單。 – 2012-03-20 13:49:00

+0

嗯......以及如何將這個輸出作爲位於列表中的「入口」節點上的兩個不同屬性輸出? 我真的想要輸出像' body fig ' – 2012-03-20 13:54:56

1
在我的情況

,我是一個標籤attribute試圖storevariable

這樣做,使用此語法tag-name/@attribute-inside-tag-name

這裏是一個例子

<xsl:variable name="articleLanguage" select="/article/@language"/><!--the tricky part --> 
//<!--now you can use this this varialbe as you like --> 
<xsl:apply-templates select="front/article-meta/kwd-group[@language=$articleLanguage]"/> 

和XML是

<article article-type="research-article" language="es" explicit-lang="es" dtd-version="1.0"> 
..... 

希望這有助於你