tags:

views:

29

answers:

2

I'm calling a template:

<table> 
     <xsl:apply-templates select="data/pics/row"/>
</table>

the template is

<xsl:template match="row">
        <tr> 
            <xsl:for-each select="td"> 
                <td border="0">
                 <a href="{@referencddePage}"> 
              <img src="{pic/@src}" width="{pic/@width}" height="{pic/@height}"/> 
          </a>
                </td> 
            </xsl:for-each> 
        </tr> 
    </xsl:template>

MY xml IS

<?xml version="1.0" encoding="iso-8859-8"?>
<?xml-stylesheet type="text/xsl" href="xslFiles\smallPageBuilder.xsl"?>
<data pageNo="3" referencePage="xxxxxxxxxxxxxxx.xml">
  <pics>
    <row no="0">
      <td col="0">
        <pic src="A.jpg" width="150" height="120"></pic>
      </td>      
    </row>   
  </pics>  
</data>

I want the line :a h r e f="{@referencddePage}" to get the input from the root :a h r e f= "{@referencddePage}"... but I'm already in the

Thanks Asaf

A: 

Use an XPATH that "jumps" to the top of the document with a leading slash, then walk down the tree:

/data/@referencePage

Applying it to your stylesheet:

<xsl:template match="row">
    <tr> 
        <xsl:for-each select="td"> 
            <td border="0">
                <a href="{/data/@referencePage}"> 
                    <img src="{pic/@src}" width="{pic/@width}" height="{pic/@height}"/> 
                </a>
            </td> 
        </xsl:for-each> 
    </tr> 
</xsl:template>
Mads Hansen
+1  A: 

I want the line :a h r e f="{@referencddePage}" to get the input from the root :a h r e f= "{@referencddePage}"... but I'm already in the <td level>

In case it is a rule that the @referencePage attribute is always attribute of the top element, then it can always be accessed as:

/*/@referencePage

Therefore, in your code you'll have:

<a href="{/*/@referencePage}"> 

I would recommend not to use <xsl:for-each> and to use only and`. In this way the resulting XSLT code is more understandable and can be more easily modified in the future:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="row">
  <tr>
   <xsl:apply-templates/>
  </tr>
 </xsl:template>

 <xsl:template match="td">
   <td border="0">
     <a href="{/*/@referencePage}">
       <xsl:apply-templates/>
     </a>
   </td>
 </xsl:template>

 <xsl:template match="pic">
   <img src="{@src}" width="{@width}" height="{@height}"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<data pageNo="3" referencePage="xxxxxxxxxxxxxxx.xml">
  <pics>
    <row no="0">
      <td col="0">
        <pic src="A.jpg" width="150" height="120"></pic>
      </td>
    </row>
  </pics>
</data>

the wanted output is produced:

<tr>
   <td border="0">
      <a href="xxxxxxxxxxxxxxx.xml">
         <img src="A.jpg" width="150" height="120"/>
      </a>
   </td>
</tr>

See how each template is so very simple. Also, the code is further simplified.

Now, instead of:

   <img src="{pic/@src}" width="{pic/@width}" height="{pic/@height}"/>

we have only:

   <img src="{@src}" width="{@width}" height="{@height}"/>
Dimitre Novatchev
@Dimitre: +1 For push style
Alejandro