sharplispers / xpath Goto Github PK
View Code? Open in Web Editor NEWCommon Lisp Implementation of the XML Path Language (XPath) Version 1.0
Common Lisp Implementation of the XML Path Language (XPath) Version 1.0
In the document I'm trying to parse tags often have pre- and suffixes
that I need to read (e.g ... [<sometag>FOO_BAR</sometag>] ...
where
I need to get the "[" and "]" if they are present).
Reading suffixes by evaluating following-sibling::text()
works like
I would expect in that it gives me the text directly after a tag.
However, when I try to read the prefix of a tag by evaluating
preceding-sibling::text()
I get the text after the parent tag of the
tag instead of the text directly before the tag.
You can test the behaviour by running the following snippet:
(defparameter *document-text*
"<foo>BAR-PREFIX<bar>BARTEXT</bar>BAR-SUFFIX=BAZ-PREFIX<baz>BAZTEXT</baz>BAZ-SUFFIX</foo>")
(defparameter *xml*
(cxml:parse *document-text*
(cxml:make-whitespace-normalizer
(stp::make-builder))))
(format t "~a~%" *document-text*)
(xpath:do-node-set (node (xpath:evaluate "foo" *xml*))
(let* ((bar-suffix (xpath:string-value (xpath:evaluate "bar/following-sibling::text()" node)))
(baz-prefix (xpath:string-value (xpath:evaluate "baz/preceding-sibling::text()" node)))
(baz-suffix (xpath:string-value (xpath:evaluate "baz/following-sibling::text()" node))))
(format t "bar/following-sibling::text() - expected: \"BAR-SUFFIX=BAZ-PREFIX\", got: ~s~%" bar-suffix)
(format t "baz/preceding-sibling::text() - expected: \"BAR-SUFFIX=BAZ-PREFIX\", got: ~s~%" baz-prefix)
(format t "baz/following-sibling::text() - expected: \"BAZ-SUFFIX\" , got: ~s~%" baz-suffix)))
I tested it on SBCL 2.0.1.debian with Quicklisp installed on Ubuntu
20.04 using the following command ("test.lisp" contains the snippet
above):
sbcl \
--non-interactive \
--eval "(progn (ql:quickload :xpath) (ql:quickload :cxml) (ql:quickload :cxml-stp))" \
--load test.lisp \
--eval "(sb-ext:quit)"
I'm relatively new to CL and it's my first time working with XPath
syntax, so if this behaviour is intended I apologize and would be
thankful for a hint as to how I can properly read text surrounding tags.
when i run (ql:quickload :xpath)
i get the following:
Lock on package COMMON-LISP violated when
proclaiming nil as a function while in package XPATH.
[Condition of type symbol-package-locked-error]
See also:
SBCL Manual, Package Locks [:node]
Common Lisp Hyperspec, 11.1.2.1.2 [:section]
i'm pretty new to CL so not sure what's going on. is this library still functioning? it seems to be up to date in quicklisp/ultralisp.
If I call xpath:evaluate
with a particular node, I don't expect the node-set iterator to map through that node's parents. Currently, if I evaluate the XPath expression //c
using the first b
node as context, I get all four c
nodes back.
ICS->CSV> (let* ((xml (cxml:parse
"<a><b><c>0</c></b><b><c>1</c></b><b><c>2</c></b><b><c>3</c></b></a>"
(cxml-stp:make-builder)))
(el (first (xpath:map-node-set->list 'identity
(xpath:evaluate "//b" xml)))))
(xpath:map-node-set->list 'identity
(xpath:evaluate "//c" el)))
(#.(CXML-STP:ELEMENT
#| :PARENT of type ELEMENT |#
:CHILDREN '(#.(CXML-STP:TEXT #| :PARENT of type ELEMENT |# :DATA "0"))
:LOCAL-NAME "c")
#.(CXML-STP:ELEMENT
#| :PARENT of type ELEMENT |#
:CHILDREN '(#.(CXML-STP:TEXT #| :PARENT of type ELEMENT |# :DATA "1"))
:LOCAL-NAME "c")
#.(CXML-STP:ELEMENT
#| :PARENT of type ELEMENT |#
:CHILDREN '(#.(CXML-STP:TEXT #| :PARENT of type ELEMENT |# :DATA "2"))
:LOCAL-NAME "c")
#.(CXML-STP:ELEMENT
#| :PARENT of type ELEMENT |#
:CHILDREN '(#.(CXML-STP:TEXT #| :PARENT of type ELEMENT |# :DATA "3"))
:LOCAL-NAME "c"))
After loading xpath
, I can't run the preparation step out of the box, parsing the XML:
(defparameter *document*
(cxml:parse "<test a='1' b='2' xmlns:foo='http://foo'>
<child>hello world</child>
<foo:child>bar</foo:child>
</test>"
(stp:make-builder)))
=>
package STP does not exist.
It looks like cxml-stp
is required.
Or if it isn't, what is an alternative to (stp:make-builder)
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.