2010-08-10 26 views
0

我有以下XML其中attribute =「X」

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="example3.xsl"?> 
<pics> 
    <page no="1"> 
     <pic> 
     <td> 
     <no src="http://www.templetons.com/brad/pq/playaphone.jpg" width="150" height="120">1</no> 
     </td>  
     </pic> 
     <pic> 
     <td> 
     <no src="http://motherjones.com/files/legacy/mojoblog/funny-cats-a10.jpg" width="150" height="120">4</no> 
     </td>  
     </pic> 
    </page> 
    <page no="2">  
     <pic> 
     <td> 
     <no src="http://motherjones.com/files/legacy/mojoblog/funny-cats-a10.jpg" width="150" height="120">4</no> 
     </td>  
     </pic> 
     <pic> 
     <td> 
     <no src="http://www.templetons.com/brad/pq/playaphone.jpg" width="150" height="120">1</no> 
     </td>  
     </pic>  
    </page> 
</pics> 

我想使用XSL文件只選擇一個頁面 這一個選擇元素給了我兩個:

<xsl:for-each select="pics/page/pic"> 
    <tr> 
     <xsl:for-each select="td"> 
     <td><img> 
      <xsl:attribute name="src"> 
      <xsl:value-of select="no//@src"/> 
      </xsl:attribute> 
      <xsl:attribute name="width"> 
      <xsl:value-of select="no//@width"/> 
      </xsl:attribute> 
      <xsl:attribute name="height"> 
      <xsl:value-of select="no//@height"/> 
      </xsl:attribute> 
     </img></td> 
     </xsl:for-each> 
    </tr> 
    </xsl:for-each> 

在哪裏,怎麼能我過濾/選擇或尋址no =「x」屬性?

感謝阿薩夫

+1

請勿使用這些'xsl:attribute'。在你的情況下,使用文字結果AVT(如'')或只使用'xsl:copy-of select =「no/@ *」'。另外,不要使用''''來選擇屬性,只是'no/@ src'工作。 – 2010-08-10 19:30:05

+0

看到我的答案是一個很好的簡單解決方案。不要使用'' - 儘量不要使用它,儘量避免它。瞭解一些... – 2010-08-10 20:57:21

+0

亞歷杭德羅和Dimitre Novatchev,謝謝你的提示。 我正在使用新技術的兩個項目中。 我目前對xslt的需求是非常基礎的。 我的代碼看起來是這樣的,因爲這是我能從這裏給出的例子中編譯出來的最簡單的代碼。 當你說不使用的東西是基於經驗。分享這些知識可以幫助許多其他需要的不僅僅是「使其工作」 Dimitre ...當我有更多的時間,我想聽取您的意見,並瞭解更多:) 我謝謝你們倆許多 – Asaf 2010-08-11 10:00:49

回答

1

更改此

<xsl:for-each select="pics/page/pic">      
    <tr>      
     <xsl:for-each select="td">      
     <td><img>      
      <xsl:attribute name="src">      
      <xsl:value-of select="no//@src"/>      
      </xsl:attribute>      
      <xsl:attribute name="width">      
      <xsl:value-of select="no//@width"/>      
      </xsl:attribute>      
      <xsl:attribute name="height">      
      <xsl:value-of select="no//@height"/>      
      </xsl:attribute>      
     </img></td>      
     </xsl:for-each>      
    </tr>      
    </xsl:for-each>  

into this

<xsl:apply-templates select="pics/page/pic"/> 

並添加處理模板:

<xsl:template match="pic"> 
<tr> 
    <xsl:for-each select="td"> 
    <td> 
    <img src="{no/@src}" width="{no/@width}" height="{no/@height}"/> 
    <td> 
    </xsl:for-each> 
</tr> 
</xsl:template> 

然後排除(過濾器)的特定PIC(說的第2次),添加這個空的模板:

<xsl:template match="pic[@no='2']"/> 
0

事情是這樣的:

<xsl:for-each select="pics/page[1]/pic"> 

應該選擇第一個。如果你想選擇一個不帶= 「1」,你可以這樣做:用[@att='val']

<xsl:for-each select="pics/page[@no='1']/pic"> 
0
<xsl:for-each select="pics/page[@no='x']/pic"> 
    <!-- do something --> 
</xsl:for-each> 
1

你可以在過濾屬性:

<xsl:for-each select="pics/page[@no='1']/pic">