【问题标题】:Xalan XPathAPI traverse questionXalan XPathAPI 遍历问题
【发布时间】:2011-03-10 07:14:23
【问题描述】:

我正在尝试使用 xalan XPathAPI 解析 xhtml 文件。我坚持以下要求。这是来自 xhtml 的 sn-p

<table border="0" cellspacing="0" cellpadding="0" class="cmnt_message">
            <tr>
              <td width="33" align="right">
                <span class="cmnt_baloon"><!-- Image --></span>
              </td>
              <td width="767" class="red pad_l_10">
                Posted by Macha on Mar 06, 2011 at 01:02 PM
              </td>

            </tr>
            <tr>
              <td colspan="2" class="cmnt_text">
                @rmaytee<br />
                <br />
                #2<br />
                <br />
                In 2011 it is possible to switch to old mat/map browser<br />

                <br />
                Just look around<br />
                <br />
                <a target="_blank" href=
                "http://area.autodesk.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/material-editor/">area.autodesk.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/material-editor/</a><br />

                <br />
                <br />
                <br />

              </td>
            </tr>
          </table>
          <table border="0" cellspacing="0" cellpadding="0" class="cmnt_message">
            <tr>
              <td width="33" align="right">
                <span class="cmnt_baloon"><!-- Image --></span>
              </td>
              <td width="767" class="red pad_l_10">

                Posted by rmaytee on Mar 02, 2011 at 06:04 PM
              </td>
            </tr>
            <tr>
              <td colspan="2" class="cmnt_text">
                2 things:<br />
                <br />
                1- Please bring back "use object center as start snap point" in the snap settings. We have voiced our opinion about this, now please show us you care. <a target="_blank" href=
                "http://www.the-area.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/use-object-center-as-start-snap-point">www.the-area.com/forum/autodesk-3ds-max/autodesk-3ds-max--3ds-max-design-2011/use-object-center-as-start-snap-point</a><br />

                <br />
                2- Make the Material/Map Browser the way it used to be. It is SO SLOW. At least make an option to switch to a "classic Material/Map Browser" or something.
              </td>
            </tr>
          </table>

我在这里遇到了几个问题。

  1. 我正在尝试提取 cmnt_message 类的值。一个是第一个块下的“发布者...”文本和 cmnt_text 下的文本内容。这是部分发布的第一个 xpath

/html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[1]/html:td[2] /文本()

这将返回“由 Macha 于 2011 年 3 月 6 日下午 1:02 发布”,这就是我想要的。但是当我尝试使用以下 xpath 表达式获取 cmnt_text 时

/html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[2]/html:td/text( )

我得到“@rmaytee”,即直到第一个
的值。我正在尝试获取 cmnt_text 中的整个文本。

  1. 其他问题是我需要遍历 cmnt_message 并创建一个 Message 对象的集合,该对象由发布者和评论组成。 不确定如何使用 Xpath 进行迭代。

    SAX2DOM sax2dom = new SAX2DOM(); p.setContentHandler(sax2dom); p.parse(new InputSource(urlXML.openStream())); 节点文档 = sax2dom.getDOM(); XObject 注释 = XPathAPI.eval(doc,commentPath);

但这只会返回第一次出现的 cmnt_message 类。

任何指针都将受到高度赞赏。

  • 谢谢

【问题讨论】:

  • 查看我的答案,了解两种可能的解决方案和一项重要建议。

标签: java xpath xalan


【解决方案1】:

你想要的是string value。

如果您的 XPath 引擎作为结果支持字符串数据类型,您可以使用:

string(
    /html:html
       /html:body
          //html:div[@class='content_d']
             /html:table[@class='cmnt_message']
                /html:tr[1]
                   /html:td[2]
)

或者选择td元素并使用正确的DOM方法。

对于像 XHTML 这样的混合内容模型使用文本节点并不好。

【讨论】:

    【解决方案2】:

    您必须自己使用 XSL 或迭代 /html:html/html:body//html:div[@class='content_d']/html:table[@class='cmnt_message']/html:tr[2]/html:td 的子节点。

    【讨论】:

    • 感谢 Jochen,我对 Xpath 没有任何疑问,而是在努力迭代列表。我已经想通了。
    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    相关资源
    最近更新 更多