Discussion:
convert an attribute's value to a text node?
Gunther Schadow
2002-12-02 18:05:12 UTC
Permalink
Hi, I am in the process of converting my heavy uses of Saxon and it's
extensions to more standard XSLT 2.0 + XPath 2.0 and here is the one
problem I have left:

I need to convert a string to a text-node. More specifically, I need
to convert the value of an attribute to a text-node, because I have
special templates that expect text-nodes.

In the old days I used:

saxon:node-set(string(@att))

which would give me back a text-node.

Note that saxon's implementation of exsl:node-set would not allow a
string argument, but saxon:node-set would, and would do exactly what
I needed.

Now Michael has removed saxon:node-set from Saxon 7 because this
RTF-madness ;-) is basically cleared now (THANKS for THAT!) But,
my little corner-use-case seems to be unhandled unless I am missing
something (which is likely.)

Thanks for any ideas!
-Gunther
--
Gunther Schadow, M.D., Ph.D. ***@regenstrief.org
Medical Information Scientist Regenstrief Institute for Health Care
Adjunct Assistant Professor Indiana University School of Medicine
tel:1(317)630-7960 http://aurora.regenstrief.org



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Joerg Heinicke
2002-12-02 18:22:16 UTC
Permalink
Hello Gunther,

do you really need a node set or do you want to output the text value
directly? I can see only one example for a needed node set:

<xsl:apply-templates select="saxon:node-set($string)"/>

But couldn't this be solved with a call-template and an appropriate
processing at this template?

Regards,

Joerg
Post by Gunther Schadow
Hi, I am in the process of converting my heavy uses of Saxon and it's
extensions to more standard XSLT 2.0 + XPath 2.0 and here is the one
I need to convert a string to a text-node. More specifically, I need
to convert the value of an attribute to a text-node, because I have
special templates that expect text-nodes.
which would give me back a text-node.
Note that saxon's implementation of exsl:node-set would not allow a
string argument, but saxon:node-set would, and would do exactly what
I needed.
Now Michael has removed saxon:node-set from Saxon 7 because this
RTF-madness ;-) is basically cleared now (THANKS for THAT!) But,
my little corner-use-case seems to be unhandled unless I am missing
something (which is likely.)
Thanks for any ideas!
-Gunther
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
David Carlisle
2002-12-02 18:34:14 UTC
Permalink
Post by Gunther Schadow
to more standard XSLT 2.0 + XPath 2.0
note nothing's standard about those yet, but...

<xsl:variable name="x">
<xsl:value-of select="@att"/>
</xsl:variable>

is (in xslt 2) a variable with a document node containing a text node,
so the text node you want is $x/text()

as in

<xsl:copy-of select="$x/text()"/>

except that copy-of always throws away top level / nodes, so you could
just do


<xsl:copy-of select="$x"/>


Incidentally why do you need a text node and not a string? There are not
thatmany places where it makes any difference?

David

_____________________________________________________________________
This message has been checked for all known viruses by Star Internet
delivered through the MessageLabs Virus Scanning Service. For further
information visit http://www.star.net.uk/stats.asp or alternatively call
Star Internet for details on the Virus Scanning Service.

XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Gunther Schadow
2002-12-02 20:14:37 UTC
Permalink
Thanks to faithful David Carlisle for pointing out the answer
before I finally found it myself (by fgrepping saxon's source
code :-). The answer was: construct a node set as a new result
set.

<xsl:variable name="stringAsText">
<xsl:value-of select="$string"/>
</xsl:variable>

As to David's and Joerg's question: what (the heck ;-) do I need
to convert strings to text nodes? Here is the answer: because
I am -- you guessed right -- throwing the text out for apply-
templates. To see more detail of my use case, refer to the
renewed regex (up-translation) thread (embryonic thread I should
say, for now there's only my message.)

Why am I not using call-templates etc? Because I never use
call-templates as a matter of policy, except for really
clear reasons, this not being one of them. I want to keep
my XSLT code as closely aligned to the "event pattern matching"
approach as possible. Whenever you may be using call-templates
I use apply-templates in a specific mode. Your "procedure"
metaphor is my use of a specific mode rather than a named
template. This keeps my code relatively flexible for change.

I am surprized how well I can still maintain this very dense
functional programming code of mine in XSLT. I rarely feel
that I am in the middle of a heap of ugly hacks and quick-fixes
(unless when I look at code that I wrote before I made my
policy decision against call-templates, and for-each, and
choose/when for that matter.)

regards
-Gunther
--
Gunther Schadow, M.D., Ph.D. ***@regenstrief.org
Medical Information Scientist Regenstrief Institute for Health Care
Adjunct Assistant Professor Indiana University School of Medicine
tel:1(317)630-7960 http://aurora.regenstrief.org



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Michael Kay
2002-12-03 10:03:34 UTC
Permalink
Post by Gunther Schadow
Hi, I am in the process of converting my heavy uses of Saxon
and it's extensions to more standard XSLT 2.0 + XPath 2.0 and
I need to convert a string to a text-node. More specifically,
I need to convert the value of an attribute to a text-node,
because I have special templates that expect text-nodes.
which would give me back a text-node.
Note that saxon's implementation of exsl:node-set would not
allow a string argument, but saxon:node-set would, and would
do exactly what I needed.
Actually (for the record), I don't think Saxon ever returned a text node
from this function. In Saxon 6.3 and 6.4 a string argument was rejected.
In Saxon 6.5 a string argument was accepted, but the result was a
document node that owned a text node whose value was the supplied
string.

The exslt common:node-set() function appears to have changed at some
stage and now specifies that with a string argument, the result should
be a text node. Saxon has never implemented this version of the EXSLT
specification (and I don't recall seeing any discussion of the change -
change control is not one of EXSLT's strong points).

The most portable way to get a text node from a string, if you really
want one, is

<xsl:variable name="tree">
<xsl:value-of select="@att"/>
</xsl:variable>
... exslt:node-set($tree/text()) ...

But it does strike me as a somewhat odd way of doing things.

Michael Kay
Software AG
home: ***@ntlworld.com
work: ***@softwareag.com


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Jeni Tennison
2002-12-03 10:30:18 UTC
Permalink
Mike,
Post by Michael Kay
The exslt common:node-set() function appears to have changed at some
stage and now specifies that with a string argument, the result
should be a text node. Saxon has never implemented this version of
the EXSLT specification (and I don't recall seeing any discussion of
the change - change control is not one of EXSLT's strong points).
We discussed it in April 2001:

http://aspn.activestate.com/ASPN/Mail/Message/639319

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Continue reading on narkive:
Loading...