2011-08-23 170 views
5

如果我有這樣的XSLXSL命名空間問題

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html"/> 
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/> 

    <xsl:template match='/'> 
     <xsl:value-of select="//Description" /> 
    </xsl:template> 
</xsl:stylesheet> 

而這個XML

<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <LookupValue> 
    <Description>AGL</Description> 
    <Value>8</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>Australian Power &amp; Gas</Description> 
    <Value>6</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>EnergyAustralia</Description> 
    <Value>13</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>Origin Energy</Description> 
    <Value>9</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>TRU Energy</Description> 
    <Value>7</Value> 
    </LookupValue> 
</ArrayOfLookupValue> 

如何真正從該行得到一些數據:

<xsl:value-of select="//Description" /> 

我花了幾個小時在這一點上,我已經得出結論,xmlns =命名空間是什麼導致我的悲傷。

任何幫助非常感謝。

者均基於XML是從Web服務來,所以我不能只是「改變」 - 我可以是進行預處理,但不是理想......

我也已經證實,去除的命名空間XML的模擬確實解決了這個問題。

+0

好問題,+1。請參閱我的回答以獲得解釋和簡短而簡單的解決方案。 –

+0

你說得對,命名空間是有差別的。請參閱Dimitre的解釋......如果您閱讀了XML名稱空間(特別是在XPath中使用的),您將真的節省自己的時間。 – LarsH

回答

12

這是XPath和XSLT的最常見問題解答

簡短回答是在XPath中,前綴名被認爲屬於「no namespace」。但是,在具有默認名稱空間的文檔中,前綴名稱屬於默認名稱空間。

因此,對於這樣的原稿的表達

//Description 

選擇什麼(因爲沒有Description(或文檔中的任何其他)元素屬於「沒有命名空間」 - 所有元件名稱屬於默認名稱空間)。

解決方案

定義在XSLT具有相同namespace-uri()作爲XML文檔的默認命名空間的命名空間。然後使用這樣定義的命名空間的前綴中的XPath表達式中使用的任何名稱:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://switchwise.com.au/"> 
    <xsl:output method="html"/> 
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8"/> 

    <xsl:template match='/'> 
     <xsl:copy-of select="//x:Description" /> 
    </xsl:template> 
</xsl:stylesheet> 

當該變換被應用到所提供的XML文檔

<ArrayOfLookupValue xmlns="http://switchwise.com.au/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <LookupValue> 
    <Description>AGL</Description> 
    <Value>8</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>Australian Power &amp; Gas</Description> 
    <Value>6</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>EnergyAustralia</Description> 
    <Value>13</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>Origin Energy</Description> 
    <Value>9</Value> 
    </LookupValue> 
    <LookupValue> 
    <Description>TRU Energy</Description> 
    <Value>7</Value> 
    </LookupValue> 
</ArrayOfLookupValue> 

有用,正確結果產生

<Description xmlns="http://switchwise.com.au/" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
>AGL</Description> 
<Description xmlns="http://switchwise.com.au/" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
>Australian Power &amp; Gas</Description> 
<Description xmlns="http://switchwise.com.au/" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
>EnergyAustralia</Description> 
<Description xmlns="http://switchwise.com.au/" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
>Origin Energy</Description> 
<Description xmlns="http://switchwise.com.au/" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 
>TRU Energy</Description> 
+0

+1好答案。您如何將問題標記爲常見問題解答? – LarsH

+0

@LarsH:我認爲沒有辦法將問題標記爲常見問題 - 目前常見問題取決於他們的觀點數量 - 實際上是「觀看次數最多」。而且我們需要的不是常見問題解答蝙蝠FAT - 常見問題:) –

+0

大聲笑 - 這很容易,當你知道如何!非常感謝您提供了這樣一個簡單易懂的答案,我已將這些更改放入我的XSL中,並且按照希望工作。再次感謝!! – Rob