Re: [xsl] Can a single XPath statement duplicate the functionality of this verbose <xsl:choose> statement?

Subject: Re: [xsl] Can a single XPath statement duplicate the functionality of this verbose <xsl:choose> statement?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sun, 23 Oct 2011 17:51:50 -0700
Use:

for $n in @souvenir-sheet | @minisheet | @booklet | @se-tenant | @coupon,
    $loc-n in ../Location/@*[name() = name($n)]

  return
    ($loc-n, ../Location/@denomination, 0)[1]

This relies on the fact mentioned in the definition of the problem,
that the attributes are mutually exclusive (only one of them can be
present).



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

On Sun, Oct 23, 2011 at 5:18 PM, G. Ken Holman
<gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
> At 2011-10-23 17:10 -0700, Mark wrote:
>>
>> Although they do have the virtue of giving me my desired output, can the
>> following sequence of statements be replaced by a more generalized
(shorter
>> and simpler) XPath statement without the repetitive "when" tests shown
>> below?
>>
>> <xsl:choose>
>> B  B  B  B  <xsl:when test="@souvenir-sheet">
>> B  B  B  B  B  <xsl:copy-of select="if (../Location/@souvenir-sheet) then
>> ../Location/@souvenir-sheet else if (../Location/@denomination) then
>> ../Location/@denomination else '0' "/>
>> B  B  B  B  </xsl:when>
>> B  B  B  B  <xsl:when test="@minisheet">
>> B  B  B  B  B  <xsl:copy-of select="if(../Location/@minisheet) then
>> ../Location/@minisheet else if(../Location/@denomination) then
>> ../Location/@denomination else '0'"/>
>> B  B  B  B  </xsl:when>
>> B  B  B  B  <xsl:when test="@booklet">
>> B  B  B  B  B  <xsl:copy-of select="if(../Location/@booklet) then
>> ../Location/@booklet else if(../Location/@denomination) then
>> ../Location/@denomination else '0'"/>
>> B  B  B  B  </xsl:when>
>> B  B  B  B  <xsl:when test="@se-tenant">
>> B  B  B  B  B  <xsl:copy-of select="if (../Location/@se-tenant) then
>> ../Location/@se-tenant else if(../Location/@denomination) then
>> ../Location/@denomination else '0'"/>
>> B  B  B  B  </xsl:when>
>> B  B  B  B  <xsl:when test="@coupon">
>> B  B  B  B  B  <xsl:copy-of select=" if (../Location/@coupon) then
>> ../Location/@coupon else if(../Location/@denomination) then
>> ../Location/@denomination else '0'"/>
>> B  B  B  B  </xsl:when>
>> </xsl:choose>
>>
>> My XPath is very weak.
>>
>> I tried:
>> <xsl:copy-of select="if (../Location[name(@*)=name(@*)]) then
>> ../Location/@* B else if(../Location/@denomination) then
>> ../Location/@denomination else '0'"></xsl:copy-of>
>
> Presuming you have only a single attribute (the name() function will abend
> if you have more than one), this may work for you ... it expresses a
> sequence and selects the first member of that sequence:
>
> <xsl:copy select="( ../Location[name(@*)=name(current()/@*)] ,
> B  B  B  B  B  B  B  B  B  B ../Location/@denomination ,
> B  B  B  B  B  B  B  B  B  B '0' )[1]"/>
>
> This relies on the XPath 2 behaviour of sequence ( (), x ) collapsing to (
x
> ).
>
> Note the use of current() to return the node that was current at the start
> of evaluating the XPath expression (also available in XPath 1).
>
> I hope this helps.
>
> . . . . . . . . . Ken
>
>
> --
> Contact us for world-wide XML consulting and instructor-led training
> Crane Softwrights Ltd. B  B  B  B  B  B http://www.CraneSoftwrights.com/s/
> G. Ken Holman B  B  B  B  B  B  B  B  B 
mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Google+ profile: https://plus.google.com/116832879756988317389/about
> Legal business disclaimers: B  B http://www.CraneSoftwrights.com/legal

Current Thread