使用哪种语言来解析

使用哪种语言来解析

本文介绍了使用哪种语言来解析 xml 以进行导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些 php 代码来解析已经使用 SimpleXML 的 xml 文件,它非常混乱,但如果有帮助,可以包括.

I have written some php code to parse a xml file already with SimpleXML, its pretty messy, but can include if that helps.

这就是xml文件的样子(导航文件,做的很奇怪,无法更改)

This is what the xml file looks like (navigation file, thats been done really weirdly and can't be changed)

    <root name="menutest">
      <menu>
          <nav name="home" path="index.php" />

          <nav name="menulink1" path="link1.php">
              <nav name="menulink1child1" path="menulink1childlink1.php">
                 <nav name="menulink1child1child1"
                      path="menulink1childlink1childlink1.php" />
              </nav>
          </nav>

          <nav name="menulink2" path="link2.php">
             <nav name="menulink1child2" path="menulink2childlink2.php">
                <nav name="menulink2child2child2"
                     path="menulink2childlink2childlink2.php" />
             </nav>
          </nav>

          <nav name="menulink3" path="link3.php">
             <nav name="menulink3child3" path="menulink3childlink3.php">
                 <nav name="menulink3child3child3"
                      path="menulink3childlink3childlink3.php" />
             </nav>
          </nav>

          <nav name="menulink4" path="link4.php">
             <nav name="menulink4child4"
                  path="menulink4childlink4.php">
                 <nav name="menulink4child4child4"
                   path="menulink4childlink4childlink4.php" />
             </nav>
          </nav>
      </menu>
  </root>

我想只使用 SimpleXML 来解析和 XPATH 来选择节点,然后使用 $_SERVER 请求使用页面 URL,这样我就可以检测应该打开哪个菜单项以使用正确的 css 样式,例如将样式添加到导航名称 =menulink1child1child1".注意,当顶级页面和后续子元素进入该元素的子页面时,只应为它们选择第 1 层导航元素

I was thinking using just SimpleXML to parse and XPATH to select nodes and then using the page URL using $_SERVER requests so I could detect what menu item should be open to use the right css styles e.g. add style to nav name="menulink1child1child1.Note that only tier 1 nav elements should be selected for top level pages and subsequent child elements when they go into child pages of that element

 homepage:  home | menulink1 | menulink2| menulink3 | menulink4 |
 menulink1: menulink1
            -- childlink1
            | menulink2 | menulink3 | menulink4

注意菜单是垂直的,不是水平的,所以子元素需要以父元素的正确顺序出现,而不是自动出现在网页的新行的下方:)

Note that menu is vertical, not horizontal, so child elements need to appear in the right order of the parent elements, not just automatically below on a new row of the webpage :)

我的问题是;解析需要条件来选择子元素的 xml 导航文件的最佳方法是什么?我应该考虑使用 XSL 来转换 xml 吗?

My question is; what is the best way to parse an xml navigation file that requires conditions to select the children elements? Should I be looking at using XSL to transform the xml?

TIA贾里德

推荐答案

值得投资学习 XSLT.这是一种有些人一见钟情的语言,许多人在掌握它之前就放弃了,但是一旦你掌握了它,它就可以很好地解决此类问题,并且可以同时用于服务器 -端和客户端.

It's worth the investment in learning XSLT. It's a language that some people find off-putting at first sight, and many give up before they've mastered it, but once you've sussed it, it works really well for this kind of problem, and can be used both server-side and client-side.

这篇关于使用哪种语言来解析 xml 以进行导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:17