RE: [xsl] How to extract data from an element and not from its children

Subject: RE: [xsl] How to extract data from an element and not from its children
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 06 Dec 2006 11:58:13 -0500
At 11:42 AM 12/6/2006, it was written:
Would you be better off by re-designing your xml as:
<text>
<content>This text is some test </content>
<content><emph>text</emph></content>
<content>.</content>
</text>

Somehow that text within the xml doesn't seem correct - it looks
orphaned, but I guess the experts can answer that..

Would one really want to have


<p><content>I have a paragraph, which contains </content><inline>inline markup</inline><content> inside it.</content></p>

On the contrary, XML's capability of modeling mixed content (the technical term for this sort of thing) is one of its greatest strengths.

As for the question, in XSLT terms, the XML markup:

<text>This text is some test <emph>text</emph>.</text>

is parsed into a model that looks like this:

element 'text'
  text node (value: 'This text is some test ')
  element 'emph'
    text node (value: 'text')
  text node (value: '.')

These XML document structures are traversed and queried in XSLT using not functions, but XPath expressions (XPath includes a function library, but also more).

In this case, given the 'text' element as context, the XPath expression "text()", short for "child::text()", will return both the text node children of the element (its first and third children). If you want the value of only the first of these, you can write "text()[1]". In XPath 1.0, simply "text()" will often get you that result, but since the rules have changed in XPath 2.0 it's perhaps best to learn not to rely on that.

How you use that depends on why you need the expression, which wasn't given in the question.

Cheers,
Wendell



======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread