2014-02-13 50 views
0

簡單的問題,但很難解釋...XSL變換XPATH字符串中的變量 - 比較

我在一個xsl轉換中的節點內。該節點有兩個信息。一個國家和一年 此節點之外有很多其他節點具有相同的架構。我希望與現在的國家共事多年。

下面的示例可以包含錯別字:

<prices> 
    <price ta:id="RPMA12875162"> 
     <context> 
      <country-ext>US</country-ext> 
      <year>2016</year> 
     </context> 
     <price> 
      <value>10</value> 
     </price> 
    </price> 
</prices> 
<prices> 
    <price ta:id="RPMA12875163"> 
     <context> <!-- lets say the transformation is here --> 
      <country-ext>US</country-ext> 
      <year>2014</year> 
     </context> 
     <price> 
      <value>10</value> 
     </price> 
    </price> 
    <price ta:id="RPMA12875162"> 
     <context> 
      <country-ext>DE</country-ext> 
      <year>2013</year> 
     </context> 
     <price> 
      <value>9</value> 
     </price> 
    </price> 
    <price ta:id="RPMA12875163"> 
     <context> 
      <country-ext>DE</country-ext> 
      <year>2014</year> 
     </context> 
     <price> 
      <value>10</value> 
     </price> 
    </price> 
    <price ta:id="RPMA12875164"> 
     <context> 
      <country-ext>DE</country-ext> 
      <year>2015</year> 
     </context> 
     <price> 
      <value>11</value> 
     </price> 
    </price> 
</prices> 

繼清理XSL

<xsl:template match="context"> 
    <DimensionProperties> 
     <xsl:if test="country-ext and year" > 
      <xsl:variable name="currentCountryExt1" select="concat(string(country-ext/text()), '')"/> 
      <xsl:variable name="currentCountryExt2" select="'US'"/> 

      <xsl:variable name="yearOfEepartPrices1" select="../../price/context[string(country-ext/text()) = $currentCountryExt1]/year"/> 
      <xsl:variable name="yearOfEepartPrices2" select="../../price/context[string(country-ext/text()) = $currentCountryExt2]/year"/> 
     </xsl:if> 
    </DimensionProperties> 
</xsl:template> 

而硬編碼的變量currentCountryExt2工作時,currentCountryExt1沒有。在調試時,我可以看到1和2是值爲US的xs:string。 我試着像contains()或不帶concat(string())的函數一樣。每次我得到相同的結果:什麼都沒有。

yearOfEepartPrices1工作不

yearOfEepartPrices2工作

什麼想法?


答案評論:

我Concat的與「」,因爲我發現了一個論壇,讓別人有同樣的問題像我這樣的暗示。他用這個小「竅門」解決了這個問題。

我使用氧氣作爲調試和XSL後使用輸出在那裏我可以驗證這一點的不當行爲

+0

我同意伊恩你應該使用**鍵**。也就是說,我不明白你是如何確定'$ yearOfEepartPrices1'不起作用的。我相信你錯了。我建議你輸出這兩個變量的「副本」,並且看到它們之間沒有區別(只要硬編碼的國家與實際值匹配)。順便說一句,你爲什麼用一個空字符串「'」串聯一個字符串(國家)? –

+1

作爲對您編輯的迴應:「* xsl稍後使用可以驗證這種不正當行爲的輸出*」我們只能通過您在此處發佈的內容進行操作。如果我從輸入中刪除未聲明的名稱空間,然後將所有變量複製到結果樹中,則兩者之間沒有區別 - 請參閱:http://xsltransform.net/nbUY4ko/1 –

+0

正如您提到的oXygen,是否使用XSLT 2.0或1.0?使用XSLT 2.0,僅將密鑰應用於子樹例如''和'key('priceByCountry',country-ext,祖先/價格)/ context/year'。 –

回答

0

你正在做的事情的方式複雜得多,他們需要的是在這裏。對於XML結構您在問題中給出的以下應很好地工作:

<xsl:template match="context"> 
    <DimensionProperties> 
     <xsl:if test="country-ext and year" > 
      <xsl:variable name="yearOfEepartPrices" 
        select="../../price/context[ 
         country-ext = current()/country-ext]/year"/> 
     </xsl:if> 
    </DimensionProperties> 
</xsl:template> 

您應該避免使用text(),除非你真的需要分別處理每個單獨的文本節點 - 如果你想要的是一個完整的字符串給定元素的值然後只選擇元素而不是它的一組文本節點子元素。


編輯:原來的問題是不明確的,有幾種不同的prices節點,下面的建議是有道理的,如果你想找到整個文檔中的所有匹配year元素。

我想要與現在的國家的所有年份。

最簡單的方法是定義一個鍵。

<xsl:key name="yearByCountry" match="year" use="../country-ext" /> 

,然後你可以提取所有

key('yearByCountry', $theCountryCode) 

匹配某個國家例如year元素:

<xsl:template match="context"> 
    <DimensionProperties> 
     <xsl:if test="country-ext and year" > 
      <xsl:variable name="yearOfEepartPrices" 
        select="key('yearByCountry', country-ext)"/> 
     </xsl:if> 
    </DimensionProperties> 
</xsl:template> 

或者,它可能會更有意義有按鍵返回price元素

<xsl:key name="priceByCountry" match="price" use="context/country-ext" /> 

<xsl:template match="context"> 
    <DimensionProperties> 
     <xsl:if test="country-ext and year" > 
      <xsl:variable name="yearOfEepartPrices" 
        select="key('priceByCountry', country-ext)/context/year"/> 
     </xsl:if> 
    </DimensionProperties> 
</xsl:template> 
+0

你好,威爾看起來相當不錯,但我忘了說價格不是根。在XML中有很多價格。你目前的鑰匙找到所有 –