473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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

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>
<xsl:otherwise><xsl:value-of select="generate-id()"/></xsl:otherwise>
</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.
Jul 20 '05 #1
4 4489
Try:

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

This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="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." <no**@hotmail.com> wrote in message
news:3f*********************@read.news.be.uu.net.. .
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>
<xsl:otherwise><xsl:value-of select="generate-id()"/></xsl:otherwise>
</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.

Jul 20 '05 #2
Thanks, Oleg.

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

Vince C.
Jul 20 '05 #3
Thanks, Dean.

"Dean Tiegs" <da*******@telus.net> a écrit dans le message de
news:87************@telus.net...
"Vince C." <no**@hotmail.com> 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.
Jul 20 '05 #4
"Dimitre Novatchev" <dn********@yahoo.com> a écrit dans le message de
news:bf************@ID-152440.news.uni-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"
xmlns:xsl="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.
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Don Garrett | last post by:
I have an XML document at the root of a directory tree that contains relative URIs to resources in a directory tree. During XSLT processing, these URI's can be used without any problems to...
2
by: Claudio Jolowicz | last post by:
How can XSLT stylesheets be used to edit, remove and add nodes specified by their position in the document tree? The XML document stores development tasks in a hierarchical way, i.e. tasks can...
0
by: Mike | last post by:
I'm generating an XSLT document programatically in VB.Net. I'm then trying to apply that XSLT against a cXML document to generate my own internally developed XML document. I'm using RichTextBox...
5
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating...
2
by: sam | last post by:
Hi, I've been buried in xsl and xslt articles for several days now, and am still unsure as to what I need to do... Basically, my vb.net app loads up an XML file from an external source...
4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
4
by: darrel | last post by:
I think I've gotten the hang of XML and XSLT...at least enough to produce the results I'm looking for. I now need to take the next step and use ASP.NET (using VB.NET) to take the xml and send it...
2
by: RJN | last post by:
Hi Can XSLT be applied for transforming .txt files or are they applicable only for xml to xml transformation. Currently all the clients send file to a server in one particular format. The...
4
by: gouranga | last post by:
Sablotron keeps giving an error (non-XSL instruction) on the line with this code: <xsl:result-document href="slideshow/index.html" format="html"> Is this implemented in Sablotron? It's quite...
11
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.