Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > upper-casing parts of xpath

Reply
Thread Tools

upper-casing parts of xpath

 
 
Johannes Koch
Guest
Posts: n/a
 
      09-17-2004
Hi there,

I'd like to apply an xpath to both HTML and XHTML documents. First I
create a DOM document with a Java DOM parser, then apply the xpath with
Xalan's XPathAPI class. The problem is that in HTML DOM element names
are all upper-case, whereas in Core DOM (used for the XHTML documents)
element names are lower-case. When I use a lower-case xpath, e.g.

/head[@profile='http://www.example.org/MyProfile']

it won't match with a head element in an HTML document. OTOH, when I use

/HEAD[@profile='http://www.example.org/MyProfile']

it won't match with a head element in an XHTML document.

I cannot make the whole xpath lower-case in case of an XHTML document,
because there may be case-sensitive things in the xpath, like the URL in
the example above.

There may be some Java classes to parse the xpath string and get the
element names to make them upper-case for HTML. Does anyone know of such
things?
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
 
Reply With Quote
 
 
 
 
Philippe Poulard
Guest
Posts: n/a
 
      09-17-2004
Johannes Koch wrote:
> Hi there,
>
> I'd like to apply an xpath to both HTML and XHTML documents. First I
> create a DOM document with a Java DOM parser, then apply the xpath with
> Xalan's XPathAPI class. The problem is that in HTML DOM element names
> are all upper-case, whereas in Core DOM (used for the XHTML documents)
> element names are lower-case. When I use a lower-case xpath, e.g.
>
> /head[@profile='http://www.example.org/MyProfile']
>
> it won't match with a head element in an HTML document. OTOH, when I use
>
> /HEAD[@profile='http://www.example.org/MyProfile']
>
> it won't match with a head element in an XHTML document.
>
> I cannot make the whole xpath lower-case in case of an XHTML document,
> because there may be case-sensitive things in the xpath, like the URL in
> the example above.
>
> There may be some Java classes to parse the xpath string and get the
> element names to make them upper-case for HTML. Does anyone know of such
> things?


hi,

maybe you have to plug a sax parser that do the job before building the
DOM model ?

another solution is to use Jaxen instead of Xalan's XPathAPI ; instead
of parsing xpath expressions with jaxen.dom.DOMXPath, you can parse them
with a copy of the package jaxen.dom.*, for example koch.dom.* ; the
main class is DocumentNavigator, that you have to extend to write your
own methods, such as getElementName() that should give upper-case names...
easy !

the last solution is to use Jaxen again, and write your own SAXPath
parser ; but i don't know really where to act exactly
--
Cordialement,

///
(. .)
-----ooO--(_)--Ooo-----
| Philippe Poulard |
-----------------------
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      09-17-2004
In article <(E-Mail Removed)>,
Johannes Koch <(E-Mail Removed)> wrote:

> /head[@profile='http://www.example.org/MyProfile']
> /HEAD[@profile='http://www.example.org/MyProfile']


You could (rather tediously) write

/*[local-name()='HEAD' or local-name='head'][@profile='http://www.example.org/MyProfile']

-- Richard
 
Reply With Quote
 
Kenneth Stephen
Guest
Posts: n/a
 
      09-18-2004
Johannes Koch wrote:

> Hi there,
>
> I'd like to apply an xpath to both HTML and XHTML documents. First I
> create a DOM document with a Java DOM parser, then apply the xpath with
> Xalan's XPathAPI class. The problem is that in HTML DOM element names
> are all upper-case, whereas in Core DOM (used for the XHTML documents)
> element names are lower-case. When I use a lower-case xpath, e.g.
>
> /head[@profile='http://www.example.org/MyProfile']
>
> it won't match with a head element in an HTML document. OTOH, when I use
>
> /HEAD[@profile='http://www.example.org/MyProfile']
>

Hi,

You could write a pre-processor XSL program that converts all uppercase
tags to lower-case tags. And then feed the output to your regular
program. Shown below is an example of such a pre-processor (warning :
not extensively tested. use at your own risk) :

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="*">
<xsl:variable name="elementName"
select="translate(local-name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklm nopqrstuvwxyz')"
/>
<xsl:element name="{$elementName}">
<xsl:for-each select="@*">
<xsl:apply-templates select="." />
</xsl:for-each>
<xsl:apply-templates />
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:variable name="attrName"
select="translate(local-name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklm nopqrstuvwxyz')"
/>
<xsl:attribute name="{$attrName}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>

<xsl:template match="text()">
<xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>

Regards,
Kenneth
 
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
Auto Parts Warehouse and Parts Train Services kizk C Programming 0 08-02-2009 07:26 AM
Serious issue: parts of my page render as not logged in, parts as logged in. Help! pcloches@gmail.com ASP .Net 1 04-12-2007 12:50 AM
Difference between "library parts" of C99 and "language parts" of C99 albert.neu@gmail.com C Programming 3 03-31-2007 08:14 PM
Parts parts....PARTS!!! ARGHHH dstvns A+ Certification 8 01-07-2004 07:57 PM
How come most multi parts dont have all parts? 123 Computer Support 2 09-22-2003 01:55 PM



Advertisments