Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XHTML vs. XPath: @class match?

Reply
Thread Tools

XHTML vs. XPath: @class match?

 
 
Ivan Shmakov
Guest
Posts: n/a
 
      02-06-2013
Given that the "class" attribute is "a value that is a set of
space-separated tokens" [1], is there an easy way to match
elements of a particular class in XPath? Unfortunately,
//node ()[@class = "foo"] doesn't seem to fit.

TIA.

[1] http://www.w3.org/TR/html5/dom.html#classes

PS. I'm using libxml2 for XPath support.

--
FSF associate member #7257
 
Reply With Quote
 
 
 
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      02-06-2013
* Ivan Shmakov wrote in comp.text.xml:
> Given that the "class" attribute is "a value that is a set of
> space-separated tokens" [1], is there an easy way to match
> elements of a particular class in XPath? Unfortunately,
> //node ()[@class = "foo"] doesn't seem to fit.


That works with XPath 2.0 if the attribute is known to be an NMTOKENS
attribute through schema information, but with XPath 1.0 you have to
use something like

contains(' foo ', concat(' ', normalize-space(@class), ' '))

To account for the various possible cases, with some caveats like the
definition of white space being different between HTML and XPath.
--
Björn Höhrmann · (E-Mail Removed) · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
 
Reply With Quote
 
 
 
 
Ivan Shmakov
Guest
Posts: n/a
 
      02-06-2013
>>>>> Bjoern Hoehrmann <(E-Mail Removed)> writes:
>>>>> * Ivan Shmakov wrote in comp.text.xml:


>> Given that the "class" attribute is "a value that is a set of
>> space-separated tokens" [1], is there an easy way to match elements
>> of a particular class in XPath? Unfortunately,
>> //node ()[@class = "foo"] doesn't seem to fit.


> That works with XPath 2.0 if the attribute is known to be an NMTOKENS
> attribute through schema information, but with XPath 1.0 you have to
> use something like


> contains(' foo ', concat(' ', normalize-space(@class), ' '))


ACK, thanks! I guess that with libxml2 I'm stuck to XPath 1.0.

> To account for the various possible cases, with some caveats like the
> definition of white space being different between HTML and XPath.


