选择attribute =“X”的元素

时间:2010-08-10 18:25:34

标签: xml xslt

我有以下XML

<?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”属性?

谢谢Asaf

4 个答案:

答案 0 :(得分:1)

您可以使用[@att='val']过滤属性:

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

答案 1 :(得分: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>     

进入

<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>

然后排除(过滤)特定的图片(比如说第2张),添加这个空模板:

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

答案 2 :(得分:0)

这样的事情:

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

它应该选择第一个。如果你想选择没有=“1”的那个,你可以这样做:

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

答案 3 :(得分:0)

<xsl:for-each select="pics/page[@no='x']/pic">
   <!-- do something -->
</xsl:for-each>
相关问题