MCPcopy Create free account
hub / github.com/ActiveState/code / DoXPath

Function DoXPath

recipes/Python/274905_Using__Apache_XPath_API/recipe-274905.py:25–46  ·  view source on GitHub ↗
(Filename,XPathString)

Source from the content-addressed store, hash-verified

23from javax.xml.transform.stream import StreamResult
24
25def DoXPath (Filename,XPathString):
26 IS = InputSource (FileInputStream (Filename))
27 df = DocumentBuilderFactory.newInstance()
28 df.setNamespaceAware(1)
29 doc = df.newDocumentBuilder().parse(IS)
30 serializer = TransformerFactory.newInstance().newTransformer()
31 serializer.setOutputProperty (OutputKeys.OMIT_XML_DECLARATION, "yes")
32 nl = XPathAPI.selectNodeIterator (doc,XPathString)
33 n = nl.nextNode()
34 while n:
35 if IsTextNode (n):
36 # Coalesce contiguous text nodes
37 res = [n.getNodeValue()]
38 nn = n.getNextSibling()
39 while (nn):
40 res.append (nn.getNodeValue())
41 nn = n.getNextSibling()
42 java.lang.System.out (string.join(res,""))
43 else:
44 serializer.transform (DOMSource(n), StreamResult (OutputStreamWriter (java.lang.System.out)))
45 java.lang.System.out.println()
46 n = nl.nextNode()
47
48def IsTextNode(n):
49 return (n.getNodeType() in [Node.TEXT_NODE,Node.CDATA_SECTION_NODE])

Callers 1

recipe-274905.pyFile · 0.85

Calls 7

IsTextNodeFunction · 0.85
parseMethod · 0.45
appendMethod · 0.45
outMethod · 0.45
joinMethod · 0.45
transformMethod · 0.45
printlnMethod · 0.45

Tested by

no test coverage detected