如何解析Rebol中的层次结构?

发布于 2024-08-24 04:27:06 字数 435 浏览 2 评论 0原文

我重读了章节 http://www.rebol.com/docs/core23/ rebolcore-15.html on any 并解析但无法实现解析这种层次结构:有可能吗?

<Multipage>
<tab id=1>
  <box id=1>
  </box>
</tab>
<tab id=2>
  <box id=2>
  Hello
  </box>
</tab>
<tab>
</tab>
<tab>
</tab>
</Multipage>

I have reread chapter http://www.rebol.com/docs/core23/rebolcore-15.html on any and parse but can't achieve to parse this kind of hiearchical structure: is it possible ?

<Multipage>
<tab id=1>
  <box id=1>
  </box>
</tab>
<tab id=2>
  <box id=2>
  Hello
  </box>
</tab>
<tab>
</tab>
<tab>
</tab>
</Multipage>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-08-31 04:27:06

Gavin MacKenzie 的 xml 解析脚本位于 http://www.rebol .org/view-script.r?script=xml-parse.r1 将解析大多数 XML 数据。由于它是一个通用解决方案,因此它比特定 XML 文件的一组解析规则更复杂,这是可以理解的。

Gavin MacKenzie's xml-parse script at http://www.rebol.org/view-script.r?script=xml-parse.r1 will parse most XML data. As it is a generalised solution it is understandably more complex than a set of parse rules for a specific XML file.

别想她 2024-08-31 04:27:06

是的,这是可能的,而且不是很难:

data: {...}

ws-chars: charset " ^/^M^-"
ws: [any ws-chars]

rule: [
    ws <Multipage> any [
        ws "<tab" opt [ws "id=" copy id to ">" (print ["tab id:" id])] ">" any [
            ws "<box" opt [ws "id=" copy value to ">" (print ["box id:" id])] ">"
                opt [copy text to "<" (if text [?? text])]
            </box>
        ]
        ws </tab>
    ]
    ws </Multipage> ws
]

parse/all data rule

运行此代码,您将得到输出:

tab id: "1"
box id: "1"
text: "^/  "
tab id: "2"
box id: "2"
text: "^/  Hello^/  "

Yes it's possible and not very hard :

data: {...}

ws-chars: charset " ^/^M^-"
ws: [any ws-chars]

rule: [
    ws <Multipage> any [
        ws "<tab" opt [ws "id=" copy id to ">" (print ["tab id:" id])] ">" any [
            ws "<box" opt [ws "id=" copy value to ">" (print ["box id:" id])] ">"
                opt [copy text to "<" (if text [?? text])]
            </box>
        ]
        ws </tab>
    ]
    ws </Multipage> ws
]

parse/all data rule

Running this code, you'll get as output:

tab id: "1"
box id: "1"
text: "^/  "
tab id: "2"
box id: "2"
text: "^/  Hello^/  "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文