[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Test character immediately preceding node


Subject: Re: [xsl] Test character immediately preceding node
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 8 Oct 2013 14:50:02 -0700

The node, whose last character we are testing for being a whitespace,
needs to be selected like this:

preceding-sibling::node()[self::text()][1]

And in case there is no such node, a new text node needs to be added,
containing only a single space -- this can be easily done overriding
the identity rule.

Cheers,
Dimitre

On Tue, Oct 8, 2013 at 2:12 PM, David Sewell <dsewell@xxxxxxxxxxxx> wrote:
> One strategy in XSLT 2.0 would be to test for the existence of a space at
> the end of the preceding node and add a space if it's not there:
>
>    <xsl:template match="unittitle">
>       <xsl:copy><xsl:apply-templates/></xsl:copy>
>    </xsl:template>
>
>    <xsl:template match="unitdate">
>       <xsl:choose>
>          <xsl:when test="preceding-sibling::node()[1][matches(., '\s$')]">
>             <xsl:value-of select="."/>
>          </xsl:when>
>          <xsl:otherwise>
>             <xsl:value-of select="concat(' ', .)"/>
>          </xsl:otherwise>
>       </xsl:choose>
>    </xsl:template>
>
> which will give you
>
>    <unittitle>Statements 2001</unittitle>
>    <unittitle>Statements 2001</unittitle>
>
> but the following should accomplish the same thing with more concision and
> is more robust as it will handle multiple spaces before <unitdate> if they
> occur:
>
>    <xsl:template match="unittitle">
>       <xsl:copy>
>          <xsl:value-of select="string-join((normalize-space(text()),
> unitdate), ' ')"/>
>       </xsl:copy>
>    </xsl:template>
>
> DS
>
>
> On Tue, 8 Oct 2013, Nathan Tallman wrote:
>
>> Is it possible to test a character immediately preceding a node? I
>> have an element with child-elements, my trouble is that sometimes
>> there is a space before the child-element, sometimes not. For example
>> I might have:
>>
>> <unittitle>Statements <unitdate>2001</unitdate></unittitle>
>> or
>> <unittitle>Statements<unitdate>2001</unitdate></unittitle>
>>
>> The XSLT that I'm forking from has instructions to insert a space
>> before <unitdate>, which sometimes results in two spaces in the
>> output. I'd like to use an xsl:choose to test for a space immediately
>> preceding <unitdate>. Is this possible? I'm using XSLT 2.0.
>>
>> Thanks,
>> Nathan
>>
>>
>
> --
> David Sewell, Editorial and Technical Manager
> ROTUNDA, The University of Virginia Press
> PO Box 400314, Charlottesville, VA 22904-4314 USA
> Email: dsewell@xxxxxxxxxxxx   Tel: +1 434 924 9973
> Web: http://rotunda.upress.virginia.edu/
>



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.


Current Thread
Keywords