Skip to content

Instantly share code, notes, and snippets.

@amobiz
Last active October 2, 2015 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amobiz/93e367f1b3b2f005739d to your computer and use it in GitHub Desktop.
Save amobiz/93e367f1b3b2f005739d to your computer and use it in GitHub Desktop.
import java.io.*
import org.jdom.*
import org.jdom.input.*
import org.jdom.xpath.*
import org.jdom.output.*
import org.xml.sax.*
def xhtml = """<html
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:myns="http://www.w3.org/1999/xhtml"
>
<head><title>Namespace Prefix Test</title></head>
<body>
<span>prefix:default</span>
<html:span>prefix:html</html:span>
<myns:span>prefix:myns</myns:span>
</body>
</html>"""
def xpaths = [
"" : "//span",
"html" : "//html:span",
"myns" : "//myns:span"
]
//def builder = new SAXBuilder( "org.ccil.cowan.tagsoup.Parser" )
def builder = new SAXBuilder()
def outputter = new XMLOutputter()
def is = new InputSource( new ByteArrayInputStream( xhtml.bytes ) )
def doc = builder.build( is )
println "input:\n" + xhtml
println "output:\n" + outputter.outputString( doc )
println "result:\n"
xpaths.each { prefix, path ->
println "namespace prefix: \"${prefix}\"; xpath: \"${path}\""
def xpath = XPath.newInstance( path )
//xpath.addNamespace( prefix, "http://www.w3.org/1999/xhtml" )
def result = xpath.selectNodes( doc )
result.each { println " " + outputter.outputString( it ) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment