简体   繁体   English

PHP:如何直接将xpath转换为节点/属性

[英]PHP: how to directly convert xpath to node/attribute

I wonder if in PHP there is a direct way (without iterations) to convert xpath, say: 我想知道在PHP中是否有直接方法(无需迭代)来转换xpath,例如:

component_volume_concentration/component[1][@formula="CH4"]
component_volume_concentration/component[1][@unit="%"]
component_volume_concentration/component[1][@min="22"]
component_volume_concentration/component[1][@norm="25"]
component_volume_concentration/component[1][@max="30"]

to XML node and attribute structure: XML节点和属性结构:

<component_volume_concentration> 
<component formula="CH4" unit="%" min="22" norm="25" max="30" />
<component formula="C2H6" unit="%" min="22" norm="25" max="30" />
<component formula="C3H8" unit="%" min="22" norm="25" max="30" />
<component formula="i-C4H10" unit="%" min="22" norm="25" max="30" />
<component formula="i-C5H12" unit="%" min="22" norm="25" max="30" />
</component_volume_concentration> 

Thank you! 谢谢!

No, use XQuery or XSLT . 不,请使用XQueryXSLT

Xpath is a query language to select nodes. Xpath是用于选择节点的查询语言。 It is used in XSLT and is a subset of XQuery. 它在XSLT中使用,是XQuery的子集。

It is not possible to expand all Xpath expressions to valid nodes. 不可能将所有Xpath表达式扩展到有效节点。

How would you expand something like .//component[@formula="CH4"][last()] or count(//node[@attr]) > 0 . 您将如何扩展.//component[@formula="CH4"][last()]count(//node[@attr]) > 0

You can define an own Xpath like syntax and develop an interpreter for it. 您可以定义自己的类似Xpath的语法,并为其开发解释器。 But this would be a lot of work and you need a really good knowledge of XML (namespaces, node types, whitespace handling, cdata sections). 但这将需要大量工作,并且您需要非常了解XML(命名空间,节点类型,空白处理,cdata部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM