xsl-list
[Top] [All Lists]

Selecting a substring()

2005-12-08 06:50:38
Hello all, 

I have a problem with Xalan where I am trying to convert some XML to HTML.

I have an XML element from which I am trying to extract information:
<data gender="Male-male.jpg" name="Nome 2" salary="4.988,04" /> 
I am trying to create a html <img> element that contains whatever occurs after 
the '-' character as the src of the image.
For example, in the above XML from [ gender="Male-male.jpg" ] I would want to 
create: <img src="male.jpg"> 

Sounds simple enough, right? ...but what I have doesn't work.

I am able to extract the value of the attribute using the following expression:
<xsl:value-of select="$currentnode/@*[name()=current()/@field]"/>
(It's a little complicated because the name of the key-attribute is contained 
as a value-attribute in the <column> part of the XML. But it works.)

Usually this evaluates to something like: [ Male-male.jpg ] or [ 
Female-female.jpg ]

When I try to seperate the String, to get the string after the '-' character 
using this expression:
<xsl:variable name="image" select='substring-after("male-man.jpg","-")'/>

Attemping to print out the variable using: <xsl:copy-of select="$image"/> 
Successfully gives: [ man.jpg ]
        
So I think I am half way there.

My challenge is that this works fine for the string literal which I pass in as 
a test, but I want it to do exactly the same thing
dynamically using the expression and it doesn't work. I have tried passing in 
the expression directly like this:
        <b>value of image:</b> 
        <xsl:variable name="image" 
select='substring-after("$currentnode/@*[name()=current()/@field]","-")'/>
        <xsl:copy-of select="$image"/>

and like this:
         <xsl:value-of 
select='substring-after("$currentnode/@*[name()=current()/@field]","-")'/>

I have also tried passing in the expression via a variable:
        <xsl:variable name="temp2" 
select="$currentnode/@*[name()=current()/@field]"/>
        <xsl:copy-of select="$temp2"/> 
        <b>value of image:</b> <xsl:variable name="image" 
select='substring-after("$temp2","-")'/>
        <xsl:copy-of select="$image"/>

...and tried passing in the expression via a parameter:
        <xsl:param name="element" 
select="$currentnode/@*[name()=current()/@field]"/>
        <b>value of image:</b> 
        <xsl:variable name="image" select='substring-after("$element","-")'/>
        <xsl:copy-of select="$image"/>

But none of these seems to work.

Could anyone point me in the right direction? Thoughts would be greatly 
appreciated.

Jules


my xsl template:

<xsl:template match="row/data">
    <!-- here we declare a local variable called 'field' within which we store
    the value of the current node -->
    <xsl:variable name="currentnode" select="."/>
    <tr>
         <!-- loop through the current columns 
         for each row, we need to loop through the columns because the "field" 
element in 
         each column contains the name of the type of data to be displayed. 
This data is 
         in the row at the moment. -->
         <xsl:for-each select="$columns"> 
         <td>
         <!-- if the align element exists in the column, copy it into the 'td' 
element. 
         This ensures that the rows align with the column headers. -->
        <xsl:copy-of select="@align"/>
         <!-- This accesses the salary attribute of the <field> element-->
        <b>current field:</b> <xsl:value-of select="@field"/>
        <br/>
         <!-- @* means any variable
         name() returns the name of the current selected element (the column)
         current() returns a node set that has the current node as its only 
member
         $field is a variable that stores the value of the current node
         @field is the current column. i.e. name, age, gender. -->
        <b>current field value:</b> <xsl:value-of 
select="$currentnode/@*[name()=current()/@field]"/>
        <br/> 
        <b>value of image:</b> <xsl:variable name="image" 
select='substring-after("male-man.jpg","-")'/>
        <xsl:copy-of select="$image"/>
        <br/>
        <!--
        
        ATTEMPTS follow
        
        Try passing in the current element via a variable:
        <xsl:variable name="temp2" 
select="$currentnode/@*[name()=current()/@field]"/>
        <xsl:copy-of select="$temp2"/> 
        <b>value of image:</b> <xsl:variable name="image" 
select='substring-after("$temp2","-")'/>
        <xsl:copy-of select="$image"/>
        -->
        <!--
        Try passing in the current element directly:
        <xsl: name="temp2" select="$currentnode/@*[name()=current()/@field]"/>
        <xsl:copy-of select="$temp2"/> 
        <b>value of image:</b> 
        <xsl:variable name="image" 
select='substring-after("$currentnode/@*[name()=current()/@field]","-")'/>
        <xsl:copy-of select="$image"/>
        -->
        <!--
        Try passing in the current element via a parameter:
        <xsl:param name="element" 
select="$currentnode/@*[name()=current()/@field]"/>
        <b>value of image:</b> 
        <xsl:variable name="image" select='substring-after("$element","-")'/>
        <xsl:copy-of select="$image"/>
        <br/>
        -->
        <!-- 
        If the field data contains an image, we need to add the image
        to the <td> as part of a src attribute.
        <xsl:if test=""> 
        The XSL
            <img>
                <xsl:attribute name="src">
                    man.jpg
                </xsl:attribute>
            </img> 
        </xsl:if> -->
        </td>
        </xsl:for-each>
        
    </tr>
    </xsl:template>


my xml:
<?xml version="1.0" encoding="UTF-8" ?> 
<table cellpadding="4"> 
<table-config> 
<columns> 
        <column align="right" field="gender" label="Gender" /> 
        <column align="right" field="salary" label="Salary" /> 
        <column align="right" field="name" label="Name" /> 
</columns> 
</table-config> 
- <rows> 
- <row> 
        <data gender="Female-female.jpg" name="Nome 1" salary="9.712,27" /> 
</row> 
- <row> 
        <data gender="Male-male.jpg" name="Nome 2" salary="4.988,04" /> 
</row>
[...etc]
</rows>
</table>


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>