upper-casing parts of xpath

Discussion in 'XML' started by Johannes Koch, Sep 17, 2004.

  1. 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.)
    Johannes Koch, Sep 17, 2004
    #1
    1. Advertising

  2. 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 |
    -----------------------
    Philippe Poulard, Sep 17, 2004
    #2
    1. Advertising

  3. In article <>,
    Johannes Koch <> 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
    Richard Tobin, Sep 17, 2004
    #3
  4. 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"
    xmlns:xsl="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','abcdefghijklmnopqrstuvwxyz')"
    />
    <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','abcdefghijklmnopqrstuvwxyz')"
    />
    <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
    Kenneth Stephen, Sep 18, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kaeli
    Replies:
    8
    Views:
    592
    Chris Smith
    Nov 18, 2004
  2. somenath

    Question regarding malloc casing

    somenath, Dec 2, 2007, in forum: C Programming
    Replies:
    10
    Views:
    506
    Joe Wright
    Dec 3, 2007
  3. Darren Spruell

    urllib2 header casing discrepancy

    Darren Spruell, Feb 23, 2011, in forum: Python
    Replies:
    0
    Views:
    387
    Darren Spruell
    Feb 23, 2011
  4. Replies:
    14
    Views:
    194
  5. BlackHelicopter
    Replies:
    0
    Views:
    515
    BlackHelicopter
    Jan 31, 2013
Loading...

Share This Page