如何在命令行中使用带有Saxon-HE的XPath解析HTML?

Gil*_*not 1 java xml xpath xquery saxon

我使用saxon HE 9.6,它非常适合在解析格式良好的XML文件时使用XPath 3.

但我想知道如何将expath-http-client(或任何其他工作解决方案)与Saxon结合起来,以便能够解析realLife©®™(可能已损坏)的HTML.(Java不是我更好的技能).

我搜索谷歌很多小时没有任何工作解决方案.我尝试过类似的东西:

xquery_file.xsl:

xquery version "1.0";

declare namespace http="http://expath.org/ns/http-client";

let $url := 'http://stackoverflow.com'
let $response := http:send-request(
   <http:request href="{$url}" method="get"/>
) return
    <echo-results>
        {$response}
    </echo-results>
Run Code Online (Sandbox Code Playgroud)

Shell命令取自expath-http-client-saxon-0.10.0的README

saxon --repo /usr/share/java/expath/repo -xsl:sample/simple-get.xsl -it:main
Run Code Online (Sandbox Code Playgroud)

要么

saxon --repo /usr/share/java/expath/repo -xsl:xquery_file.xsl -it:main
Run Code Online (Sandbox Code Playgroud)

没有成功.我明白了:Transformation failed: Unknown configuration property http://saxon.sf.net/feature/repo

理想情况下,我最终要做的是直接从命令行查询一个URL,而不是XQuery文件,而是一个XPath表达式(如果可能的话).我很确定那里的一些XML/Java/XPath专家有我正在寻找的解决方案.

/usr/share/java/expath/repo 包含:

/usr/share/java/expath/repo
??? expath-http-client-saxon-0.10.0
?   ??? cxan.xml
?   ??? expath-http-client-saxon
?   ?   ??? jar
?   ?   ?   ??? expath-http-client-java.jar
?   ?   ?   ??? expath-http-client-saxon.jar
?   ?   ??? lib
?   ?   ?   ??? apache-mime4j-0.6.jar
?   ?   ?   ??? commons-codec-1.4.jar
?   ?   ?   ??? commons-logging-1.1.1.jar
?   ?   ?   ??? httpclient-4.0.1.jar
?   ?   ?   ??? httpcore-4.0.1.jar
?   ?   ?   ??? tagsoup-1.2.jar
?   ?   ??? xq
?   ?   ?   ??? expath-http-client-saxon.xq
?   ?   ??? xsl
?   ?       ??? expath-http-client-saxon.xsl
?   ??? expath-pkg.xml
?   ??? saxon.xml
??? hello-1.1
    ??? expath-pkg.xml
    ??? hello
        ??? hello.xq
        ??? hello.xsl
Run Code Online (Sandbox Code Playgroud)

编辑:

我最好的尝试(基于Linux的解决方案)

java -classpath "./tagsoup-1.2.jar:./saxon9he.jar" \
    net.sf.saxon.Query \
   -x:org.ccil.cowan.tagsoup.Parser \
   -s:myrealLife.html \
   -qs://*:body
Run Code Online (Sandbox Code Playgroud)

这项工作,但现在我试图弄清楚如何设置default namespace能够通过示例直接查询//a

编辑2

我根据这个POST创建了一个完整的github项目,请查看https://github.com/sputnick-dev/saxon-lint

Mic*_*Kay 5

我认为你不需要任何HTTP客户端.您可以使用doc()函数读取该文件,或者将其作为主输入文档提供,前提是您将其配置为使用HTML SAX解析器而不是XML解析器进行解析.如果你把John Cowan的TagSoup放在类路径上,那么就调用Saxon

-x:org.ccil.cowan.tagsoup.Parser -s:myrealLife.html
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.

我想你也可以使用validator.nu,它比HTMLS比HTMLS更加快速,但我自己没有尝试过.