xslt I get multiple values in my variable

U

utterberg

Hi, I've got an xml that looks like this:

<section name="Risk">
<label>RISK MANAGEMENT (RM)</label>
<field name="RM.1">
<ref>RM.1</ref>
<label>Text goes here...</label>
<option value="2">Yes</option>
<option value="1">No</option>
<option value="0">-</option>
<data>1</data>
</field>
<field name="RM.2">
<ref>RM.2</ref>
<label>Text 2 goes here...</label>
<option value="2">Yes</option>
<option value="1">No</option>
<option value="0">-</option>
<data>1</data>
</field>
<field name="RM.2">
<ref>RM.2</ref>
<label>Text 2 goes here...</label>
<option value="2">Yes</option>
<option value="1">No</option>
<option value="0">-</option>
<data></data>
</field>
</section>

And what I want to do is loop through all fields and if <data> has
value get the <option>-content it "belongs" to ("Yes", "No" or "-").

So this is my xslt:
<xsl:for-each select="section[@name='Risk']/field">

<xsl:variable
name="data"
select="data" />

<xsl:variable name="optionText">
<xsl:if test="$data!=''">
<xsl:for-each select="option">
<xsl:if test="@value=$data">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:variable>

Print:<xsl:value-of select="label" /> <xsl:value-of
select="$optionText" />
</xsl:for-each>

(the thing is this loop is included in a bit more complex context
(wordml) - but for simplicity it is left out)

The problem I'm having with the above xslt is that when <data> has a
value my optionText-variable will run three times and that messes my
structure up. The variable does something strange it gets the value -
in the first example "No". But before and after there's a "gap" sort of
like it has gone through my loop and added some white space.

I don't know if this makes any scense. But I hope so. Maybe there's a
better way to do this?

Thanks in advance!
..christer
 
U

utterberg

I see that I was a bit sloppy when writing the xml. The third "field"
should look like this

<field name="RM.3">
<ref>RM.3</ref>
<label>Text 3 goes here...</label>
<option value="2">Yes</option>
<option value="1">No</option>
<option value="0">-</option>
<data></data>
</field>

..christer
 
?

=?ISO-8859-1?Q?Joachim_Wei=DF?=

.christer

sth. like that??

HIH

Jo

<xsl:template match="/">
<xsl:apply-templates select="section[@name='Risk']/field" />
</xsl:template>

<xsl:template match="field">
<xsl:value-of select="label" /><xsl:apply-templates select="data" />
</xsl:template>

<xsl:template match="data[.='']">
<xsl:apply-templates select="../option[@value='0']" />
</xsl:template>

<xsl:template match="data">
<xsl:variable name="value" select="." />
<xsl:apply-templates select="../option[@value=$value]" />
</xsl:template>

<xsl:template match="option">
<xsl:value-of select="." />
</xsl:template>
 
P

Peter Flynn

And what I want to do is loop through all fields

Probably not...
and if <data> has
value get the <option>-content it "belongs" to ("Yes", "No" or "-").

....what you seem to need is

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template
match="/section/field/option[following-sibling::data=@value]">
<xsl:value-of select="parent::field/@name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="label|ref|option|data"/>

<xsl:template match="section|field">
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

This produces the output

RM.1,No
RM.2,No
The problem I'm having with the above xslt is that when <data> has a
value my optionText-variable will run three times and that messes my
structure up. The variable does something strange it gets the value -
in the first example "No". But before and after there's a "gap" sort of
like it has gone through my loop and added some white space.

Using XPath to select the specific elements or attributes you want
and ignoring the rest is much faster and simpler than loops.

///Peter
 
U

utterberg

Tanks for your reply!

I'm not sure I understand or if I can use your solution on my problem
(probably can but my knowledge of xslt is limited). But as far as I
understand the <xsl:strip-space elements="*"/> together with <template
match="section ... etc. > should be enough to make my "extra space"
disapear?!

If so then I would be able to do something similar to this with my
variable (and keeping the outer loop on <field>):

<xsl:variable name="optionText">
<xsl:value-of select="option[following-sibling::data=@value]" />
</xsl:variable>

But this generates the same problem as before. What I dont understand
is why the number of my <option>-tags makes this extra space. In my
first example should it not be set to a value only when the
@value=$data? It seems to me that it "makes up room" for each
<option>-value - because if i have 2 option-tags in my xml the cell is
not as big as is the case with three option-tags.

Thanks!
..christer
 
P

Peter Flynn

Tanks for your reply!

I'm not sure I understand or if I can use your solution on my problem
(probably can but my knowledge of xslt is limited). But as far as I
understand the <xsl:strip-space elements="*"/> together with <template
match="section ... etc. > should be enough to make my "extra space"
disapear?!

If so then I would be able to do something similar to this with my
variable (and keeping the outer loop on <field>):

There is no need to use a loop for this.
<xsl:variable name="optionText">
<xsl:value-of select="option[following-sibling::data=@value]" />
</xsl:variable>

But this generates the same problem as before. What I dont understand
is why the number of my <option>-tags makes this extra space.

There is no space when I run your code through Saxon: I get

Print:Text goes here...No
Print:Text 2 goes here...No
Print:Text 3 goes here...

Can you post your output, and the software and platform you are using?
In my
first example should it not be set to a value only when the
@value=$data?
Yes.

It seems to me that it "makes up room" for each
<option>-value - because if i have 2 option-tags in my xml the cell is
not as big as is the case with three option-tags.

Please let us see this.

///Peter
 
U

utterberg

Thank you Peter!

You set me on the right track. I finally used a <xsl:call-template ...
and in the template your "option[following-sibling::data=@value]"

Thanks for your help!
..christer
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top