Which are "(#x20 | #x9 | #xD | #xA)" (as per XML 1.0),
vs. (#xC | #x20 | #x9 | #xD | #xA) (as per HTML5.)

Isn't all that bad (especially given that the document I'm
processing is an XHTML template, to be shipped with the
application I'm working on); one of them could've been allowing
the whole host of Unicode whitespace characters, too.

Do I understand it correctly that explicitly translate'ing &#xC
to ' ' would be the proper solution? Or perhaps translate ()
may render normalize-space () unnecessary /in this case/?
Consider, e. g.:

contains (' foo ',
concat (' ', translate (@class, ' ', ' '), ' '))

TIA.

--
<!DOCTYPE xml><xml the=" e"><tensible ribbon="campaign" /><p>Advocating
the judicious use of XML applications on the Internet at large.</p></xml>
 
Reply With Quote
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      02-06-2013
* Ivan Shmakov wrote in comp.text.xml:
> Which are "(#x20 | #x9 | #xD | #xA)" (as per XML 1.0),
> vs. (#xC | #x20 | #x9 | #xD | #xA) (as per HTML5.)
>
> Isn't all that bad (especially given that the document I'm
> processing is an XHTML template, to be shipped with the
> application I'm working on); one of them could've been allowing
> the whole host of Unicode whitespace characters, too.
>
> Do I understand it correctly that explicitly translate'ing &#xC
> to ' ' would be the proper solution? Or perhaps translate ()
> may render normalize-space () unnecessary /in this case/?
> Consider, e. g.:


You can use translate() in place of normalize-space() to normalize white
space to simple spaces, but it might not be possible to handle the case
of U+000C since that's not a valid character in XPath expressions and no
escaping mechanism exists in XPath 1.0 (and neither is there a function
to get the Unicode character numbers which would be a possible solution
otherwise).
--
Björn Höhrmann · (E-Mail Removed) · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
 
Reply With Quote
 
Ivan Shmakov
Guest
Posts: n/a
 
      02-06-2013
>>>>> Bjoern Hoehrmann <(E-Mail Removed)> writes:
>>>>> * Ivan Shmakov wrote in comp.text.xml:


>> Which are "(#x20 | #x9 | #xD | #xA)" (as per XML 1.0), vs. (#xC |
>> #x20 | #x9 | #xD | #xA) (as per HTML5.)


[...]

>> Do I understand it correctly that explicitly translate'ing &#xC to
>> ' ' would be the proper solution? Or perhaps translate () may
>> render normalize-space () unnecessary /in this case/?


[...]

> You can use translate() in place of normalize-space () to normalize
> white space to simple spaces, but it might not be possible to handle
> the case of U+000C since that's not a valid character in XPath
> expressions and no escaping mechanism exists in XPath 1.0 (and
> neither is there a function to get the Unicode character numbers
> which would be a possible solution otherwise).


ACK, thanks.

Though, as I've just found, the whole point is moot, for U+000C
is not a valid character in XML 1.0, either [1], and thus no
XHTML document may ever contain one, whether in @class, or any
other place.

[1] http://www.w3.org/TR/REC-xml/#charsets

--
FSF associate member #7257 np. Brave New World -- Iron Maiden
 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      02-09-2013
On 02/06/2013 06:25 PM, Bjoern Hoehrmann wrote:
> * Ivan Shmakov wrote in comp.text.xml:
>> Given that the "class" attribute is "a value that is a set of
>> space-separated tokens" [1], is there an easy way to match
>> elements of a particular class in XPath? Unfortunately,
>> //node ()[@class = "foo"] doesn't seem to fit.

>
> That works with XPath 2.0 if the attribute is known to be an NMTOKENS
> attribute through schema information


Does it also work if the attribute has been declared as IDREFS?

///Peter


 
Reply With Quote
 
Bjoern Hoehrmann
Guest
Posts: n/a
 
      02-09-2013
* Peter Flynn wrote in comp.text.xml:
>On 02/06/2013 06:25 PM, Bjoern Hoehrmann wrote:
>> * Ivan Shmakov wrote in comp.text.xml:
>>> Given that the "class" attribute is "a value that is a set of
>>> space-separated tokens" [1], is there an easy way to match
>>> elements of a particular class in XPath? Unfortunately,
>>> //node ()[@class = "foo"] doesn't seem to fit.

>>
>> That works with XPath 2.0 if the attribute is known to be an NMTOKENS
>> attribute through schema information

>
>Does it also work if the attribute has been declared as IDREFS?


Good question. I gave up researching this when I found that there is no
constructor function xs:IDREFS defined in the 2.0 specifications, but in
the 3.0 proposals there is one. So for 3.0 I suspect "yes", but I don't
know about 2.0.
--
Björn Höhrmann · (E-Mail Removed) · http://bjoern.hoehrmann.de
Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      02-17-2013
On 02/09/2013 11:13 PM, Bjoern Hoehrmann wrote:
> * Peter Flynn wrote in comp.text.xml:
>> On 02/06/2013 06:25 PM, Bjoern Hoehrmann wrote:
>>> * Ivan Shmakov wrote in comp.text.xml:
>>>> Given that the "class" attribute is "a value that is a set of
>>>> space-separated tokens" [1], is there an easy way to match
>>>> elements of a particular class in XPath? Unfortunately,
>>>> //node ()[@class = "foo"] doesn't seem to fit.
>>>
>>> That works with XPath 2.0 if the attribute is known to be an NMTOKENS
>>> attribute through schema information

>>
>> Does it also work if the attribute has been declared as IDREFS?

>
> Good question. I gave up researching this when I found that there is no
> constructor function xs:IDREFS defined in the 2.0 specifications, but in
> the 3.0 proposals there is one. So for 3.0 I suspect "yes", but I don't
> know about 2.0.


Excellent, thanks. Some of us still have clients using ID/IDREF

///Peter

 
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
PSD to XHTML Conversion, PSD to HTML, Joomla, Drupal, WordpressConversion, PSD to XHTML CSS xhtml champs XML 0 08-02-2011 05:40 AM
PSD to XHTML Conversion, PSD to HTML, Joomla, Drupal, WordpressConversion, PSD to XHTML CSS xhtml champs C Programming 0 08-01-2011 06:29 AM
convert xhtml to another xhtml using xslt Usha2009 XML 0 12-20-2009 01:13 PM
Should I Convert Site To XHTML or XHTML mobile? chronos3d HTML 9 12-05-2006 04:46 PM
parse URL (href) from xhtml, xhtml -> text, for data hawat.thufir@gmail.com XML 7 02-08-2006 07:39 PM



Advertisments