如何计算出现在两个字符串中的单词数?
我在考虑这样的事情
let $nequalwords := count($item[text() eq $speech])
Run Code Online (Sandbox Code Playgroud)
做这个的最好方式是什么?
我想与两个人for逐字逐句比较,但我不知道是否有更好的方法来做到这一点.
我先搜索谷歌,然后发现了很多关于如何用xpath解析xml文档的结果.我已解析它但想要在String中转换NODELIST并且我已经为它创建了一个方法:
private String processResult(Document responseDocument) throws XPathExpressionException, TransformerException {
NodeList soaphead = responseDocument.getElementsByTagName("xmlTagToTrasform");
StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(soaphead.item(0)), new StreamResult(sw));
String result = sw.toString();
return result;
}
Run Code Online (Sandbox Code Playgroud)
这种方法工作正常,但变换器<?xml version="1.0" encoding="UTF-8"?>在结果的标题中添加了一个,我不希望这样.这是该方法的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xmlTagToTrasform>
<xmlTagToTrasform2>
.
.
.
.
</xmlTagToTrasform2>
</xmlTagToTrasform>
Run Code Online (Sandbox Code Playgroud) ::的确切含义是什么?除了父母之外,还有什么不同的东西我们可以使用?
By.xpath("parent::*/parent::*")
Run Code Online (Sandbox Code Playgroud) 我使用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
? ? ? …Run Code Online (Sandbox Code Playgroud) 我试图找到xml中每个元素的xpath并将其作为元素值.我的源文件看起来像:
<root>
<parent1>
<child1></child1>
<child2></child2>
</parent1>
<parent2>
<child1></child1>
</parent2>
</root>
Run Code Online (Sandbox Code Playgroud)
我想要一个输出像:
<root>
<parent1>
<child1> /root/parent1/child1 </child1>
<child2> /root/parent1/child2 </child2>
</parent1>
<parent2>
<child1> /root/parent2/child1 </child1>
</parent2>
</root>
Run Code Online (Sandbox Code Playgroud)
我目前得到的输出为:
<root>
<parent1>
<child1> /root/parent1/child1 </child1>
<child2> /root/parent1/child2 </child2>
</parent1>"
<parent2>
<child1> /root/parent1/parent2/child1 </child1>
</parent2>
</root>
Run Code Online (Sandbox Code Playgroud)
我无法正确遍历以找到xpath.任何输入都是有价值的.
输入:
<root>
<aa><aaa/><bbb/><ccc/><ddd/><eee/></aa>
<bb><ggg/></bb>
</root>
Run Code Online (Sandbox Code Playgroud)
理想的输出:
<root>
<aa>aaa<aa>
<aa>bbb<aa>
<aa>ccc<aa>
<aa>ddd<aa>
<aa>eee<aa>
<bb>ggg</bb>
</root>
Run Code Online (Sandbox Code Playgroud)
我想出了简单的xslt,但它只是正确处理,不会创建标签列表.
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- select all elements that doesn't have any child nodes (elements or text etc) -->
<xsl:template match="//*[not(node())]">
<xsl:value-of select="name()"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出:
<root>
<aa>aaabbbcccdddeee</aa>
<bb>ggg</bb>
</root>
Run Code Online (Sandbox Code Playgroud)
PS它是python脚本的一部分.是否可以在python脚本中使用xslt进行此类转换?或者使用简单的xpath和python逻辑的python解决方案会更好吗?
我正试图抓住新的ESPN NBA记分牌.这是一个简单的脚本,应该在4/5/15返回所有游戏的开始时间:
import requests
import lxml.html
from lxml.cssselect import CSSSelector
doc = lxml.html.fromstring(requests.get('http://scores.espn.go.com/nba/scoreboard?date=20150405').text)
#xpath
print doc.xpath("//title/text()") #print page title
print doc.xpath("//span/@time")
print doc.xpath("//span[@class='time']")
print doc.xpath("//span[@class='time']/text()")
#CCS Selector
sel = CSSSelector('span.time')
for i in sel(doc):
print i.text
Run Code Online (Sandbox Code Playgroud)
它不返回任何内容,但页面标题:
['NBA Basketball Scores - NBA Scoreboard - ESPN']
[]
[]
[]
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢
在我们的例子中,我们有这样的动态XML标记
<ab:SomeProcessResponse xmlns:ab="http://something.com/xyz"
xmlns:cd="http://something.com/lmno">
Run Code Online (Sandbox Code Playgroud)
有时我可能会得到回应
<def:SomeProcessResponse xmlns:def="http://something.com/xyz"
xmlns:cd="http://something.com/lmno">
Run Code Online (Sandbox Code Playgroud)
SomeProcessResponse在这种情况下选择节点时应遵循的最佳实践是什么?
这是第一次加载页面时元素x的XPath:
.//*[@id='001g000000YJnpR_00Nb0000004Lzej_body']/table/tbody/tr[3]/td[3]
Run Code Online (Sandbox Code Playgroud)
此页面上的文字是:"text1".
这是第二次加载时相同元素x的XPath:
//*[@id="001g000000YJnm8_00Nb0000004Lzej_body"]/table/tbody/tr[3]/td[3]
Run Code Online (Sandbox Code Playgroud)
此页面上的文字是:"text2".
元素没有名称或ID,这里是HTML代码: '<'td class="dataCell">vxgkVwD7JvnOBKaGCIS7'<'/td'>'
请协助.
我在表中的一行中有如下所示的XML(表中有很多行):
<?xml version="1.0" encoding="UTF-8"?>
<AuditTrail>
<Action />
<ActionDetail />
<ChangesXML>
<Details>
<Object ObjectType="Data.Review_Extension" AuditType="Modified" FriendlyName="Review">
<ObjectKeys>
<ReviewExtID>21482283</ReviewExtID>
</ObjectKeys>
<Properties>
<Property name="Document Type 01" FieldName="Document_Type_01" TemplateFieldID="644140" ReviewExtensionID="214822182" PropertyType="System.String">
<OldValue />
<NewValue><![CDATA[1145]]></NewValue>
</Property>
<Property name="Document Type 02" FieldName="Document_Type_02" TemplateFieldID="644141" ReviewExtensionID="21482283" PropertyType="System.String">
<OldValue />
<NewValue><![CDATA[123]]></NewValue>
</Property>
</Properties>
</Object>
</Details>
</ChangesXML>
</AuditTrail>
Run Code Online (Sandbox Code Playgroud)
我需要编写一个查询(在SQL Server 2008中),对于源表中的每一行,它将为PropertyXML中的EACH 元素输出一行.因此,如果我在上面的记录中查询,我会得到以下结果集:
UserId Timestamp PropertyName
-------------------------------------------------
1 1-1-2011 00:11:22:11 Document_Type_01
2 1-1-2011 00:11:22:11 Document_Type_02
Run Code Online (Sandbox Code Playgroud)
我的源表看起来像这样:
UserId Timestamp XML
--------------------------------------
1 1-1-2011 00:11:22:11 <XML>
2 4-1-2011 00:22:33:22 <XML>
3 …Run Code Online (Sandbox Code Playgroud)