tags:

views:

38

answers:

4

I have the following 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>

I want using XSL file select only one page This one gives me both:

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

Where and how can I filter/select or address the no="x" attribute?

Thanks Asaf

A: 

Something like this:

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

It should select the first. If you want to select the one with no="1" you can do this:

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

you can filter on attribute using [@att='val']:

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

Change this:

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

and add the processing in a template:

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

Then to exclude (filter) a particular pic (say the 2nd), add this empty template:

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