This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: variable as select for template


> That was my try (it's a fragment):
>
> <xsl:variable name="parent_comp">  <!-- I wanted to get a node set in
> variable -->
>   <xsl:choose>
>    <xsl:when test="$comp_id=''">   <!-- the node set depends
> on a condition
> -->
>      <xsl:value-of select="component[@class=$comp_class and
> not(@id)]"/>
>    </xsl:when>
>    <xsl:otherwise>
>      <xsl:value-of select="component[@class=$comp_class and
> @id=$comp_id]"/>
>    </xsl:otherwise>
>   </xsl:choose>
> </xsl:variable>
>
> then i could use variable $parent_comp just as node set here
> and in other
> places in stylesheet:
> <xsl:apply-templates mode="extract_property" select="$parent_comp">
>
> If <xsl:value-of select"..."/> always get text() as result,
> are there any solutions???
>
Yes, there's a neat trick for this one:

<xsl:variable name="parent_comp"
  select="self::node()[$comp_id='']/
            component[@class=$comp_class and not(@id)] |
          self::node()[not($comp_id='')]/
            component[@class=$comp_class and @id=$comp_id]"/>

It's basically forming the union of two node-sets, one of which is always
empty.

The aboev is a general approach. Looking at the specific conditions used in
this case, there is actually a simpler solution. If $comp_id equals "", you
want the components with no @id attribute; otherwise you want those whose
@id attribute equals $comp_id. I'm assuming $comp_id is a string.

You can select these nodes as
select="component[@class=$comp_class and
        (($comp_id='' and not(@id)) or ($comp_id!='' and $comp_id=@id))]

Mike Kay


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]