English 中文(简体)
Errang 中带有 xmerl_xpath 的上游位置
原标题:Position predicates with xmerl_xpath in Erlang
  • 时间:2012-05-27 17:11:44
  •  标签:
  • xpath
  • erlang

我正在尝试使用xmerl_xpath在Erlang中查询解析的XML文档,但我无法使位置谓词工作。我没有返回第n个子元素,而是获取到目前为止所选内容的第n个元素。

示例代码,其中我想提取值11和21(“第一列”):

{XML,_} = xmerl_scan:string(
    "<table>" ++
        "<row><el>11</el><el>12</el></row>" ++
        "<row><el>21</el><el>22</el></row>" ++
    "</table>" ).

4 = length(xmerl_xpath:string( "//table/row/el", XML )). % OK
1 = length(xmerl_xpath:string( "(//table/row/el)[1]", XML )). % OK
1 = length(xmerl_xpath:string( "//table/row/el[1]", XML )). % Why not 2?

是否需要上次查询的结果?在一般情况下,使用xmerl_path提取第n个子节点的正确方法是什么?

(我真正想做的是使用mochiweb_HTML解析HTML,并使用mochiwebxpath,但后者本质上是xmerlxpath的包装器。)

最佳回答

我不知道Erlang,但第三个XPath确实应该提取第一列——两个节点<;el>;11</el><;el>;21</el>-我创建了一个简单的测试XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <root>
      <xsl:for-each select="//table/row/el[1]">
        <xsl:copy-of select="."/>
      </xsl:for-each>
    </root>
  </xsl:template>
</xsl:stylesheet>

适用于

<table>
  <row>
    <el>11</el>
    <el>12</el>
  </row>
  <row>
    <el>21</el>
    <el>22</el>
  </row>
</table>

生产:

<root>
  <el>11</el>
  <el>21</el>
</root>

如果这不是你得到的,我怀疑你使用的库有缺陷。

问题回答

暂无回答




相关问题
VTD XML inner XPath Expressions

I have a xml file for example like this <root> <test> <bla>test1</bla> </test> <test> <bla>test2</bla> </test> <test> </test&...

C# XML adding/copying

With the following XML I am trying to copy and add to another XML but I haven t used the C# XML document objects before. So here is the XML <config> <configXML> <Connections&...

Filter SQL queries on the XML column using XPath/XQuery

I m having a table with one XML column. I d like to filter out the rows where a specific attribute in the XML match a string, essentially doing a WHERE or HAVING. The table looks something like this ...

Python XPath Result displaying only []

Hey I have just started to use Python recently and I want to use it with a bit of xPath, the thing is when I print the result of the query I only get [] and I don t know why =S import libxml2, ...

热门标签