Re: [xsl] following-sibling on attributes

Subject: Re: [xsl] following-sibling on attributes
From: "Vidar S. Ramdal" <vidar@xxxxxxxx>
Date: Mon, 08 Sep 2003 16:24:35 +0200
Som Dimitre Novatchev sa den 08.09.2003 13:56:
Dimitre, are you sure that these lines in your reply

        <xsl:otherwise>
            <xsl:value-of select=".."/>
          </xsl:otherwise>

shouldn't be

<xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise>


Yes, I am definitely, absolutely sure. In this case (and typically) I do
not post untested and not working code.


What problem do you see with this code?

Didn't you run the transformation to see that it produces the wanted
result?

Yes, I did, and without changing your code I get an error when trying to parse the results of the transformation into a JDOM tree. However, changing to <xsl:value-of select="."/> everything works as expected, and I'm happy as a lark.


Right now, my mind is too blurry to see why that happens.

But perhaps I was not clear about the context in which the templates are used. Below is the entire code:

    <xsl:template match="IPS:TEXT">
        <xsl:choose>
            <xsl:when test="@*">
                <xsl:call-template name="TEXT_ATTRIBUTE_TO_ELEMENT">
                    <xsl:with-param name="attList" select="@*"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


<!-- Suggested by Dimitri Novatsjev http://www.biglist.com/lists/xsl-list/archives/200309/msg00243.html -->


    <xsl:template name="TEXT_ATTRIBUTE_TO_ELEMENT">
        <xsl:param name="attList" select="/.."/>
        <xsl:if test="$attList">
            <xsl:variable name="elementname"><xsl:apply-templates select="$attList[1]" mode="ELEMENTNAME"/></xsl:variable>
            <xsl:variable name="attributes">
                <xsl:apply-templates select="$attList[1]" mode="ATTRIBUTES"/>
            </xsl:variable>
            <xsl:element name="{$elementname}">
                <xsl:for-each select="$attributes/attr">
                    <xsl:attribute name="{@name}"><xsl:value-of select="."/></xsl:attribute>
                </xsl:for-each>
                <xsl:choose>
                    <xsl:when test="$attList[2]">
                        <xsl:call-template name="TEXT_ATTRIBUTE_TO_ELEMENT">
                            <xsl:with-param name="attList"
                                select="$attList[position() > 1]"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:if>
    </xsl:template>
>
    <!-- Templates below are used to change IPS:TEXT attributes into proper XHTML elements -->
    <xsl:template match="IPS:TEXT/attribute::node()" mode="ELEMENTNAME"><xsl:value-of select="local-name()"/></xsl:template>
    <xsl:template match="IPS:TEXT/@URL" mode="ELEMENTNAME">a</xsl:template>
    <xsl:template match="IPS:TEXT/@B" mode="ELEMENTNAME">b</xsl:template>
    <xsl:template match="IPS:TEXT/@I" mode="ELEMENTNAME">i</xsl:template>
    <xsl:template match="IPS:TEXT/@U" mode="ELEMENTNAME">u</xsl:template>
    <xsl:template match="IPS:TEXT/@TARGET" mode="ELEMENTNAME"/> <!-- TARGET is not an element, but is handled by @URL -->

<xsl:template match="IPS:TEXT/attribute::node()" mode="ATTRIBUTES"/> <!-- Most XHTML elements have no attributes -->
>
    <xsl:template match="IPS:TEXT/@URL" mode="ATTRIBUTES">
        <attr name="href"><xsl:value-of select="."/></attr>
        <xsl:if test="../@TARGET">
            <attr name="target"><xsl:value-of select="../@TARGET"/></attr>
        </xsl:if>
    </xsl:template>

Sample XML fragment:


<IPS:TEXT B="1" I="1">Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</IPS:TEXT>
<IPS:TEXT URL="ps://237;393" TARGET="_blank">Lorem ipsum dolor sit amet.</IPS:TEXT>
<IPS:TEXT B="1">Lorem ipsum dolor sit amet</IPS:TEXT>

This transforms very nicely into:


<b><i>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet</i></b>
<a href="ps://237;393" target="_blank">Lorem ipsum dolor sit amet.</a>
<b>Lorem ipsum dolor sit amet</b>

-- Vidar S. Ramdal <vidar@xxxxxxxx> Idium AS - http://www.idium.no Tlf. +47 22 00 84 00 / Fax: +47 22 00 84 01 A: Top posting. Q: What is the most annoying thing on usenet and in e-mail?


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



Current Thread