File: registerXPathNamespace.xml

package info (click to toggle)
php-doc 20140201-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,084 kB
  • ctags: 4,040
  • sloc: xml: 998,137; php: 20,812; cpp: 500; sh: 177; makefile: 63; awk: 28
file content (153 lines) | stat: -rw-r--r-- 4,755 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 311775 $ -->
<refentry xml:id="simplexmlelement.registerxpathnamespace" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>SimpleXMLElement::registerXPathNamespace</refname>
  <refpurpose>
   Creates a prefix/ns context for the next XPath query
  </refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier> <type>bool</type><methodname>SimpleXMLElement::registerXPathNamespace</methodname>
   <methodparam><type>string</type><parameter>prefix</parameter></methodparam>
   <methodparam><type>string</type><parameter>ns</parameter></methodparam>
  </methodsynopsis>
  <para>
   Creates a prefix/ns context for the next XPath query. In particular, this is
   helpful if the provider of the given XML document alters the namespace
   prefixes. <literal>registerXPathNamespace</literal> will create a prefix for
   the associated namespace, allowing one to access nodes in that namespace 
   without the need to change code to allow for the new prefixes dictated by the
   provider.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>prefix</parameter></term>
     <listitem>
      <para>
       The namespace prefix to use in the XPath query for the namespace given in 
       <parameter>ns</parameter>.
      </para>
     </listitem>
    </varlistentry>
    <varlistentry>
     <term><parameter>ns</parameter></term>
     <listitem>
      <para>
       The namespace to use for the XPath query. This must match a namespace in
       use by the XML document or the XPath query using 
       <parameter>prefix</parameter> will not return any results.
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   &return.success;
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>Setting a namespace prefix to use in an XPath query</title>
    <programlisting role="php">
<![CDATA[
<?php

$xml = <<<EOD
<book xmlns:chap="http://example.org/chapter-title">
    <title>My Book</title>
    <chapter id="1">
        <chap:title>Chapter 1</chap:title>
        <para>Donec velit. Nullam eget tellus vitae tortor gravida scelerisque. 
            In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci. 
            Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut, 
            ultricies id, mauris.</para>
    </chapter>
    <chapter id="2">
        <chap:title>Chapter 2</chap:title>
        <para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin 
            gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam 
            vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros 
            tellus, pharetra id, faucibus eu, dapibus dictum, odio.</para>
    </chapter>
</book>
EOD;

$sxe = new SimpleXMLElement($xml);

$sxe->registerXPathNamespace('c', 'http://example.org/chapter-title');
$result = $sxe->xpath('//c:title');

foreach ($result as $title) {
  echo $title . "\n";
}

?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
Chapter 1
Chapter 2
]]>
    </screen>
    <para>
     Notice how the XML document shown in the example sets a namespace with a 
     prefix of <literal>chap</literal>. Imagine that this document (or another
     one like it) may have used a prefix of <literal>c</literal> in the past for
     the same namespace. Since it has changed, the XPath query will no longer 
     return the proper results and the query will require modification. Using 
     <literal>registerXPathNamespace</literal> avoids future modification of the
     query even if the provider changes the namespace prefix.
    </para>
   </example>
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><methodname>SimpleXMLElement::xpath</methodname></member>
    <member><methodname>SimpleXMLElement::getDocNamespaces</methodname></member>
    <member><methodname>SimpleXMLElement::getNamespaces</methodname></member>
   </simplelist>
  </para>
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->