2013-01-13 33 views
0

可能重複:
XSLT with XML source that has a default namespace set to xmlns無法獲得預期的XSL轉換結果

我的目標是在一個XML OpenSearch的結果進行簡單的XSL轉換。

<?xml version="1.0"?> 
<SearchSuggestion version="2.0" xmlns="http://opensearch.org/searchsuggest2"> 
    <Query xml:space="preserve">wireframes</Query> 
    <Section> 
     <Item> 
      <Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Wireframes.png/50px-Wireframes.png" width="50" height="20" /> 
      <Text xml:space="preserve">File:Wireframes.png</Text> 
      <Description xml:space="preserve">The original description page is/was [http://en.wikipedia.org/w/index.php?title=Image%3AWireframe.png here]. </Description> 
      <Url xml:space="preserve">http://commons.wikimedia.org/wiki/File:Wireframes.png</Url> 
     </Item> 
    </Section> 
</SearchSuggestion> 

我現在的XSL文件:我一直在使用這個例子,從一個簡單的Wikimedia Commons Search獲得

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/>  
    <xsl:template match="/"> 
     <pictures> 
      <xsl:for-each select="//Item"> 
       <picture> 
        <xsl:attribute name="text"> 
         <xsl:value-of select="Text/text()"/> 
        </xsl:attribute> 
        <xsl:attribute name="url"> 
         <xsl:value-of select="Image/@source"/> 
        </xsl:attribute> 
        <xsl:attribute name="width"> 
         <xsl:value-of select="Image/@width"/> 
        </xsl:attribute> 
        <xsl:attribute name="height"> 
         <xsl:value-of select="Image/@height"/> 
        </xsl:attribute> 
        <xsl:attribute name="description"> 
         <xsl:value-of select="Description/text()"/> 
        </xsl:attribute> 
        <xsl:attribute name="entry_url"> 
         <xsl:value-of select="Url/text()"/> 
        </xsl:attribute> 
       </picture> 
      </xsl:for-each> 
     </pictures> 
    </xsl:template> 
</xsl:stylesheet> 

但它發生,無論我怎麼更改xs:for-each XPath表達式,所有在這樣的XML文檔上應用XSLT時(我在V​​isual Studio 2010中測試這個)時得到的結果是空的<pictures/>,就好像任何出現的Item元素都被忽略了一樣。我究竟做錯了什麼?

+0

爲什麼你使用'換each'代替模板? – Oded

+0

搜索「XPath默認命名空間」 - 這是最常見的XPath問題,並有許多很好的答案。 –

回答

0

在製作XSLT時,我當然錯過了命名空間的整個概念。將xsml:my="http://opensearch.org/searchsuggest2"添加到樣式表中,並在所需元素上使用my:前綴修復了問題。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:my="http://opensearch.org/searchsuggest2" 
       exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
     <pictures> 
      <xsl:for-each select="//my:Item"> 
       <picture> 
        <xsl:attribute name="text"> 
         <xsl:value-of select="my:Text/text()"/> 
        </xsl:attribute> 
        <xsl:attribute name="url"> 
         <xsl:value-of select="my:Image/@source"/> 
        </xsl:attribute> 
        <xsl:attribute name="width"> 
         <xsl:value-of select="my:Image/@width"/> 
        </xsl:attribute> 
        <xsl:attribute name="height"> 
         <xsl:value-of select="my:Image/@height"/> 
        </xsl:attribute> 
        <xsl:attribute name="description"> 
         <xsl:value-of select="my:Description/text()"/> 
        </xsl:attribute> 
        <xsl:attribute name="entry_url"> 
         <xsl:value-of select="my:Url/text()"/> 
        </xsl:attribute> 
       </picture> 
      </xsl:for-each> 
     </pictures> 
    </xsl:template> 
</xsl:stylesheet> 

我也設法主要利用模板來製作不同的版本,但我仍然是缺少解決方案圍繞着最簡單的方法:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:my="http://opensearch.org/searchsuggest2" 
       exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/my:SearchSuggestion/my:Section/my:Item"/> 
     <picture> 
      <xsl:attribute name="text"> 
       <xsl:value-of select="my:Text/text()"/> 
      </xsl:attribute> 
      <xsl:attribute name="url"> 
       <xsl:value-of select="my:Image/@source"/> 
      </xsl:attribute> 
      <xsl:attribute name="width"> 
       <xsl:value-of select="my:Image/@width"/> 
      </xsl:attribute> 
      <xsl:attribute name="height"> 
       <xsl:value-of select="my:Image/@height"/> 
      </xsl:attribute> 
      <xsl:attribute name="description"> 
       <xsl:value-of select="my:Description/text()"/> 
      </xsl:attribute> 
      <xsl:attribute name="entry_url"> 
       <xsl:value-of select="my:Url/text()"/> 
      </xsl:attribute> 
     </picture> 
    </xsl:template> 

    <xsl:template match="/"> 
     <pictures> 
      <xsl:apply-templates select="//my:Item"/> 
     </pictures> 
    </xsl:template> 
</xsl:stylesheet> 
+0

您不需要''中的''''。 – JLRishe

+0

@JLRishe'Item'元素是'SearchSuggestion'的子元素嗎? –

+0

具有'match =「my:Item」'的模板將匹配任何my:Item(只要沒有具有較高優先級的模板)。其中一個使用'match =「// my:Item」'將匹配任何my:Item與一個或多個祖先。由於「有一個或多個祖先」部分在這裏是多餘的,因此不需要「//」。 – JLRishe