XSLT beginner's question

M

Martin

I have 5 days of experince with XSLT and I am sure my problem is
pretty much as basic as they come but I cannot work it out, so I hope
someone will take pity on me - please!

I have inherited an XML file format which has one header node and
several child nodes :

<wi workitemid= "99999" .... etc .... />
<widata dataname="AMOUNT" datadisplay="Amount" datavalue="123.45"/>
<widata dataname="ADDRESS1" datadisplay="Address" datavalue="Line
1" >
<widata .... etc ....

and am processing them using XSLT to produce an HTML table, by for
example :

<xsl:apply-templates select="widata[@dataname = 'AGENTNAME']"
mode="left_left"/>

The template puts the value of datadisplay into one table cell, and
the value of datavalue into the next cell.

So far, so good. However, how would I get the values from two widata
nodes and concatenate them so that I can put them into one cell? I
know about the concat function but I am having problems getting hold
of the values from the nodes and storing them so that I can then use
that function.

I hope I have included enough info to be helpful.

Thank-you.

Martin
 
V

vin sharma

Use variables to hold the values and then concat the variables.

<td>

<xsl:variable name="state">
<xsl:value-of select="widata[@dataname = 'ADDRESS3']/@datavalue" />
<!-- or some other node selection -->
</xsl:variable>

<xsl:variable name="zip">
<xsl:value-of select="widata[@dataname = 'ADDRESS4']/@datavalue" />
<!-- or some other node selection -->
</xsl:variable>

....

<xsl:value-of select="concat($state, ' ', $zip)" />

....

</td>

HTH,
-vin
 
A

Andy Dingley

However, how would I get the values from two widata
nodes and concatenate them so that I can put them into one cell?

I am having problems getting hold
of the values from the nodes and storing them so that I can then use
that function.

So don't store them. Write two XPath expressions, get them working
and tested separately, then cut-and-paste each one into the arguments
to concat()
 
M

Martin

Thank you both for your responses. They both make sense to me but I'm
still doing something not quite right because I can't get either of
them to transform.

Firstly my XPath expressions are fine now, thanks for the pointers.
I've put them in a couple of xsl:comments and the values are what I'm
after. However,

Vin - if I use your suggestion, I get the error

A reference to variable or parameter 'x' cannot be resolved. The
variable or parameter may not be defined, or it may not be in scope.

This has completely puzzled me as the only time at the moment I
reference the variable is when I try to allocate a value to it; I
haven't got to the point of trying to then use that value later on, so
how can it possibly be that when I create the variable and at the same
time allocate a value to it the variable is out of scope ?!

Andy - I have tried pasting my XPath expressions as you suggested :

<xsl:call-template name="left_left">
<xsl:with-param name="data" select="concat(
xsl:value-of select='widata[@dataname =
&quot;AGENTID&quot;]/@datavalue' ,
' ' ,
xsl:value-of select='widata[@dataname =
&quot;AGENTNAME&quot;]/@datavalue')"
/>
</xsl:call-template>

I get the error

Keyword xsl:call-template may not contain PCDATA nodes.

Please could you help me understand these issues.

Thanks,

Martin
 
P

Patrick TJ McPhee

% <xsl:call-template name="left_left">
% <xsl:with-param name="data" select="concat(
% xsl:value-of select='widata[@dataname =
% &quot;AGENTID&quot;]/@datavalue' ,
% ' ' ,
% xsl:value-of select='widata[@dataname =
% &quot;AGENTNAME&quot;]/@datavalue')"
% />

You're trying to put xslt elements inside an xpath expression. Try

% <xsl:call-template name="left_left">
% <xsl:with-param name="data" select="concat(
widata[@dataname ='AGENTID']/@datavalue,
% ' ' ,
widata[@dataname = 'AGENTNAME']/@datavalue)"
% />
% </xsl:call-template>

The suggestion to concatenate two xsl:value-of was simply to do that:

<xsl:template name="left_left">
<!-- whatever -->
<xsl:value-of select ="widata[@dataname ='AGENTID']/@datavalue"/>
<xsl:value-of select ="widata[@dataname = 'AGENTNAME']/@datavalue"/>
<!-- whatever else -->
</xsl:template>
 
M

Martin

You're trying to put xslt elements inside an xpath expression. Try

% <xsl:call-template name="left_left">
% <xsl:with-param name="data" select="concat(
widata[@dataname ='AGENTID']/@datavalue,
% ' ' ,
widata[@dataname = 'AGENTNAME']/@datavalue)"
% />
% </xsl:call-template>

