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

Discussion in 'XML' started by Vince C., Jul 23, 2003.

  1. Vince C.

    Vince C. Guest

    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:eek:therwise><xsl:value-of select="generate-id()"/></xsl:eek:therwise>
    </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.
     
    Vince C., Jul 23, 2003
    #1
    1. Advertising

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

    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." <> wrote in message
    news:3f1eb67d$0$242$...
    > 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:eek:therwise><xsl:value-of select="generate-id()"/></xsl:eek:therwise>
    > </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.
    >
    >
     
    Dimitre Novatchev, Jul 23, 2003
    #2
    1. Advertising

  3. Vince C.

    Vince C. Guest

    Thanks, Oleg.

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

    Vince C.
     
    Vince C., Jul 27, 2003
    #3
  4. Vince C.

    Vince C. Guest

    Thanks, Dean.

    "Dean Tiegs" <> a écrit dans le message de
    news:...
    > "Vince C." <> 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.
     
    Vince C., Jul 27, 2003
    #4
  5. Vince C.

    Vince C. Guest

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

    "Dimitre Novatchev" <> a écrit dans le message de
    news:bfmqij$gku16$-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.
     
    Vince C., Jul 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alastair Cameron
    Replies:
    1
    Views:
    7,416
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  2. Iain
    Replies:
    9
    Views:
    1,446
  3. Marc Aymerich
    Replies:
    0
    Views:
    73
    Marc Aymerich
    Nov 24, 2013
  4. Devin Jeanpierre
    Replies:
    12
    Views:
    108
    Steven D'Aprano
    Nov 24, 2013
  5. Chris Angelico
    Replies:
    0
    Views:
    64
    Chris Angelico
    Nov 24, 2013
Loading...

Share This Page