xpath_eval
xpath_eval — ��������� XPath Location Path � ������ ������.
��������
array xpath_eval (object xpath context)
��������������! | |||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
��������� | ������� |
---|---|
all | ����������, ��� ������ �� �������� ��������� ������ ������������� ���� �� ���, �� � ����� ������������������. |
choice | ����������, ��� ����� ������������� ����� �� ����������� |
sequence | ����������, ��� �������� �������� ������ ���������� � ������������ ������� |
@maxOccurs | ���������� ������������ ���������� ������� ������������ �������� |
@minOccurs | ���������� ����������� ���������� ������� ������������ �������� |
group | ������������ ��� ����������� ��������������� ������� ��������� |
attributeGroup | ������������ ��� ����������� ��������������� ������� ��������� |
������ ������ �������� ������� ��������� ������������ XML ����� � ������ ����������� ����� �������� ��� ����������� � ����������. �������������� ���������� �� ������ ����� �� ��������, ����������� W3C XML Schema.
������ ��������� XPath � JDK 1.5
������ �������, ����������� � �������� XML � JDK 1.5, �������� java.xml.xpath . ������ ����� ������������� API ��� ������ ���������, ��������� �� ������ XML Path Language (XPath) ������ 1.0. XPath ��������� �������� ���� ������ ��������� ������ ���������� (DOM) XML. � XPath ����� ���������� ������� �������������� ����� � ���������� �� ���������� boolean, double ��� string. � ������������ Javadocs ���������� ��������� ����������: �XPath ������ � 1999 ���� � �������� ���������� ������ XSLT � Xpointer, �� ������ ���� ���������� ��� ��������������� ����, ��� ��� ���� ��������� XPath �������� �������� ������� ���������� ����� ���� DOM API.
�������� XPath
������� ���������� ��������� XPath � ������� �� ����������. ���� �������� ������ �������� ��������� XPath:
������ ��������� �������� ��� ����. ��� �������� ��� �������� author , ���������� ��������� ��� �������� book . ������� book �������� �������� ��� �������� ��������� ����. ��������, ���� ������� ���������� ���� �������� ������� library, �� ���������� ��������� XPath � book/author ������� ��� �������� author �� ������������ ����� ����:
�������� ����� ����� ���� ����� ���� ����� XML DOM, � ��� ����� � �������� ����.
��������, ��� ������� author ������ ���� ������ �������� �������� book . ���������� ����������� �������� // , ���������� ���� ����� ������� � XML ��������� ��� �������� ��������� ����. ��������, ��������� ��� ��������� ������� ��� �������� author ��� ��������� ����.
� ������� 2 ������������ ��������� �������� ��������� XPath.
���� | �������� |
---|---|
../author | �������� ��� �������� author , ���������� ��������� ��� ��������� ����. |
* | �������� ��� �������� �������� ��������� ���� |
*/author | �������� ��� �������� �������� author ��� �������� ��������� ���� |
/book/author | �������� ��� �������� author ���������� ��������� ��� �������� book , ������� � ���� ������� �������� ��������� ��� ��������� ���� ���������. |
./book/author | �������� ��� �������� author ���������� ��������� ��� �������� book , ������� � ���� ������� �������� ��������� ��� ��� �������� ��������� ����. |
����� ���������, ���� XPath ����� �������� � ����������, �������, ������������� � ������� ������ DOM ������. � ������� 3 ��������� ��������� ��������.
���� | �������� |
---|---|
author/@name | �������� ������� name �������� author |
author/node() | �������� ����� ��� ���� (�����, �����������, ��� ���������� ���������). |
author/text() | �������� ��������� ���� �������� author |
author/comment() | �������� ��� ����������� � �������� author . |
author/processing-instruction() | �������� ��� ���������� ��������� � �������� author . |
��������� XPath ��������� ������ ���������� �����, ��������� ��� ������ ����� XPath. ��������� ������������ � ���� � [expression] . ��������� ������ �������� ��� �������� foo , ���������� ������� include , ������� �������� true .
��������� ����� ��������� ���� �� ������ ��� ����� ������� ������:
������������� XPath API
��������� ������ ������������ ���������� XPath API ��� ������ ���� �� ������ ���� �� XML ���������.
��������, ��� XPath API ��������� ���������������, ��������� ���� � �������, ��������� ����� Boolean , Number � String . ������������ ��� ������������ ���������� QName � ������ ������ ������ ��������� XPathExpression.evaluate() , ��� �������� � ������� ���� (������ ��������). ����������� �������� QName ���������� � ���� �������� � ������ XPathConstants:
- XPathConstants.NODESET
- XPathConstants.NODE
- XPathConstants.STRING
- XPathConstants.BOOLEAN
- XPathConstants.NUMBER
� ������ ������ ���� Boolean , ������������ �������� Boolean.TRUE , ���� ������ ���� ��� ����� �����. � ������ ������ ������������ �������� Boolean.FALSE . ��� String ������ ��� ��������� ������ �� ��������� �����, ����� ���������, ����� ������������ ��� ����� ���������� ���������. �������, ��� ������ ���� Number , ������������ ������� �������������� ���� ������ � ��� double .
��� XML ���������, ��������������� � ������ �� ������ ��������� ��������� ��� ��� ������ �������� year � ���� ����, ������ � �����:
DocumentBuilder builder =
DocumentBuilderFactory.newInstance () .newDocumentBuilder () ;
Document document = builder.parse ( new File ( «myXMLDocument.xml» )) ;
XPath xpath = XPathFactory.newInstance () .newXPath () ;
String expression = «/birthdate/year» ;
Node birthdateNode = ( Node ) xpath.evaluate ( expression, document, XPathConstants.NODE ) ;
System.out.println ( «Node is: » + birthdateNode ) ;
String birthdateString = ( String ) xpath.evaluate ( expression, document, XPathConstants.STRING ) ;
System.out.println ( «String is: » + birthdateString ) ;
Double birthdateDouble = ( Double ) xpath.evaluate ( expression, document, XPathConstants.NUMBER ) ;
System.out.println ( «Double is: » + birthdateDouble ) ;
> catch ( ParserConfigurationException e ) <
System.err.println ( «ParserConfigurationException caught. » ) ;
e.printStackTrace () ;
> catch ( XPathExpressionException e ) <
System.err.println ( «XPathExpressionException caught. » ) ;
e.printStackTrace () ;
> catch ( SAXException e ) <
System.err.println ( «SAXException caught. » ) ;
e.printStackTrace () ;
> catch ( IOException e ) <
System.err.println ( «IOException caught. » ) ;
e.printStackTrace () ;
>
����������� ���������� ������� ������� �����:
XML � XPath
XPath � ��� ���� ��� ������ ���������� ������ XML ���������.
��� ����� XPath?
- XPath — ����������� ���� ��� ����������� ������ XML ���������
- XPath ���������� ���������� ��������� ��� ��������� �� XML ����������
- XPath �������� ���������� ����������� �������
- XPath � ������� ������� � XSLT
- XPath ����� ������������ � XQuery, XPointer � XLink
���������� ��������� XPath
XPath ���������� ���������� ��������� ��� ������ ����� ��� ������� ������� � XML ���������. ��� ���������� ��������� ������ �� �� ���������, ������� ����� ������� ��� ������ � ������������� ��������� ���������.
� ��������� ����� ��������� XPath ����� ������������ � JavaScript, Java, XML ������, PHP, Python, C � C++, � ����� �� ��������� ������ ������ ����������������.
XPath ������������ � XSLT
XPath �������� ������� ������������ ��������� ��������� XSLT. ��� ������ XPath ���������� ��������� ��������� XSLT.
������ XPath
��� ������������ XPath ����� ������������ ��������� XML ��������:
� ����������� ���� ������� ������������ ��������� ��������� XPath � ��������� �� ������:
Java XPath API
������� � XML-���������� �� Java-��������
���� ���������� ������� ����-�� ������ ���� ������, ��� ����� ������� ����� ��������? «����������, ����� ���� ���� ������» ��� «����� �� ���� ����� �������� �����. ������� ������. ������ ��� ��������. ������� �������. ������ �������� ��������. ������� ������� � ����� � �������. ������� � ���������� ��������. ������ ���� ������ ����� ��������. ������� ������. ������ �������� ����� ������. ������� ��� � �����. ������� �� ������. ����� ����������� �����». ��� ������! ������� ����� �������� ����� ���������� ����, ����� �������������� ������ ������, ������������� ���� ����������� ���� «����������, ����� ���� ���� ������».
����� �������� � ������������ ����� ����� ������. ����� ������� «����� ����� ��������������», ��� ������� ��������� ������ ������ � ��������� ���� ������. ��� ��� �������� ������ ����� ���������� ������, ����� ����������� ���������� ����, ������� �������� �������� ��������� ���� «����� ��� �����, ���������� ����� �����������», � ����� ������� �������� ��������� ����� �������� ��� ���������� ��� ������.
XPath
����� ���� ������ �������� Structured Query Language (SQL) � ��� ����, ������������� � ���������������� ��� ������ � ������������ ����� ����������� ��� ������. ������ ������, ����� ��������� ������ �������� �������� � ���� Object Query Language (OQL) � XQuery. ������ ��������� ������������ ���� ������ �������� XPath, ���� ��������, ������������� ��� ������ ���������� � XML-����������. ��������, ������� ������ �� ����� Xpath ��� ������ ��������� �������� ���� ����, ������� ������� �������� ��� ��������� (Neal Stephenson), �������� �������� ���:
��� ���������, ��� ��� ���������� ������������ �������� � �������������� ������� DOM ������ �������� ��� � �������� 1:
������� 1. ���, ������������ DOM, ��� ������ �������� ����, ���������� ����� �����������
������ ������ � ������ ���, �� DOM-��� � �������� 1 ���-���� �� ��� ����� � ������, ��� ������� ��������� �� ����� XPath. ����� ��� ����� � ������� ��������, �������� � ������������? ��-�����, ����� ��������!
������ ��� �� �� �� ��� �����������, XPath � ��� ���-���� �� ���� Java, ����������, XPath ���� �� �������� ������ ����������������. ���������� ����� ����� �����, ������� ���������� ��������� � ������� XPath, ������� ���� �������, ������� ������ ������������. ��������, XPath �� ����� ����� ��� �����, ��� ������� ������ ������������ International Standard Book Number (ISBN) ��� ����� ���� �������, ��� ������� ��������� ���� ������ ����������, ��� ��������� �������� ����������� � ������� ������. � �������, ����� ������������� XPath � ���������, ���������� �� Java, � ������, �������� ��� ������ �� ���� �����: Java � ��� ���� ��� �����, � �������� ������ ����������� Java, � XPath ��� ����� ����, � ��� ������ ����������� Xpath.
�� ��� ��� ����� ����������� ���������������� ���������� (API), � ������� �������� Java-��������� ��������� XPath-�������, ������� �� ���� ���������� XPath. Xalan ���� ���� ����� API, Saxon � ������, � ��������� ���������� ����� ��������� ����������� ������ API. ��� �������, ��� ��� ���������� �������� � ������ ��������. � ����� ������ �������� �� ��� ������ ���������� � ������������� ���� �������� ������������ � ���������� ������������ �� ����������� ������������� �������������.
�� ���� ������� � Java 5 ������� ����� javax.xml.xpath � �� ������������� ���������� � ��������-����������� ������ ���������� XPath. ���� ����� ����� �������� � Java ������ 1.3 � ����, ���� ������������� ���������� Java API for XML Processing (JAXP) 1.3. �� ����� ��������� ��������� Xalan 2.7 � Saxon 8 ����� �������� � ���� ���������� ���� ����������.
������� ������
������ ����� ������������������ ��� ��� �������� �� ��������. ����� ����� ��������� ����������� ��������� ������. ��������������, ��� ��� �������� ���� ������ ����, � ������� ����� ���� ������� �����, ���������� ����� �����������. ���������� �����, ��� ������ ���� ����� ����� �� ���, ��� � �������� 2:
������� 2. XML-��������, ���������� ���������� � ������
����������� �������
The XPathFactory � ��� ����������� �������. ������ �������������� ������������ ������� ��������� ������� API ��� ��������� ���������� ��������� ������, ����� ��� DOM, JDOM � XOM. ����� ������� ����� ������, ���������� �������� Uniform Resource Identifier (URI), ���������������� ��������� ������, ������ XPathFactory.newInstance() . ��������, http://xom.nu/ �������� ��������� ������ XOM. ������ �� ��������, DOM � ��� ������������ ��������� ������, �������������� API �� ������ ������.
������ XPath, ������� �������� ��� ����������� ����� �� ���������, ���������� �����: //book[author=»Neal Stephenson»] . ����� ����� ������ �������� ���� ����, ��������� ����� ���� ��������� ��������� ������� � ��������� ����� ��������� ��������� �������: //book[author=»Neal Stephenson»]/title . �������, ������ ��, ��� ������������� ���������� � ��� ����� ����-������� �������� title . ��� ����� ��������� ��� ���� ��������� ���������, ������ ��������� ����� ���������: //book[author=»Neal Stephenson»]/title/text() .
������ ���������� ���������, ������� ���������� ���� ��������� ������ � ��������� �� ����� Java � ������� ����� �������� ���� ��������� ����. ������ ����� ���������� ��������� �������� � ������ Document . ��� ��������, ����� ��������, ��� ������ ���� ���������� � ����� books.xml � ������� ��������. ���� ��� ������� �������� ����, ������� ������������ �������� � ������� ��������������� ������ Document :
������� 3. ��������� ��������� � ������� JAXP
����, ��� ����� ���� ����������� JAXP � DOM, ������ ������.
������ ������� XPathFactory :
����� ������������ ��� ������� ��� �������� ������� XPath :
XPath ����������� XPath-���������:
����������� ����������: ���� ���������� ������������ ��������� XPath ������ ���� ���, �� ����� ���������� ��� ���������� � ������ ����� ������� ��������������� ����� evaluate() � ������� XPath . ������ ���� ���������� ������������ ���� � �� �� ��������� ��������� ���, �� ���������� ������� ��������� ������� ����������� �������.
�������, ���������� ��������� ������ XPath ��� ��������� ����������. ��������� ����������� �� ��������� � ������������� �� ��������� ����, ������� � ������ ������ �������� ���� ��������. ����� ���������� ���� ������ ��� ������������� ����������. � ������� ��� NODE �������� ��� ��� ���������� ���������� ���������:
������ ����� ��������� ��������� � DOM NodeList , ����� ���������� ���������� � ������� ��������, ����� ����� ��� ��������:
� �������� 4 ���� ���� ��� ������ � ���� ���������. ���������� ����� ��������, ��� ������������� ��������� ������� ����� ������� ��������� ����������, ������� ����� �������� � ������ throws , ���������� ���������� ����, �������������� ����:
������� 4. ��������� ��� ������ � XML-��������� � ������� �������������� XPath ���������
������ ������ XPath
��� ���������� ���������� �� ���� ��������� ������ ����������������, ����� XPath � Java, ����� ������� ��������� �������� «����» ���, ��� ����������� ��� ����� ����������������. �� ��� ����������� ���������� ������. XPath � Java ����� ��������� ������� ����� ������. � ��������� XPath 1.0 ����� ������ ������ �������� ���� ������:
Java, ����������, ����� ����������� ������� ���������� ��������� ����� ������, ������� ��������� ���� ������, ������������ �������������.
������ ��������� XPath, �������� ������������� ��������� �������� XML-���������, ���������� ��������� ���� node-set. ������ ���� ����� ������ �����������. �������� ��������� �� ����� XPath count(//book) ���������� ���������� ���� � ���������. ��������� ������ ������� ��������� �� ����� XPath count(//book[@author=»Neal Stephenson»]) > 10 ����� ��� ���������� boolean: ������������ �������� ����� true, ���� ������ ������ ���� ���� ���������� ���������� � ���������, � false, ���� ������ � �����.
����� evaluate() ���������� ��������� ���� Object . . ��� ������ ����� ����������� ������� �� ���������� XPath-���������, ����� ��� � ��� ������������� ������. � ����� ������, XPath
- number c������������ � java.lang.Double
- string c������������ � java.lang.String
- boolean c������������ � java.lang.Boolean
- node-set c������������ � org.w3c.dom.NodeList
XPath 2
�� ��� ��� ����������, ��� ������������ XPath 1.0. XPath 2 ����������� ��������� � �������������� ������� �����. �������� ��������� ��������� � Java XPath API ��� ��������� XPath 2 � ��� �������������� ��������� ��� ����� ����� ������������ ��������, ��������� � XPath 2.
����� ����������� XPath ��������� � Java, ������ �������� ���������� ��� ������������� ���������. ���������� ���� ��������� ����� ������������ ����������, ��� ��� ����� ���������� ��� ��������� � ������ javax.xml.xpath.XPathConstants �� ���������� �������:
- XPathConstants.NODESET
- XPathConstants.BOOLEAN
- XPathConstants.NUMBER
- XPathConstants.STRING
- XPathConstants.NODE
���������� ��������, ��� �������� XPathConstants.NODE �� �������� � ���������������� ����� ������ XPath. ��� �������� ������������ ������ �����, ����� ��� �����������, ��� ��������� �� XPath ������ ��������, ��������� �� ������������� ���� ��� ����� �� ��������� ����� ������ ��������. ���� ��������� �� XPath ���������� ����� ������ ���� � ��� ��������� ��� ������������� ��������� ��� XPathConstants.NODE , �� evaluate() ������ ������ ��������� � ��������� ����, ��������������� ��������� ���������. ���� ��������� �� ����� XPath ������ ���������� ������ ������ � ��� ��������� ��� ������������� ��������� XPathConstants.NODE , ����� ����� evaluate() ������ null.
���� ������������� �������������� ���� �� ����� ���� �������, �� ����� evaluate() ���������� �������������� �������� ���� XPathException .
�������� ������������ ����
���� �������� XML-��������� ���������� ������������ ����, �� ��������� XPath ��� ������� ������ �� ����� ��������� ������ ������������ �� �� ������������ ����. XPath-��������� �� ��������� ����� �� �� ��������, ����� ������ ������������ URI ���� �� ������������ ����. ����� XML-�������� ���������� ������������ ���� �� ���������, ��������� XPath ������ ������������ �������, ���� ���� ������� �������� ��� �� ����������.
������ Java-��������� � ��� �� XML-���������, ������� ������� ���������� ������������ ���� �� �����������. ������ ����� ���������� ������������ ������, ������� ���������� �������� � ������������ ���� URI. ���� ������ � ��������� ���������� javax.xml.namespace.NamespaceContext . ��������, �����������, ��� �������� �������� ���������� � ������ � ������������ ���� http://www.example.com/books, ��� � �������� 5:
������� 5. XML-�������� ���������� ������������ ���� �� ���������
��������� XPath, ������� ������� �������� ���� ���� ���� ���������� ������ ���������� ��� �����: //pre:book[pre:author=»Neal Stephenson»]/pre:title/text() . ������ ���������� ���������� ������� pre to the URI http://www.example.com/books. � URI http://www.example.com/books. ������� �������, ��� ��������� NamespaceContext �� ����� ���������� �� ��������� � ��������� ������������ Java (JDK) ��� JAXP, �� ��� ���. ��� �� �����, �� ��� �� ������ ������� ����� ���������� ��������������. � �������� 6 �������� ������� ���������� ��� ������ ������������ ����. ���������� ����� ���������� ������� xml .
������� 6. ������� �������� ��� ���������� ������ ������������ ���� � ������������� ����, ������������ �� ���������
�� ��� �� ������ ������������ ����������� ��� ������������ ���������� ���� � ���������, � ����� ��� ���������� �������, ������� ��������� ������� �������� ������������ ���� ����������� ������������.
����� �������� ������� NamespaceContext ���������� ���������� ��� � ������ XPath ����� ����������� ���������. ������ ����� ������������ � �������� �� �� ��������, ��� � ������. ��������:
������� 7. ������ XPath, ������� ���������� ������������ ����
�������������� �������
� ��������� ������� ������� ���������� ���������� ������� �� ����� Java ��� ������������� ������ � ����������� �� XPath. ��� ������� ����� ��������� ������, ������� ������ ��� ���� ���������� ��� ������������� ������� XPath. ������ ��� ������ ���� ���������� ���������, � �� ������ ������������� ��������. �� ���� ��� �� ������ ����� �������� ��������. (������� XPath ����� ���� ��������� � ����� ������� � ������������ ����� ���).
������� ����������, ����������� ����� Java XPath API ������ ������������� ��������� javax.xml.xpath.XPathFunction . ���� ��������� ���������� ������������ ����� evaluate():
���� ����� ������ ���������� �������� ������ �� ���� ����� � ����� Java, ������� ����� ���� �������������� � ���� ������ XPath:
� �������� 8 �������� ������� ����������, ������� ��������� ����������� ����� ���� ISBN � ���������� �������� ���� Boolean . �������� ������� ��� �������� ����������� ����� ����������� � ���, ��� ������ ������ ���� ���������� �� ���� ������� (�� ���� ������ ����� ���������� �� �������, ������ � �� ��� � ��� �����). ���������� �������� ������������ � ������������� ������� �� ������� �� �����������. ���� ������� ����� ������, �� ��������� ����� ����� X.
������� 8. ������� ���������� XPath ��� �������� ���� ISBN
��������� ��� � ��� ������� ������� ���������� ��������� ��� Java-���������. ��� ����� ���������� ���������� javax.xml.xpath.XPathFunctionResolver � ������ XPath ����� ����������� ���������. ��������������� ������� ���������� XPath-��� � URI ������������ ���� ��� ������� � Java-����� � ��������� �������. � �������� 9 �������� ������ �������� ��������������� �������, ������� ���������� ������� ���������� valid-isbn � ������������� ���� http://www.example.org/books � ����� �� �������� 8. ��������, ��������� �� XPath //book[not(pre:valid-isbn(isbn))] ������� ��� �����, ��� ���� ISBN �� �������� �������� �� ����������� �����.
������� 9. �������� �������-���������� val >
��� ��� �������-���������� ������ ���������� � ������������ ����, ���������� ������������ ������ NamespaceResolver ��� ���������� ���������, ����������� ������� ����������, ���� ���� ��������, � ������� ���������� ����� �� ���������� ������������ ����. ��� ��� XPathFunctionResolver , XPathFunction � NamespaceResolver �������� ������������, �� ����� ������������ �� � ����� ������, ���� ��� ������.
����������
����������� ����� ������ ������� �� ������������� ������ ���������������� ���� SQL � XPath, ��� �� ������������ ������, ����� ��� Java � C. ����������� ����� ������������� ������� ������ �� ������ �� �������� ������, ����� ��� Java � C, ��� �� ������������� ������ ��������� SQL � XPath. � �������, ����� ���������� ������������ � ������������� �����, ��������� ����� ����� API, ��� Java Database Connectivity (JDBC) � javax.xml.xpath . ��� ������ � ������ ������ � ���� ����������� � ������ XML, ������� javax.xml.xpath ���������� ����� �� ������ �����������, ����� ��� ���� java.sql .
������� ��� ����������
������� ����
- �������� ������: The Java XPath API (EN).
- «Get started with XPath 2.0» (EN): ��������� ��������� ���� � ������������� XPath 2.0, ��������� ��������� ������ ����� ������� ������� � ������� ����� ������ ������.
- «Working with JAXP namespace contexts»(EN): ���� ���� ������������ ������������� ������������ ����.
- XML in a Nutshell (Elliotte Rusty Harold and W. Scott Means, O’Reilly, 2005): �������� ��� ������ ����������� �� XML, ���������� �������� XPath 1.0, DOM � JAXP.
- ������ ���������� Java ����� developerWorks ������: �������� ��� ���� ����� ������, ���������� ���������� ��� ���� �������� ���������������� �� Java.
- ������������ IBM XML 1.1: ������� ��� ����� ����������������� ������������� IBM �� XML 1.1 � ������������� ����������� (EN).
- ������ JAXP: �������� JAXP 1.3 ��� Java 1.3 � 1.4 � java.net (EN).
- Xalan 2: ������� � ������ XSLT, �������������� XPath API (� ������� �������������� � ���� ������) � ��������������� �������� Apache Project (EN).
- SAXON 8: ���������� ������ XSLT, ���������� �������� �������� Michael Kay. ���� ������ ����� ������������ XPath API, ��������� � ���� ������ (EN).
- IBM trial software: �������� ���� ��������� ������ � ������� ������������ �����������, ������� ����� ��������� ��������������� � ����� developerWorks (EN).
�����������
������� ��� ����������������� ��� ���� ����� ��������� ����������� ��� ����������� �� ���.
���������� ��������� XPath � ������� ������ XPathNavigator Evaluate XPath Expressions using XPathNavigator
����� XPathNavigator �������� ����� Evaluate ��� ���������� ��������� Xpath. The XPathNavigator class provides the Evaluate method to evaluate an XPath expression. ����� Evaluate ��������� ��������� XPath, ��������� ��� � ���������� ��������������� ��������� W3C ��� XPath Boolean, Number, String ��� Node Set, ���������� �� ���������� ���������� ��������� XPath. The Evaluate method takes an XPath expression, evaluates it and returns a W3C XPath type of Boolean, Number, String, or Node Set based on the result of the XPath expression.
����� Evaluate The Evaluate Method
����� Evaluate ��������� ��������� XPath, ��������� ��� � ���������� �������������� ��������� Boolean (Boolean), Number (Double), String (String) ��� Node Set (XPathNodeIterator). The Evaluate method takes an XPath expression, evaluates it, and returns a typed result of Boolean (Boolean), Number (Double), String (String), or Node Set (XPathNodeIterator). � �������, ����� Evaluate ����� ������������ � �������������� ������. For example, the Evaluate method could be used in a mathematical method. � ��������� ������� ���� �������������� ���������� ��������� ���� ���� � ����� books.xml . The following example code calculates the total price of all the books in the books.xml file.
� ������� � �������� ������� ������ ������������ ���� books.xml . The example takes the books.xml file as input.
������� position � last position and last Functions
����� Evaluate ����������. The Evaluate method is overloaded. ���� �� ������� Evaluate ��������� ������ XPathNodeIterator � �������� ���������. One of the Evaluate methods takes an XPathNodeIterator object as a parameter. ������ ���������� ����� Evaluate ��������� ������ Evaluate, ������� ��������� � �������� ��������� ������ ������ XPathExpression; ������������ ������� ������� � ���, ��� ������ ����� ��������� ���������� � �������� ��������� ������ ����� ��� ����������� �������� ���������, � ������� ����� �������������� ����������. This particular Evaluate method is identical to the Evaluate method that takes only an XPathExpression object as a parameter, except that it allows a node set argument to specify the current context to perform the evaluation on. ���� �������� ��������� ��� ������� position() � last() , ��������� ��� �������� ������������ �������� ���� ���������. This context is required for the XPath position() and last() functions as they are relative to the current context node. ���� ������� position() � last() �� ������������ � �������� ���������� � ���� �������, ��� ������� ������� ������ �� ����� ����� ��� ���������� ����������; ����� ������� position � last ���������� �������� 0 . Unless used as a predicate in a location step, the position() and last() functions require a reference to a node set in order to be evaluated otherwise, the position and last functions return 0 .
xpath_eval
xpath_eval — ��������� XPath Location Path � ������ ������.
��������
array xpath_eval (object xpath context)
��������������! |
---|
����������� +����� ������� 28 2012 18:40:42 41766 ��������� |
|
����� XPath-��������������� ��� ������ PHP-���, ��������� ��������� ����� �� �������� �����, ����� � ������� XPath. ���� products.xml � �������� 1 �������� ������� ���������� ����� ������������ ��������� ��� ������ ������ � XML, ���������� �� ����������, ��� ������ ��������� �������� �����, � ����� ������� ������������� �������. ������� 1. ������ XML ��������� � �������� (products.xml)/PRODUCTS ���������� ��� ����, ������� �������� ��������� ���� PRODUCTS , � ������ ������ �� ������. �������� �������� �� ����� ����� ( / ). ���� �� ������� � UNIX-���������, �� ������, ��� ����� ����� �������� ���������� ����. ��� � � �������� ����� � UNIX, �� ������ ������������ ���������� ���� � ������ �������� �� ���� �������� �������������� ���������. ��� ��������� � �������� 1, PRODUCTS �������� �������� �����. ������������� ���� � XPath ����� ��������. ������������� .. � ��������� ��������� ���������� ������� �� ���� ������� ���� �������� ���� � �������� (����� �� ���������� ������ � ���������� � UNIX-��������). ��������, ../PRODUCT/SKU ���������� ��������� �������� ���� ��� ���� ������� SKU-�������, ��������� � ������ ����� PRODUCT . ����� ���� � ���������� ������� ������������ ���������� � XPath. ���� �� ������ �������� ��� ���� PRODUCT , ������������ � ��������� software, ����������� ��������� /PRODUCTS/PRODUCT[@category=’software’] . � ���� ��������� category ��������� ��������� � XML-���������. XPath ����� �������� �������� � �������� (@). ����� ����, �� ������ ������� ������� � ������� ���������� attribute:: — ��� ���� ��������� ������ ��� /PRODUCTS/PRODUCT[attribute::category=’software’] . ������ �������, ��� ������ @ ���������� � ����� � �������������. ��� ������ ��������� ������� �������� (�� ���� ����������� ��������� ��������) ��� ���� �������� ��������� ���������� ����� $ 2500 ����� �������� ��������� /PRODUCTS/PRODUCT[PRICE > 2500]/NAME . ��� ���������� ��� ��������� ���������� �������� Widget Reporting � Widget100 Series . ������ ��� ���������� � ������������� XPath � PHP, ��������� �� ��������� ������ ���������: /PRODUCTS/PRODUCT/NAME[string-length( ) > 15]/../@category . ��� ���������� ��� ��������� ���������� ��� ��������: software � storage . � ���������, ��� ��������� ������������� �������� ��������� ��� ������� ��������, ��� �������� ������� 15 ��������. XML-�������� � �������� 1 ������ �������� ���������� ��� ���� ��������� XPath, ������� ��������������� � ���� ������. ��������� ��������� XPath � PHP��� ����, ��� PHP ������������ XML � XPath, �� ������ �������� ���������. �� ����� ����, ���� ����� ���������� Web-���������� ���� ������������� � ����� ������� ����������� ��������� ������� ������� ��� ������ � XPath. ������� ���������� PHP ������������� ����� ��� ��������� ��� ������ � XML: ���������� SimpleXML ������ � ������������� � �������� ��� ������������ ������� �������� ����� ��������� XML. � ��� ���� �����������, ��������, ��� �� ��������� ������������ ��������, ������ � ������������ ����. ��� ��������� ������� XML-�������� ������ ������ � ����, ��� SimpleXML ��������� ��������� XML-������ ��������� � ������ ����� ����������. ���� ��� ����� ������������ ����� ������� ��������� XPath � ��������� ������ �������� ��� ����������, ������� ������� �������� DOM. DOM � ��� ���������� �� Document Object Model (��������� ������ ��������� — �������� W3C). �� ������ ������������ DOM � PHP, ��������� ��� � �������� ����������. ����������, �������� DOM, ��� �������, ������ � ��������� � �������������. �������� ���������� ����������������� ���� ������ � ����� php.ini, ����� ������������ ��� ���������������� ������. ��� � SimpleXML, DOM ��������� ������ XML-������ ��������� � ������ ����� ����������. ��� �� ������� ����� � ������, DOMXPath ����� �������� ������� � �������������. �� ������ ����� ��������� � ���������� XML_XPath �� ��������� PEAR (PHP Extension and Application Repository). ���� ����� ���������� DOM � ������������ ������� � �������������� XPath ��� ��������������� ����������� � ���������� ��������� �������� �����. ���� �� ��������� � Zend Framework, � ����� ������������ ���������� Zend_Dom_Query . ���� ���� ���������� ����� PHP �� ��������������� ����������� ������� � ������� ��� XML � XPath, �� ������ ������ ������������ ��, ��� PHP ��� �������������. XMLWriter / Reader �� ������������ XPath ��� ������ SimpleXML ��� DOMXPath, ������� � ���� ������ �� �� ���������������. ���������� �� ����, ����� ���������� ��� ����� �� �����������, ��� ����������� ������ ����� ����� ��������, ��� ��������� XPath-���������. ��� ������������� XPath � ��������� � PHP-������������, ���������� � XML, ��������� XPath �� ��������. � ����������� ����� �������� � ������������� ������������ ��������� DOM � SimpleXML. ������ � XPath � PHP� ������� ����� products.xml � ��������� �� �������� 1 � PHP5 SimpleXML API �� ������ �������������������� � ���������� ����������� XPath. ��� ���������� ���� �� �������� 2 ��������� ������ XML-���� � �������, � ���� �������. XPath-��������� /PRODUCTS ����������� ��� ������ ���������� ����, ������� �������� �������� ��������� ���� ( PRODUCTS ). ������� 2. ������������� SimpleXML ��� ����������� ���� ����� � ���������� ���������� ���� �� �������� 3 ����������� ����� ������ �������� ������� ���� NAME XML-������. �������� ��������, ��� ��������� /PRODUCTS/PRODUCT/NAME ������� ������ ���������� ���� � XML-������, � �� ������ ������ ��� ���������. ������� 3. ������������� SimpleXML ��� ����������� ���� �������� ��������� � ����������� ��� ����� �������� ����������� ���� ��� ����� �� ������ ��������� ���������, �������� �������� �� ��������� 4 � 5. ����� ��������� �������� ���� �������� ���������� ������ �������� �� XML-���������. ��� ���������� ���� �� �������� 4 ����������� ����� ��������� �������� ������ ����. ������� 4. ������������� SimpleXML ��� ����������� �������� �������� ��� ����������� SKUXPath-��������� /PRODUCTS/PRODUCT[SKU=’soft5678′]/NAME ���������� ��� ����, ������� ������������� ���������. � ���� ������ ������ ���� ������� ����� ����� SKU, ����������� � ��������. ���� ��� ����� ����� �������� ���� � ������������ �� ����� �������� � ������ XML, �� ������ ������������ ������� position() . �� ������ ������������ �������� ��������� � XPath ��� ����������� ����������� ��������������� ���������� �����. � �������� 5 ������� ������ ������������� SimpleXML � XPath � �������� ����������. ������� 5. ������������� SimpleXML ��� ���������� ���������, ��������������� ���������, ��������, ��������, ��� � ��������� 2, 3, 4 � 5 ���������� ���������� PHP ��� � ������������ �������� ����������� � XPath ���������. ����� �� ������� SimpleXML, ��� ����� �������� ��� ���� ����� XPath. ����, ����������� � ���� PHP ��� ������������� SimpleXML, ����� ������������ ��������� �������:
����� ���������� � ������ �������� ���������� � ������� ��������� print_r($products); . ��� ������ �������� ��� ����������� � ���� �������. � ���������� �� ������ ����� ������ ������������ ��������� � �����-�� ���������, ����������� � ������� PHP. � �������� 6 ������������ DOM � DOMXPath ��� ������ � XML � XPath. ������� 6. ������������� DOMXPath ��� ����������� �������� �������� ��� ����������� SKU������������������ PHP-������, ������� ������������ ��� ������ � DOM � DOMXPath, ��������� �������, ��� ��� ������ � SimpleXML. � �������� 6 ��� ���� ��������� � ��������� ������������������:
����� ��, ��� ������ �� ��������� ������ �������� PHP-���� ��� ������ � DOM, XPath �������� �� ��� ��� ������� ������. ������������� XPath ��� ��������������� ���������������� XML ����� ������������� � XHTML ��� ������������� XSLT. �������� ���� �����������������, �� ������ ������� ���������� ������� � ������� ����� XHTML ��� �����������, ��������� ������ SimpleXML ��� DOM. ������ ��������, ��� ����� ������ ����� �������. ������, ��������, ��� ����� �� ��������� ���������� XPath �������� �������������� � ������ XSLT, � ��, ��� PHP ������������ XSLT, ������������� ��� ������������������. ����� ����, � ������� XSLT ����� ���������� ����� ������� � ���. �������������� XSLT � PHP����� ��������������� �� PHP � XPath, ������ �������������� � ���� ������� �� ���������� CSS ��� ����������� URL-������. ��� �������������� ������ � ������� XSLT ������ � ����, ��� �� ������ �������� �����, JavaScript ��� ���-������ ���, ��� ���������� ����� �������� HTML ���������. ����� ����, ��� XSL-���� � ����������, ������ ����� ����� ��������� ����������� � ����� ���������, ������� ������������� ����� �����. ������� ��������� XPath ���������� ��� �������������� XML-������ � ������ �������, ����� ��� HTML � XSLT. RSS � Atom-������ �������� �� XML, ������� XPath �������� ��������� ������������ ��� ��������� ������ � ������ ������ ������. ���� ������������, ��� ��� ������ ����� ����������� ��� Atom ��� ��������� PHP- � XPath-�������, �� ������ ������������ ���� �� XML-��������� PHP ��� ���������� ������� �� ������ � ����������� �� �� ������ ������� �� ����� �����. ���� ����� ������� XSLT-����, ��� � �������� 7, � ������������ ������� ���������� �� XPath. ������� 7. ������� XSLT-����, ������� ����������� ��� � HTML (article_feed.xsl)��� ����� ����� ( // ) ��������� XPath, ��� ���������� ������������ ������ ����������� ����, ������� � ��������� ����. ��������� ������ �� ������ ���� �� ������ ���������, ������������, ������, �������� �����, ���������� ���� ������� ��� ������������� ����������� ����. ��������� ���� for-each �������� � ��������� ���� //feed/entry , ������������ ������������� ���� ��� �������� � ������. ��������� XSLT-���� �� �������� 7, �� ������ �������� ��������������� PHP-��� ��� ���������� ��������������, ��� �������� � �������� 8. ������� 8. ������������� DOM ��� �������������� XLST�������� ��������, ��� � �������� 8 ��� ��������� $xpath->query() , ��� �������� � �������� 6. ��� XPath-��������� ��������� � XSL-�����. ������ ����������� DOM, ����� ������������� ������� ������, � ����� ����� ��� ��������� ��������������! ����������� ���� ������ �� ������������� � XPath � �������� ��� ������������� � ����� PHP5 ��� ������ � XML. ��� � ������ ������ ����������, ��������� � PHP, XML-���������� �������� ��� ��� ������������ ��������������� �� �������������� �����������, � �� �� �������������� ����������� ������� � ��������. XPath ����� ������ ���������� � ����������� �������� ����������� � ������� ������ � XML. � ����������� �� ����� ������������ ����� ������������� ������������ SimpleXML, DOM, XML ��� ���������� ��������������� ����, ����� ��� Zend Framework. � �������, ��� ��� �������� � W3C XPath � ����������� �����. ������� ����� ��� � ��������� ��� ����������� ��������� XML-���� ��� ����� ������ � ����� �������� ��� ��������� � �� ���������! ������� ��� ����������������� ����
������������������ ��� ����������������� ��� ���� ����� ��������� ����������� ��� ����������� �� ���. XML � XPathXPath � ��� ���� ��� ������ ���������� ������ XML ���������. ��� ����� XPath?
���������� ��������� XPathXPath ���������� ���������� ��������� ��� ������ ����� ��� ������� ������� � XML ���������. ��� ���������� ��������� ������ �� �� ���������, ������� ����� ������� ��� ������ � ������������� ��������� ���������. � ��������� ����� ��������� XPath ����� ������������ � JavaScript, Java, XML ������, PHP, Python, C � C++, � ����� �� ��������� ������ ������ ����������������. XPath ������������ � XSLTXPath �������� ������� ������������ ��������� ��������� XSLT. ��� ������ XPath ���������� ��������� ��������� XSLT. ������ XPath��� ������������ XPath ����� ������������ ��������� XML ��������: � ����������� ���� ������� ������������ ��������� ��������� XPath � ��������� �� ������: ��� ����� ��� xpath_evalxpath_eval — Evaluates the XPath Location Path in the given string Описание array xpath_eval ( XPathContext xpath_context, string xpath_expression [, domnode contextnode] )
|