[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Selecting a substring()

Subject: Selecting a substring()
From: "Julian Karsten Arthur" <julian.arthur@xxxxxxxxxxxxxxxxxx>
Date: Thu, 8 Dec 2005 10:50:38 -0300
man substring
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>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.