Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Literal Nodes

Reply
Thread Tools

Literal Nodes

 
 
Greg Esres
Guest
Posts: n/a
 
      05-21-2007
<One>
This is a test
<Two>Some Stuff</Two>
<Two>Some Stuff</Two>
<Two>Some Stuff</Two>
</One

When I process a "Two" node, I need to see if there is a text line
above it, as "This is a test" in the above example. I can't figure
out how to get access to that node. Suggestions?

Thanks!

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      05-21-2007
Greg Esres wrote:
> When I process a "Two" node, I need to see if there is a text line
> above it, as "This is a test" in the above example. I can't figure
> out how to get access to that node. Suggestions?


You haven't told us how you're processing the nodes. As a DOM? As a SAX
stream? Using XPath/XSLT? Other?

In the document you've shown us, every <Two> element is preceded by a
text node. Most of them are just whitespace (line break and
indentation); one of them has more than that. Navigating the document
tree to the previous sibling (or doing a bit of buffering, if you're
doing SAX programming) and checking whether the value contains
non-whitespace (or contains more than one line break, or whatever
criterion make sense for your application) should be straightforward.


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      05-21-2007

Greg Esres <(E-Mail Removed)> wrote in
<(E-Mail Removed) .com>:
> <One>
> This is a test
> <Two>Some Stuff</Two>
> <Two>Some Stuff</Two>
> <Two>Some Stuff</Two>
> </One
>
> When I process a "Two" node, I need to see if there is a
> text line above it


preceding-sibling::text()

--
Pavel Lepin
 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      05-21-2007
Greg Esres wrote:
> <One>
> This is a test
> <Two>Some Stuff</Two>
> <Two>Some Stuff</Two>
> <Two>Some Stuff</Two>
> </One


Don't ever do this. It will only end in tears and recriminations. Unless
you really know what you are doing with text-document markup, put your
free-floating text inside some markup, eg

<One>
<Description>This is a test</Description>
<Two>Some Stuff</Two>
<Two>Some Stuff</Two>
<Two>Some Stuff</Two>
</One>

If what you showed is is stuff you've been sent, send it back and
suggest they get someone to mark it up sensibly before you process it.

> When I process a "Two" node, I need to see if there is a text line
> above it, as "This is a test" in the above example. I can't figure
> out how to get access to that node. Suggestions?


preceding-sibling::text()[.!='']

///Peter
--
XML FAQ: http://xml.silmaril.ie/
 
Reply With Quote
 
Greg Esres
Guest
Posts: n/a
 
      05-23-2007
On May 20, 7:56 pm, Joe Kesselman <(E-Mail Removed)> wrote:
> Greg Esres wrote:
> > When I process a "Two" node, I need to see if there is a text line
> > above it, as "This is a test" in the above example. I can't figure
> > out how to get access to that node. Suggestions?

>

<<You haven't told us how you're processing the nodes. As a DOM? As a
SAX
stream? Using XPath/XSLT? Other?>>>

Oh, sorry, I've got my mind so wrapped in XSLT that it never occurred
to me that it could be something else.

<<should be straightforward.>>

Yes. I was pretty sure that I tried "previous-sibling" but it didn't
work. Since you all seem to think that's the proper way, I must have
been doing something else wrong. I'll give it another shot. Thank
you.



 
Reply With Quote
 
Greg Esres
Guest
Posts: n/a
 
      05-23-2007
<<preceding-sibling::text()>>

I thought I had tried that. I had about decided that "siblings" were
only of the same node type. I'll try again, thanks.

 
Reply With Quote
 
Greg Esres
Guest
Posts: n/a
 
      05-23-2007
Peter Flynn wrote:

<<Don't ever do this. It will only end in tears and recriminations.
Unless
you really know what you are doing with text-document markup, put your
free-floating text inside some markup, eg>>

Argh, that seems impractical....the node is essentially all text, with
markup on only a few sections (in theory). Surely you wouldn't find
anything wrong with this HTML:

<div>
This is a bunch of "free floating text" followed by <b>something</
b> in bold that is just a
short section.
</div>

I don't see a fundamental difference between that and mine. (I'm
willing to learn, though, if you care to explain further.)

<<If what you showed is is stuff you've been sent, send it back and
suggest they get someone to mark it up sensibly before you process
it.>>

Sorry, it's my own. However, in the real life application, the
free floating texts is several paragraphs of text, interspersed with
other markup items.


 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      05-23-2007
Greg Esres wrote:
> <<Don't ever do this. It will only end in tears and recriminations.
> Unless
> you really know what you are doing with text-document markup, put your
> free-floating text inside some markup, eg>>
>
> Argh, that seems impractical....


I agree that "don't ever do this" is overstated at best.

There are two approaches to using XML. One is very much data-oriented,
and for that the advice to give text its own elements may make sense.
(Or may not, depending on exactly what you're trying to represent.) The
other is text-document-oriented -- which is where XML, HTML, and SGML
started -- and does want to intermix text and markup.

XML, and XML tools, will handle either. The document-oriented approach
is sometimes a bit more work because -- obviously -- text nodes don't
have names, and because they force you to think about whether whitespace
is or isn't meaningful... and, in SAX, because you have to be aware of
buffering effects. But that doesn't make it wrong, just different.


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      05-23-2007
Greg Esres wrote:
> <<preceding-sibling::text()>>
>
> I thought I had tried that. I had about decided that "siblings" were
> only of the same node type. I'll try again, thanks.


Siblings are other children of the same parent, which means they can be
anything but attributes (depending on the context you're starting from).
Type constraints are imposed explicitly by whatever follows the axis
specifier -- in this example, text().

Note that the expression given above finds all text nodes before this
one. If you want the nearest preceding text node, that would be
preceding-sibling::text()[1]. If you want the preceding node if and only
if it is a text node, one way to say that would be
preceding-sibling::node()[1][self::text()]

XSLT's a programming language. If you want robust results, you need to
know exactly what you're going to see as input, describe exactly what
you want to retrieve from it, or (preferably) both.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
Looking A Nodes From Within Nodes Johnny Ooi XML 10 11-14-2004 06:55 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments