2014-08-29 42 views
1

我有一個關於在我正在處理的項目上重構som XSLT代碼的問題。在XSLT我現在我該如何重構這個XSLT

<pdf:sometext text="{$metadata/namespace:template 
        /namespace:template_collection 
        //namespace:option 
        [namespace:identificator 
         = $report_option_identifier] 
        /namespace:name}"/> 

的問題是,XSLT需要擴展打更多的節點(在原著XML的新版本,以略低改變了命名空間和略有改變標籤)。

我想出了這個代碼:

<xsl:variable name="report_template_collection" 
       select="$metadata 
         /namespace:template 
         /namespace:template_collection 
         | 
         $metadata 
         /namespace2:templateV2 
         /namespace2:template_collectionV2" /> 
<xsl:variable name="current_report_option" 
       select="namespace:option | namespace2:optionV2" /> 
<xsl:variable name="incomplete_report_option_text" 
       select="$report_template_collection 
         //$current_report_option 
         [namespace:identificator 
         = $report_option_identifier] 
         /namespace:name"/> 

<pdf:sometext text="{$incomplete_report_option_text}"/> 

但是在編譯時它帶有的部份錯誤:

Unexpected token '$' in the expression. $report_template_collection// -->$<-- current_report_option[namespace:opt...

所以我的問題是:我如何重構XSLT佔新的命名空間(命名爲V2)和另一個命名空間。重要的是相同的XSLT符合XML的所有版本(包括舊版本和新版本)。

在此先感謝!

+3

你的問題是相當無法回答的,因爲它是現在。所有這些變量的目的是什麼?您提到了不同版本的XML輸入,但不顯示任何內容。我建議您按照以下順序編輯您的問題並顯示以下內容:1個句子,解釋您的目標是什麼,出了什麼問題,帶有問題的_full_,最小XSLT樣式表,XML輸入以及您期望的XML輸出。 – 2014-08-29 09:43:31

+0

錯誤消息告訴你,其中一個'select'屬性的值不是XPath表達式。你的問題出現在表達部分......'// current_report_option' ... - 因爲只有你知道你在那裏說什麼,只有你可以修復它。 – 2014-08-29 14:35:13

回答

0

對於xpath表達式中所做的事情,您不能引用$current_report_option。您不能像使用宏那樣使用XSLT變量,這就是您嘗試執行的操作。 $current_report_option的類型是一個節點集。

如果我正確地解釋你的意圖,你如何試圖用(錯誤地)$current_report_option,你應該這樣做以下代替:

<xsl:variable name="incomplete_report_option_text" 
       select="$report_template_collection 
         //*[self::namespace:option or 
          self::namespace2:optionV2] 
         [namespace:identificator 
         = $report_option_identifier] 
         /namespace:name"/> 

我換成你的$current_report_option使用與節點測試檢查舊的或新的選項節點類型。

1

形式爲A/B/$C的XPath表達式在XPath 2.0中實際上是合法的,但在XPath 1.0中不合法。但它可能並不意味着你的想法。像@ewh我懷疑(沒有任何證據),你在想象如果變量$C綁定到表達式D|E,那麼A/B/$C是另一種寫作方式A/B/(D|E)。情況並非如此; $C綁定到一個值(一系列節點),而不是一個表達式。

你可以使用一個函數,而不是一個變量:

<xsl:function name="f:current_report_option"> 
    <xsl:param name="node" as="node()"/> 
    <xsl:sequence select="$node/(D|E)"/> 
</xsl:function> 

<xsl:variable name="X" select="A/B/f:current_report_option(.)"/>