Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: how do I implement a VB-like IIF in XPath for setting attribute values?

Reply
Thread Tools

XSLT: how do I implement a VB-like IIF in XPath for setting attribute values?

 
 
Vince C.
Guest
Posts: n/a
 
      07-23-2003
Hi, all.

I'd like to know if there's a way to simplify writing attribute values in XSLT.
The case is the following:

<xsl:template ...>
<div id="{ if (@id) @id else generate-id() }">Div Text</div>
</xsl:template>

If context node has an @id then I want that id for output. Otherwise I want to
generate an id if there is none. I know the "long" method which is:

<xsl:attribute name="id">
<xsl:choose>
<xsl:when test="@id"><xsl:value-of select="@id"/></xsl:when>
<xsltherwise><xsl:value-of select="generate-id()"/></xsltherwise>
</xsl:choose>
</xsl:attribute>

But it's too long to me. Is there a shortcut? Note as I'm using MSXML4 I can use
MS XSL extensions too.

Thanks for any hint/suggestion,
Vince C.


 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-23-2003
Try:

<div id="{ concat(@id, generate-id(self::*[not(@id and
string(@id))]))}">Div Text</div>

This transformation:

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

<xsl:template match="div">
<div id="{ concat(@id, generate-id(self::*[not(@id and
string(@id))]))}">Div Text</div>
</xsl:template>
</xsl:stylesheet>

when applied on this source.xml:

<t>
<div id="1">xxx</div>
<div id="">yyy</div>
<div>zzz</div>
</t>

produces the wanted result:

<?xml version="1.0" encoding="UTF-16"?>
<div id="1">Div Text</div>
<div id="IDAJA2S">Div Text</div>
<div id="IDANA2S">Div Text</div>


Hope this helped.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"Vince C." <(E-Mail Removed)> wrote in message
news:3f1eb67d$0$242$(E-Mail Removed).. .
> Hi, all.
>
> I'd like to know if there's a way to simplify writing attribute values in

XSLT.
> The case is the following:
>
> <xsl:template ...>
> <div id="{ if (@id) @id else generate-id() }">Div Text</div>
> </xsl:template>
>
> If context node has an @id then I want that id for output. Otherwise I

want to
> generate an id if there is none. I know the "long" method which is:
>
> <xsl:attribute name="id">
> <xsl:choose>
> <xsl:when test="@id"><xsl:value-of select="@id"/></xsl:when>
> <xsltherwise><xsl:value-of select="generate-id()"/></xsltherwise>
> </xsl:choose>
> </xsl:attribute>
>
> But it's too long to me. Is there a shortcut? Note as I'm using MSXML4 I

can use
> MS XSL extensions too.
>
> Thanks for any hint/suggestion,
> Vince C.
>
>



 
Reply With Quote
 
 
 
 
Vince C.
Guest
Posts: n/a
 
      07-27-2003
Thanks, Oleg.

Think I'll wait till MSXML4 supports XPath2.0.

Vince C.


 
Reply With Quote
 
Vince C.
Guest
Posts: n/a
 
      07-27-2003
Thanks, Dean.

"Dean Tiegs" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> "Vince C." <(E-Mail Removed)> writes:

[...]
> You could split it into two templates:


That's exactly what I wanted to work around since I have somewhat duplicated
templates.

Vince C.


 
Reply With Quote
 
Vince C.
Guest
Posts: n/a
 
      07-27-2003
"Dimitre Novatchev" <(E-Mail Removed)> a écrit dans le message de
news:bfmqij$gku16$(E-Mail Removed)-berlin.de...
> Try:
>
> <div id="{ concat(@id, generate-id(self::*[not(@id and
> string(@id))]))}">Div Text</div>
>
> This transformation:
>
> <xsl:stylesheet version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:template match="div">
> <div id="{ concat(@id, generate-id(self::*[not(@id and
> string(@id))]))}">Div Text</div>
> </xsl:template>
> </xsl:stylesheet>
>
> when applied on this source.xml:
>
> <t>
> <div id="1">xxx</div>
> <div id="">yyy</div>
> <div>zzz</div>
> </t>
>
> produces the wanted result:
>
> <?xml version="1.0" encoding="UTF-16"?>
> <div id="1">Div Text</div>
> <div id="IDAJA2S">Div Text</div>
> <div id="IDANA2S">Div Text</div>
>
>
> Hope this helped.


Great! That's exactly what I wanted. If none of my IDs are empty, i.e. <div
id="">, I presume I can safely omit [and string(@id)] in the filter?

Note I didn't know there had a way to have generate-id() return an empty
string.

Vince C.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
picking value of one attribute based on a child elements attribute? XPath? vjethava@gmail.com XML 2 03-06-2006 05:19 AM
iif not working =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 4 06-03-2005 08:31 PM
IIF problem Vishal ASP .Net 0 11-29-2004 07:21 PM
Easy question, IIF and Convert with DBNulls Max ASP .Net 2 10-11-2004 08:49 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments