get list item by attribute value

R

RolfK

Dear ALL,

How can I do this best in XSLT2.0:

Imagine I have a value from element/@attribute which represents an
integer.

With this integer I would like to choose a value from a list like this
('red', 'blue' , 'green' )
The list is pre-defined and stored in some global variable.

How to acces it without long xsl:choose / when or if statements ?

And what about there is no value returned from element/@attribute ?
(In that case I would like to get a default value e.g. 'white' )

I guess there might be a nice function in xslt2, but I did not find
one yet.

Thanks a lot

Rolf
 
M

Martin Honnen

RolfK said:
Imagine I have a value from element/@attribute which represents an
integer.

With this integer I would like to choose a value from a list like this
('red', 'blue' , 'green' )
The list is pre-defined and stored in some global variable.

Well how does the integer map to the string values? If the integer value
is the one based index then it is easy:
$seq[$el/@bar]
How to acces it without long xsl:choose / when or if statements ?

And what about there is no value returned from element/@attribute ?
(In that case I would like to get a default value e.g. 'white' )

($seq[$el/@bar], 'white')[1]

Example: XML input is

<root>
<foo bar="1"/>
<foo/>
</root>

XSLT stylesheet is

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xsd">

<xsl:variable name="seq" as="xsd:string*" select="('red', 'blue' ,
'green')"/>

<xsl:template match="root">
<result>
<xsl:apply-templates select="foo"/>
</result>
</xsl:template>

<xsl:template match="foo">
<color>
<xsl:value-of select="($seq[current()/@bar], 'white')[1]"/>
</color>
</xsl:template>

</xsl:stylesheet>

Result is

<?xml version="1.0"
encoding="UTF-8"?><result><color>red</color><color>white</color></result>
 
R

RolfK

Hallo Martin,

thanks a lot. It is quite what I tried. It may be the difference in my
variable declaration.
Is it important that I declare as="xs:string*" in the variable
declaration ?
I did not do any type declaration and got always all colors back,
independent of the index.

Rolf

Martin said:
RolfK said:
Imagine I have a value from element/@attribute which represents an
integer.

With this integer I would like to choose a value from a list like this
('red', 'blue' , 'green' )
The list is pre-defined and stored in some global variable.

Well how does the integer map to the string values? If the integer value
is the one based index then it is easy:
$seq[$el/@bar]
How to acces it without long xsl:choose / when or if statements ?

And what about there is no value returned from element/@attribute ?
(In that case I would like to get a default value e.g. 'white' )

($seq[$el/@bar], 'white')[1]

Example: XML input is

<root>
<foo bar="1"/>
<foo/>
</root>

XSLT stylesheet is

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xsd">

<xsl:variable name="seq" as="xsd:string*" select="('red', 'blue' ,
'green')"/>

<xsl:template match="root">
<result>
<xsl:apply-templates select="foo"/>
</result>
</xsl:template>

<xsl:template match="foo">
<color>
<xsl:value-of select="($seq[current()/@bar], 'white')[1]"/>
</color>
</xsl:template>

</xsl:stylesheet>

Result is

<?xml version="1.0"
encoding="UTF-8"?><result><color>red</color><color>white</color></result>
 
M

Martin Honnen

RolfK said:
thanks a lot. It is quite what I tried. It may be the difference in my
variable declaration.
Is it important that I declare as="xs:string*" in the variable
declaration ?

Well it is optional but it is good practice to declare the type of
variables, whether you use XSLT 2.0 or other programming languages.
I did not do any type declaration and got always all colors back,
independent of the index.

You will need to show your exact code (XML and XSLT).
 
D

David Carlisle

RolfK said:
Hallo Martin,

thanks a lot. It is quite what I tried. It may be the difference in my
variable declaration.
Is it important that I declare as="xs:string*" in the variable
declaration ?
I did not do any type declaration and got always all colors back,
independent of the index.

you need $seq[position()=current()/@bar] if you just say
$seq[current()/@bar]
then the predicate is true if there is a bar attribute (with any value),
so will return all the colours. (Martin's example didn't show the
problem as the input example uses @bar="1" and the value-of used a [1]
filter so only showed the first one, which produces the right example as
posted but wouldnt work if you changed th einput to bar="2"

David
Rolf

Martin said:
RolfK said:
Imagine I have a value from element/@attribute which represents an
integer.

With this integer I would like to choose a value from a list like this
('red', 'blue' , 'green' )
The list is pre-defined and stored in some global variable.
Well how does the integer map to the string values? If the integer value
is the one based index then it is easy:
$seq[$el/@bar]
How to acces it without long xsl:choose / when or if statements ?

And what about there is no value returned from element/@attribute ?
(In that case I would like to get a default value e.g. 'white' )
($seq[$el/@bar], 'white')[1]

Example: XML input is

<root>
<foo bar="1"/>
<foo/>
</root>

XSLT stylesheet is

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xsd">

<xsl:variable name="seq" as="xsd:string*" select="('red', 'blue' ,
'green')"/>

<xsl:template match="root">
<result>
<xsl:apply-templates select="foo"/>
</result>
</xsl:template>

<xsl:template match="foo">
<color>
<xsl:value-of select="($seq[current()/@bar], 'white')[1]"/>
</color>
</xsl:template>

</xsl:stylesheet>

Result is

<?xml version="1.0"
encoding="UTF-8"?><result><color>red</color><color>white</color></result>
 
P

Peter Flynn

RolfK said:
Dear ALL,

How can I do this best in XSLT2.0:

Imagine I have a value from element/@attribute which represents an
integer.

With this integer I would like to choose a value from a list like this
('red', 'blue' , 'green' )
The list is pre-defined and stored in some global variable.

How to acces it without long xsl:choose / when or if statements ?

And what about there is no value returned from element/@attribute ?
(In that case I would like to get a default value e.g. 'white' )

I guess there might be a nice function in xslt2, but I did not find
one yet.

You could even do this in XSLT 1.0 with a node-set function: store the
lookup data, eg <table><colour value="1" name="red"/><colour value="2"
name="blue"/><colour value="3" name="green"/></table> and then just
reference the structure as
select="$whatever/lookup/colour[@value=current()/element/@attribute]/@name"

///Peter
 
D

Dimitre Novatchev

You could even do this in XSLT 1.0 with a node-set function: store the
lookup data, eg <table><colour value="1" name="red"/><colour value="2"
name="blue"/><colour value="3" name="green"/></table> and then just
reference the structure as
select="$whatever/lookup/colour[@value=current()/element/@attribute]/@name"

Actually, the xx:node-set() function is not needed:

document('')/*/xsl:variable[@name='myGlobalVarName']/lookup/colour[@value=current()/element/@attribute]/@name



Cheers,
Dimitre Novatchev


Peter Flynn said:
RolfK said:
Dear ALL,

How can I do this best in XSLT2.0:

Imagine I have a value from element/@attribute which represents an
integer.

With this integer I would like to choose a value from a list like this
('red', 'blue' , 'green' )
The list is pre-defined and stored in some global variable.

How to acces it without long xsl:choose / when or if statements ?

And what about there is no value returned from element/@attribute ?
(In that case I would like to get a default value e.g. 'white' )

I guess there might be a nice function in xslt2, but I did not find
one yet.

You could even do this in XSLT 1.0 with a node-set function: store the
lookup data, eg <table><colour value="1" name="red"/><colour value="2"
name="blue"/><colour value="3" name="green"/></table> and then just
reference the structure as
select="$whatever/lookup/colour[@value=current()/element/@attribute]/@name"

///Peter
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top