2

I'm trying to get html tags that start with uppercase using DOMDocument in PHP 5.3.

I'm using a php function registered in XPath to test it, but the function is receiving as first parameters tagNames in lowercase.

The xml:

 <test>
     <A>Match this</A>
     <b>Dont match this</b>
 </test>

The php function:

registerPhpFunctions - phpDoc

...
public function isUpper($name) {
    return (bool)preg_match('/^[A-Z]/', $name);
}
...

Ant this is the Xpath:

//*[php:function("\Cdr\Dom\DOMXPath::isUpper", name())]

the function isUpper receives $name in lowercase so it don't works.

My questions are:

  1. Why isn't case sensitive?
  2. There is a better way to do this?
hakre
  • 193,403
  • 52
  • 435
  • 836
Wiliam
  • 3,714
  • 7
  • 36
  • 56

3 Answers3

0

Load the code as XML and not HTML. The HTML is not case-sensitive.

$xmlDoc->loadXML('<html>');

instead of:

$xmlDoc->loadHTML('<html>');
Wiliam
  • 3,714
  • 7
  • 36
  • 56
0

Use this one-liner:

//*[contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', substring(name(),1,1))]

this selects any element in the XML document, the first character of whose name is contained in the string of all capital letters.

XSLT - based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  "//*[contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', substring(name(),1,1))]"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<test>
    <A>Match this</A>
    <b>Dont match this</b>
</test>

the XPath expression is evaluated and the selected nodes (in this case just one) are copied to the output:

<A>Match this</A>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Unrelated, but here you have a hassle-free approach: http://fsockopen.com/php-programming/your-final-stop-for-php-xpath-case-insensitive – Chris Russo Aug 17 '16 at 04:15
0

A complete working example (test.php):

$doc = new DOMDocument;
$doc->load('test.xml');

$xpath = new DOMXPath($doc);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions("isUpper");

function isUpper($name) {
    return (bool)preg_match('/^[A-Z]/', $name);
}

$els = $xpath->query('//*[php:function("isUpper", name())]');

foreach ($els as $el) {
    echo $el->nodeValue . "\n";
}

test.xml:

<test>
    <A>Match this</A>
    <b>Dont match this</b>
</test>

Output:

lwburk$ php test.php 
Match this
Wayne
  • 59,728
  • 15
  • 131
  • 126
  • Unrelated, but here you have a hassle-free approach: http://fsockopen.com/php-programming/your-final-stop-for-php-xpath-case-insensitive – Chris Russo Aug 17 '16 at 04:15