diff options
Diffstat (limited to 'ext/dom/examples')
-rw-r--r-- | ext/dom/examples/dom1.inc | 43 | ||||
-rw-r--r-- | ext/dom/examples/dom1.php | 94 | ||||
-rw-r--r-- | ext/dom/examples/note-invalid.xml | 9 | ||||
-rw-r--r-- | ext/dom/examples/note.dtd | 6 | ||||
-rw-r--r-- | ext/dom/examples/note.php | 19 | ||||
-rw-r--r-- | ext/dom/examples/note.xml | 8 | ||||
-rw-r--r-- | ext/dom/examples/relaxNG.php | 11 | ||||
-rw-r--r-- | ext/dom/examples/relaxNG.rng | 11 | ||||
-rw-r--r-- | ext/dom/examples/relaxNG.xml | 1 | ||||
-rw-r--r-- | ext/dom/examples/relaxNG2.rng | 23 | ||||
-rw-r--r-- | ext/dom/examples/relaxNG3.rng | 8 | ||||
-rw-r--r-- | ext/dom/examples/shipping.php | 11 | ||||
-rw-r--r-- | ext/dom/examples/shipping.xml | 21 | ||||
-rw-r--r-- | ext/dom/examples/shipping.xsd | 36 |
14 files changed, 301 insertions, 0 deletions
diff --git a/ext/dom/examples/dom1.inc b/ext/dom/examples/dom1.inc new file mode 100644 index 0000000..792d6f2 --- /dev/null +++ b/ext/dom/examples/dom1.inc @@ -0,0 +1,43 @@ +<?PHP +$xmlstr = "<?xml version='1.0' standalone='yes'?> +<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd' +[ <!ENTITY sp \"spanish\"> +]> +<!-- lsfj --> +<chapter language='en'><title language='en'>Title</title> +<para language='ge'> +&sp; +<!-- comment --> +<informaltable language='&sp;kkk'> +<tgroup cols='3'> +<tbody> +<row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row> +<row><entry>a2</entry><entry>c2</entry></row> +<row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row> +</tbody> +</tgroup> +</informaltable> +</para> +</chapter> "; + +function print_node($node) +{ + print "Node Name: " . $node->nodeName; + print "\nNode Type: " . $node->nodeType; + $child_count = $node->childNodes->length; + print "\nNum Children: " . $child_count; + if($child_count <= 1){ + print "\nNode Content: " . $node->nodeValue; + } + print "\n\n"; +} + +function print_node_list($nodelist) +{ + foreach($nodelist as $node) + { + print_node($node); + } +} + +?> diff --git a/ext/dom/examples/dom1.php b/ext/dom/examples/dom1.php new file mode 100644 index 0000000..8ea3674 --- /dev/null +++ b/ext/dom/examples/dom1.php @@ -0,0 +1,94 @@ +<?php +require_once("dom1.inc"); + +echo "Test 1: accessing single nodes from php\n"; +$dom = new domDocument; +$dom->loadxml($xmlstr); +if(!$dom) { + echo "Error while parsing the document\n"; + exit; +} + +// children() of of document would result in a memleak +//$children = $dom->children(); +//print_node_list($children); + +echo "--------- root\n"; +$rootnode = $dom->documentElement; +print_node($rootnode); + +echo "--------- children of root\n"; +$children = $rootnode->childNodes; +print_node_list($children); + +// The last node should be identical with the last entry in the children array +echo "--------- last\n"; +$last = $rootnode->lastChild; +print_node($last); + +// The parent of this last node is the root again +echo "--------- parent\n"; +$parent = $last->parentNode; +print_node($parent); + +// The children of this parent are the same children as one above +echo "--------- children of parent\n"; +$children = $parent->childNodes; +print_node_list($children); + +echo "--------- creating a new attribute\n"; +//This is worthless +//$attr = $dom->createAttribute("src", "picture.gif"); +//print_r($attr); + +//$rootnode->set_attributeNode($attr); +$attr = $rootnode->setAttribute("src", "picture.gif"); +$attr = $rootnode->getAttribute("src"); +print_r($attr); +print "\n"; + +echo "--------- Get Attribute Node\n"; +$attr = $rootnode->getAttributeNode("src"); +print_node($attr); + +echo "--------- Remove Attribute Node\n"; +$attr = $rootnode->removeAttribute("src"); +print "Removed " . $attr . " attributes.\n"; + +echo "--------- attributes of rootnode\n"; +$attrs = $rootnode->attributes; +print_node_list($attrs); + +echo "--------- children of an attribute\n"; +$children = $attrs->item(0)->childNodes; +print_node_list($children); + +echo "--------- Add child to root\n"; +$myelement = new domElement("Silly", "Symphony"); +$newchild = $rootnode->appendChild($myelement); +print_node($newchild); +print $dom->saveXML(); +print "\n"; + +echo "--------- Find element by tagname\n"; +echo " Using dom\n"; +$children = $dom->getElementsByTagname("Silly"); +print_node_list($children); + +echo " Using elem\n"; +$children = $rootnode->getElementsByTagName("Silly"); +print_node_list($children); + +echo "--------- Unlink Node\n"; +print_node($children->item(0)); +$rootnode->removeChild($children->item(0)); +print_node_list($rootnode->childNodes); +print $dom->savexml(); + +echo "--------- Find element by id\n"; +print ("Not implemented\n"); + +echo "--------- Check various node_name return values\n"; +print ("Not needed\n"); + +?> diff --git a/ext/dom/examples/note-invalid.xml b/ext/dom/examples/note-invalid.xml new file mode 100644 index 0000000..58d4e65 --- /dev/null +++ b/ext/dom/examples/note-invalid.xml @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<!DOCTYPE note SYSTEM "note.dtd"> +<note> +<to>PHP User Group</to> +<from>Shane</from> +<heading>Reminder</heading> +<body>Don't forget the meeting tonight!</body> +<footer>Or I'll clobber you!</footer> +</note> diff --git a/ext/dom/examples/note.dtd b/ext/dom/examples/note.dtd new file mode 100644 index 0000000..c2d558e --- /dev/null +++ b/ext/dom/examples/note.dtd @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8" ?> +<!ELEMENT note (to,from,heading,body)> +<!ELEMENT to (#PCDATA)> +<!ELEMENT from (#PCDATA)> +<!ELEMENT heading (#PCDATA)> +<!ELEMENT body (#PCDATA)> diff --git a/ext/dom/examples/note.php b/ext/dom/examples/note.php new file mode 100644 index 0000000..a8695f3 --- /dev/null +++ b/ext/dom/examples/note.php @@ -0,0 +1,19 @@ +<?php + +$dom = new domDocument; +$dom->load('note.xml'); +if (!$dom->validate('note.dtd')) { + print "Document note.dtd is not valid\n"; +} else { + print "Document note.dtd is valid\n"; +} + +$dom = new domDocument; +$dom->load('note-invalid.xml'); +if (!$dom->validate('note.dtd')) { + print "Document note-invalid.xml is not valid\n"; +} else { + print "Document note-invalid.xml is valid\n"; +} + +?> diff --git a/ext/dom/examples/note.xml b/ext/dom/examples/note.xml new file mode 100644 index 0000000..49614a1 --- /dev/null +++ b/ext/dom/examples/note.xml @@ -0,0 +1,8 @@ +<?xml version="1.0"?> +<!DOCTYPE note SYSTEM "note.dtd"> +<note> +<to>PHP User Group</to> +<from>Shane</from> +<heading>Reminder</heading> +<body>Don't forget the meeting tonight!</body> +</note> diff --git a/ext/dom/examples/relaxNG.php b/ext/dom/examples/relaxNG.php new file mode 100644 index 0000000..d265fd9 --- /dev/null +++ b/ext/dom/examples/relaxNG.php @@ -0,0 +1,11 @@ +<?php + +$dom = new domDocument; +$dom->load('relaxNG.xml'); +if (!$dom->relaxNGValidate('relaxNG.rng')) { + print "Document is not valid"; +} else { + print "Document is valid"; +} + +?>
\ No newline at end of file diff --git a/ext/dom/examples/relaxNG.rng b/ext/dom/examples/relaxNG.rng new file mode 100644 index 0000000..f4357e0 --- /dev/null +++ b/ext/dom/examples/relaxNG.rng @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="iso-8859-1"?> +<grammar xmlns="http://relaxng.org/ns/structure/1.0" + datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> + +<include href="relaxNG2.rng"> +<define name="TEI.prose"><ref name="INCLUDE"/></define> +</include> +</grammar> + + + diff --git a/ext/dom/examples/relaxNG.xml b/ext/dom/examples/relaxNG.xml new file mode 100644 index 0000000..6b0cac1 --- /dev/null +++ b/ext/dom/examples/relaxNG.xml @@ -0,0 +1 @@ +<TEI.2>hello</TEI.2>
\ No newline at end of file diff --git a/ext/dom/examples/relaxNG2.rng b/ext/dom/examples/relaxNG2.rng new file mode 100644 index 0000000..4adae7b --- /dev/null +++ b/ext/dom/examples/relaxNG2.rng @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:t="http://www.thaiopensource.com/ns/annotations" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> + + <start> + <ref name="TEI.2"/> + </start> + <define name="IGNORE"> + <notAllowed/> + </define> + <define name="INCLUDE"> + <empty/> + </define> + + + <include href="relaxNG3.rng"/> + + <define name="TEI.2"> + <element name="TEI.2"> + <text/> + </element> + </define> + +</grammar>
\ No newline at end of file diff --git a/ext/dom/examples/relaxNG3.rng b/ext/dom/examples/relaxNG3.rng new file mode 100644 index 0000000..73e1eb6 --- /dev/null +++ b/ext/dom/examples/relaxNG3.rng @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8"?> +<grammar xmlns="http://relaxng.org/ns/structure/1.0" xmlns:t="http://www.thaiopensource.com/ns/annotations" xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> + + <define name="TEI.prose" combine="interleave"> + <ref name="IGNORE"/> + </define> + +</grammar>
\ No newline at end of file diff --git a/ext/dom/examples/shipping.php b/ext/dom/examples/shipping.php new file mode 100644 index 0000000..5205fd2 --- /dev/null +++ b/ext/dom/examples/shipping.php @@ -0,0 +1,11 @@ +<?php + +$dom = new domDocument; +$dom->load('shipping.xml'); +if (!$dom->schemaValidate('shipping.xsd')) { + print "Document is not valid"; +} else { + print "Document is valid"; +} + +?>
\ No newline at end of file diff --git a/ext/dom/examples/shipping.xml b/ext/dom/examples/shipping.xml new file mode 100644 index 0000000..dc8a09e --- /dev/null +++ b/ext/dom/examples/shipping.xml @@ -0,0 +1,21 @@ +<?xml version="1.0"?> +<shipOrder> + <shipTo> + <name>Tove Svendson</name> + <street>Ragnhildvei 2</street> + <address>4000 Stavanger</address> + <country>Norway</country> + </shipTo> + <items> + <item> + <title>Empire Burlesque</title> + <quantity>1</quantity> + <price>10.90</price> + </item> + <item> + <title>Hide your heart</title> + <quantity>1</quantity> + <price>9.90</price> + </item> + </items> +</shipOrder>
\ No newline at end of file diff --git a/ext/dom/examples/shipping.xsd b/ext/dom/examples/shipping.xsd new file mode 100644 index 0000000..8b16b7c --- /dev/null +++ b/ext/dom/examples/shipping.xsd @@ -0,0 +1,36 @@ +<?xml version="1.0"?> +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> + + <xsd:element name="shipOrder" type="order"/> + + <xsd:complexType name="order"> + <xsd:all> + <xsd:element name="shipTo" type="shipAddress"/> + <xsd:element name="items" type="cdItems"/> + </xsd:all> + </xsd:complexType> + + <xsd:complexType name="shipAddress"> + <xsd:all> + <xsd:element name="name" type="xsd:string"/> + <xsd:element name="street" type="xsd:string"/> + <xsd:element name="address" type="xsd:string"/> + <xsd:element name="country" type="xsd:string"/> + </xsd:all> + </xsd:complexType> + + <xsd:complexType name="cdItems"> + <xsd:sequence> + <xsd:element name="item" type="cdItem" maxOccurs="unbounded" minOccurs="1"/> + </xsd:sequence> + </xsd:complexType> + + <xsd:complexType name="cdItem"> + <xsd:all> + <xsd:element name="title" type="xsd:string"/> + <xsd:element name="quantity" type="xsd:positiveInteger"/> + <xsd:element name="price" type="xsd:decimal"/> + </xsd:all> + </xsd:complexType> + +</xsd:schema>
\ No newline at end of file |