Thank you, Patrick. Yes, I was trying to make it more complicated
than was necessary. I have now got it working and I appreciate the
help that you and everyone else gave me.

I would still like to understand the problem I had earlier when trying
to use a variable. I have stripped down my stlyesheet to the
following minimum skeleton :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:eek:utput method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:variable name="agentname" select="0"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


I still get the error : "A reference to variable or parameter
'agentname' cannot be resolved. The variable or parameter may not be
defined, or it may not be in scope".

Can anyone enlighten me as to what I am doing wrong here, please?

Martin
 
P

Patrick TJ McPhee

% to use a variable. I have stripped down my stlyesheet to the
% following minimum skeleton :
%
% <xsl:stylesheet version="1.0"
% xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
% xmlns:fo="http://www.w3.org/1999/XSL/Format">
%
% <xsl:eek:utput method="html"/>
% <xsl:template match="/">
% <html>
% <body>
% <xsl:variable name="agentname" select="0"/>
% </body>
% </html>
% </xsl:template>
% </xsl:stylesheet>

% I still get the error : "A reference to variable or parameter
% 'agentname' cannot be resolved. The variable or parameter may not be
% defined, or it may not be in scope".

Are you sure this is the stylesheet giving that error? There is no
reference to agentname here. Is there another stylesheet with
$agentname in it somewhere?
 
V

vin sharma

Martin said:
I would still like to understand the problem I had earlier when trying
to use a variable. I have stripped down my stlyesheet to the
following minimum skeleton :

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:eek:utput method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:variable name="agentname" select="0"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


I still get the error : "A reference to variable or parameter
'agentname' cannot be resolved. The variable or parameter may not be
defined, or it may not be in scope".

Can anyone enlighten me as to what I am doing wrong here, please?

Are you using *-PARAM agentname* as a command-line option?
 
M

Martin

vin sharma said:
Are you using *-PARAM agentname* as a command-line option?

