Xpath
XPath parser in NPL
| Title |
XPath parser in NPL |
| Author(s) |
WangTian? |
| Date |
2007/12/28 |
| File |
script/ide/Xpath.lua |
Description
XPath uses path expressions to select nodes in an XML document. The node is selected by
following an xpath. An xpath consists of xpath expression and predicate(optional).
Sample Code
NPL.load("(gl)script/ide/XPath.lua");
local xmlDocIP = ParaXML.LuaXML_ParseFile("script/apps/Poke/IP.xml");
local xpath = "/mcml:mcml/mcml:packageList/mcml:package/";
local xpath = "//mcml:IPList/mcml:IP[@text = 'Level2_2']";
local xpath = "//mcml:IPList/mcml:IP[@version = 5]";
local xpath = "//mcml:IPList/mcml:IP[@version < 6]";
local xpath = "//mcml:IPList/mcml:IP[@version > 4]";
local xpath = "//mcml:IPList/mcml:IP[@version >= 5]";
local xpath = "//mcml:IPList/mcml:IP[@version <= 5]";
local xmlDocIP = ParaXML.LuaXML_ParseFile("character/v3/Pet/MGBB/mgbb.xml");
local xpath = "/mesh/shader/@index";
local xpath = "/mesh/boundingbox/@minx";
local xpath = "/mesh/submesh/@filename";
local xpath = "/mesh/submesh";
--
-- select nodes to an array table
--
local result = commonlib.XPath.selectNodes(xmlDocIP, xpath);
--
-- iterate on all nodes.
--
for node in commonlib.XPath.eachNode(xmlDocIP, xpath) do
commonlib.echo(node[1]);
end
-- debug: print the result table
NPL.load("(gl)script/kids/3DMapSystem_Misc.lua");
Map3DSystem.Misc.SaveTableToFile(result, "TestTable/LODxml.ini");
Member Functions
XPath.TraverseXMLTable1
traverse through the xml table to get nodes according to xpath expression
/ expression : Selects from the ROOT node
syntax
function XPath.TraverseXMLTable1(o, xpath, result)
parameters
XPath.TraverseXMLTable2
traverse through the xml table to get nodes according to xpath expression
// expression : Selects nodes in the document from the current node that match the selection
ANYWHERE in the document
syntax
function XPath.TraverseXMLTable2(o, xpath, currentPath, result)
parameters
| o |
|
| xpath |
|
| currentPath |
|
| result |
|
XPath.FilterResultPredicate
extract the sPredicate string into compareable format
and filter the traverse result
syntax
function XPath.FilterResultPredicate(sPredicate, result)
parameters
XPath.FilterResultAttribute
filter the result according to sAttribute
syntax
function XPath.FilterResultAttribute(sAttribute, result)
parameters
XPath.selectNodes
Decs: XPath uses path expressions to select nodes in an XML document. The node is selected by
following an xpath. An xpath consists of xpath expression and predicate(optional).
-- Supported path expressions are listed below:
-- nodename: Selects all child nodes of the named node
/ : Selects from the ROOT node
// : Selects nodes in the document from the current node that match the selection ANYWHERE in the document
@ : Selects attributes
-- Predicates are used to find a specific node or a node that
contains a specific value. Predicates are always embedded in square brackets.
Supported operators are listed below:
< : number
> : number
<= : number
>= : number
= : number or string
-- operators:
| : | operator in an XPath expression you can select several paths. (not implemented yet)
-- e.g: xpath: /mcml:mcml/mcml:packageList/mcml:package/*[mcml:AuthorName = 'ParaEngine']
- e.g
- xpath: /mcml:mcml/mcml:app/mcml:IPList/mcml:IP[@something < 23]
- e.g
- xpath: /mcml:mcml/mcml:app/mcml:IPList/mcml:IP[@something]
syntax
function XPath.selectNodes(xmlLuaTable, xpath)
parameters
XPath.TraverseXMLTableNonRecursive1
traverse through the xml table to get nodes according to xpath expression
/ expression : Selects from the ROOT node
syntax
function XPath.TraverseXMLTableNonRecursive1(o, xpath, result)
parameters
XPath.eachNode
return an iterator of selected nodes in the XML document.
see function XPath.selectNodes(xmlLuaTable, xpath) for parameter specification
syntax
function XPath.eachNode(xmlLuaTable, xpath)
parameters
XPath.XMLDecodeString
Desc: Decode character references and standard entity references in the string (str).
- param str : the string to be decoded
- return __ : a new decoded string as the result.
syntax
function XPath.XMLDecodeString(str)
parameters
| str |
the string to be decoded |
XPath.XMLEncodeString
Desc: Encode the following characters found in (str): & ' < > " and replace them with the standard entity references.
- param str : the string to be encoded
- return __ : an encoded string.
syntax
function XPath.XMLEncodeString(str)
parameters
| str |
the string to be encoded |