@ 2013-10-30T12:21:48Z <?php
$schema = <<<SCHEMA
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="item" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="is-foo" type="xs:boolean" default="true" use="optional" />
<xs:attribute name="is-bar" type="xs:boolean" default="false" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
SCHEMA;
$xml = <<<XML
<item is-foo="false" is-bar="false">
<item is-bar="true">
<item>
<item is-foo="true">
<item />
<item is-foo="true" />
</item>
</item>
<item is-foo="true" is-bar="true" />
</item>
</item>
XML;
$document = new DOMDocument();
$document->loadXML($xml);
$document->schemaValidateSource($schema, LIBXML_SCHEMA_CREATE);
$document->resolveExternals = true;
$document->substituteEntities = true;
$xpath = new DOMXPath($document);
foreach ($xpath->query('//item') as $item) {
echo $item->getNodePath() . PHP_EOL
. ' is-foo : ' . ($item->getAttribute('is-foo') === 'true' ? 'true' : 'false') . PHP_EOL
. ' is-bar : ' . ($item->getAttribute('is-bar') === 'true' ? 'true' : 'false') . PHP_EOL;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.5.2 - 5.5.38 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.5 - 7.2.34 , 7.3.12 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.30 , 8.2.0 - 8.2.25 , 8.3.0 - 8.3.13 /item
is-foo : false
is-bar : false
/item/item
is-foo : true
is-bar : true
/item/item/item[1]
is-foo : true
is-bar : false
/item/item/item[1]/item
is-foo : true
is-bar : false
/item/item/item[1]/item/item[1]
is-foo : true
is-bar : false
/item/item/item[1]/item/item[2]
is-foo : true
is-bar : false
/item/item/item[2]
is-foo : true
is-bar : true
Output for 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.1 Notice: Use of undefined constant LIBXML_SCHEMA_CREATE - assumed 'LIBXML_SCHEMA_CREATE' in /in/qCdY3 on line 33
Warning: DOMDocument::schemaValidateSource() expects exactly 1 parameter, 2 given in /in/qCdY3 on line 33
/item
is-foo : false
is-bar : false
/item/item
is-foo : false
is-bar : true
/item/item/item[1]
is-foo : false
is-bar : false
/item/item/item[1]/item
is-foo : true
is-bar : false
/item/item/item[1]/item/item[1]
is-foo : false
is-bar : false
/item/item/item[1]/item/item[2]
is-foo : true
is-bar : false
/item/item/item[2]
is-foo : true
is-bar : true
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.1 , 5.1.3 - 5.1.6 Notice: Use of undefined constant LIBXML_SCHEMA_CREATE - assumed 'LIBXML_SCHEMA_CREATE' in /in/qCdY3 on line 33
Warning: DOMDocument::schemaValidateSource() expects exactly 1 parameter, 2 given in /in/qCdY3 on line 33
Fatal error: Call to undefined method DOMElement::getNodePath() in /in/qCdY3 on line 39
Process exited with code 255 . Output for 5.1.2 Notice: Use of undefined constant LIBXML_SCHEMA_CREATE - assumed 'LIBXML_SCHEMA_CREATE' in /in/qCdY3 on line 35
Warning: DOMDocument::schemaValidateSource() expects exactly 1 parameter, 2 given in /in/qCdY3 on line 35
Fatal error: Call to undefined method DOMElement::getNodePath() in /in/qCdY3 on line 41
Process exited with code 255 . Output for 4.3.2 - 4.3.11 , 4.4.0 - 4.4.1 , 4.4.3 - 4.4.9 Fatal error: Cannot instantiate non-existent class: domdocument in /in/qCdY3 on line 31
Process exited with code 255 . Output for 4.4.2 Fatal error: Cannot instantiate non-existent class: domdocument in /in/qCdY3 on line 33
Process exited with code 255 . Output for 4.3.0 - 4.3.1 Fatal error: Cannot instantiate non-existent class: domdocument in /in/qCdY3 on line 31
preferences:dark mode live preview
71.08 ms | 414 KiB | 5 Q