No, I am using XSLDebugger (http://www.vbxml.com/xsldebugger/)to do my
development, and it is this that is giving me the error message.

Should my previously posted stylesheet work OK? If so, then maybe
this product is buggy?? Is there another freeware product or
alternative method I can use instead?

Can I throw in another issue that I've come up against? Now that my
concat statement is working I want to add line breaks between my
values and so I am trying to get <BR> tags in my output HTML.

<xsl:call-template name="left_left">
<xsl:with-param name="data" select="concat(
widata[@dataname = 'ADDRESS1']/@datavalue, '<' 'BR',
'>',
widata[@dataname = 'ADDRESS2']/@datavalue, '<' 'BR',
'>',
etc.

However, this gets transformed into
&lt;BR&gt;
not
<BR>
and so my HTML doesn't display as I want it.

How can I get the angle brackets to output please?

Martin
 
B

Bob Foster

You can't output tags as data. Output them as HTML tags.

Bob Foster

Martin said:
vin sharma <[email protected]> wrote in message
Are you using *-PARAM agentname* as a command-line option?

No, I am using XSLDebugger (http://www.vbxml.com/xsldebugger/)to do my
development, and it is this that is giving me the error message.

Should my previously posted stylesheet work OK? If so, then maybe
this product is buggy?? Is there another freeware product or
alternative method I can use instead?

Can I throw in another issue that I've come up against? Now that my
concat statement is working I want to add line breaks between my
values and so I am trying to get <BR> tags in my output HTML.

<xsl:call-template name="left_left">
<xsl:with-param name="data" select="concat(
widata[@dataname = 'ADDRESS1']/@datavalue, '<' 'BR',
'>',
widata[@dataname = 'ADDRESS2']/@datavalue, '<' 'BR',
'>',
etc.

However, this gets transformed into
&lt;BR&gt;
not
<BR>
and so my HTML doesn't display as I want it.

How can I get the angle brackets to output please?

Martin
 
M

Martin

Bob Foster said:
You can't output tags as data. Output them as HTML tags.

Bob Foster

Ah, but how?? I really am very new to this stuff and it is very
different from the VB with which I mainly make my living. I'm trying
to learn from books and the helpful people on here but at the moment
all I can find are the ways I cannot do this! I have tried each of
the following :

<xsl:call-template name="left_left">
<xsl:with-param name="data" select="concat(
widata[@dataname = 'SEC_ADDRESS2']/@datavalue, '<', 'BR',
'>',
widata[@dataname = 'SEC_ADDRESS3']/@datavalue, <BR>,
widata[@dataname = 'SEC_ADDRESS4']/@datavalue, '<BR>',

with the following results :
the first one transforms into &gt; and &lt; and displays as <BR> as
I've already said;
the second and third ones say that "the character '<' cannot be used
in an attribute value" which I understand and which was why I tried
using the hex values.

It was suggested to me that a captial A with a circumflex (hex C2)
might act as an escape character but it only seems to work with a
pound-sterling sign £, not generally.

So, I am still in the dark as to how to output these tags as I
require. Please can anyone offer further help.

Thanks,

Martin
 
M

Malcolm Dew-Jones

Martin ([email protected]) wrote:
: > You can't output tags as data. Output them as HTML tags.
: >
: > Bob Foster
: >

: Ah, but how?? I really am very new to this stuff and it is very
: different from the VB with which I mainly make my living. I'm trying
: to learn from books and the helpful people on here but at the moment
: all I can find are the ways I cannot do this! I have tried each of
: the following :

: <xsl:call-template name="left_left">
: <xsl:with-param name="data" select="concat(
: widata[@dataname = 'SEC_ADDRESS2']/@datavalue, '<', 'BR',
: '>',
: widata[@dataname = 'SEC_ADDRESS3']/@datavalue, <BR>,
: widata[@dataname = 'SEC_ADDRESS4']/@datavalue, '<BR>',

: with the following results :
: the first one transforms into &gt; and &lt; and displays as <BR> as
: I've already said;
: the second and third ones say that "the character '<' cannot be used
: in an attribute value" which I understand and which was why I tried
: using the hex values.

Butting in, if I missed something, sorry, but...

To get the < into the attribute, the attribute actually has to contain the
literal string &lt; (i.e. four characters). To get that sequence of
characters into the outer "layer" of markup you have to encode those four
characters (which actually means just the & cause it's the thing that's
special).


So I would try &amp;lt;

(or even &amp;amp;lt; , though that might be one level of encoding too
many)
 
D

Dean Tiegs

Bob Foster said:
You can't output tags as data. Output them as HTML tags.

Bob Foster

Ah, but how?? I really am very new to this stuff and it is very
different from the VB with which I mainly make my living. I'm
trying to learn from books and the helpful people on here but at the
moment all I can find are the ways I cannot do this! I have tried
each of the following :

<xsl:call-template name="left_left">
<xsl:with-param name="data" select="concat(
widata[@dataname = 'SEC_ADDRESS2']/@datavalue, '<', 'BR',
'>',
widata[@dataname = 'SEC_ADDRESS3']/@datavalue, <BR>,
widata[@dataname = 'SEC_ADDRESS4']/@datavalue, '<BR>',

Try this:

<xsl:call-template name="left_left">
<xsl:with-param name="data">
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS2']/@datavalue"/>
<br/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS3']/@datavalue"/>
<br/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS4']/@datavalue"/>
<br/>
</xsl:with-param>
</xsl:call-template>
 
P

Patrick TJ McPhee

% % > You can't output tags as data. Output them as HTML tags.
% >
% > Bob Foster
% >
%
% Ah, but how??

Go back to the other suggestion for generating the parameter:

<xsl:call-template name="left_left">
<xsl:with-param name="data">
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS1']/@datavalue"/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS2']/@datavalue"/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS3']/@datavalue"/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS4']/@datavalue"/>
</xsl:with-param>
</xsl:call-template>

Now insert the breaks between them. I think you have to use xsl:element
to do this, since <br> is not well-formed. I expect if you're using
xhtml you could write <br/> directly, but I'm not an html maven.


<xsl:call-template name="left_left">
<xsl:with-param name="data">
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS1']/@datavalue"/>
<xsl:element name="br"/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS2']/@datavalue"/>
<xsl:element name="br"/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS3']/@datavalue"/>
<xsl:element name="br"/>
<xsl:value-of select="widata[@dataname = 'SEC_ADDRESS4']/@datavalue"/>
<xsl:element name="br"/>
</xsl:with-param>
</xsl:call-template>

Now, in template left_left, you refer to this using copy-of, rather than
value-of

<xsl:copy-of select="$data"/>

copy-of will insert a node tree, while value-of inserts the concatenated
text nodes (i.e., throwing away the <br> nodes).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Contact form question 2
xslt concatenation 1
xslt / xpath question 3
Merging tables with XSLT 2
beginner's problem with sqlite3 14
Help with XSLT 0
XSLT help 3
XSLT - removing a tag from within text 20

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top