Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使xpath查询动态化?_Php_Xpath - Fatal编程技术网

Php 如何使xpath查询动态化?

Php 如何使xpath查询动态化?,php,xpath,Php,Xpath,我正在使用PHP使用XPATH。我是用硬编码的方法做的。但是为了使xpath查询动态化,我做了很多谷歌搜索。有没有办法使xpath查询成为动态的 我有以下xml代码: <information> <field name="secondtitle">ABCt</field> <field name="author">XYZ</field> <field name="ISBN">9780712617611</field&g

我正在使用PHP使用XPATH。我是用硬编码的方法做的。但是为了使xpath查询动态化,我做了很多谷歌搜索。有没有办法使xpath查询成为动态的

我有以下xml代码:

<information>
<field name="secondtitle">ABCt</field>
<field name="author">XYZ</field>
<field name="ISBN">9780712617611</field>
<field name="publisher">abc</field>
</information>
这段代码正在使用我上面粘贴的第二段xml代码。

您可以使用一个“skelleton”表达式并生成一个“可执行实例”。我不懂PHP,但在C语言中,可以这样做:

string XpathExpression 
   = string.Format("/products/product[$i]/additional/field[@name='{0}']",
                    myName)

XPATH提供了一些运算符,希望您正在使用它们。As |运算符是用于对两个查询进行并集的用户,还有一个运算符用于对两个或多个查询进行交集

下面我建议您使用:

$auth = $xml->xpath("/products/product[$i]/additional/field[@name='auteur' or @name='author']");
 foreach($auth as $au)
{
foreach($type as $ty)
{
echo $au = mysql_real_escape_string($au);

}}
对于作者来说,它在这里起作用,对于产品类型的问题,请遵循下面的代码

  $type = $xml->xpath("/products/product[$i]/additional/field[@name='type']");
if(!empty($type ))
{
  echo 'the node is available';
}
else
{
  echo 'the node is not available';
}

xml有'information'元素,但xpath有'additional'元素。不应该是/products/product/information/field[@name='auteur']”之类的吗?最明显的方法是使用if/else和字符串连接查询。请参见示例。@Himanshu,这是完整查询的一部分file@Gordonthnx我用同样的方式做了这件事
$auth = $xml->xpath("/products/product[$i]/additional/field[@name='auteur' or @name='author']");
 foreach($auth as $au)
{
foreach($type as $ty)
{
echo $au = mysql_real_escape_string($au);

}}
  $type = $xml->xpath("/products/product[$i]/additional/field[@name='type']");
if(!empty($type ))
{
  echo 'the node is available';
}
else
{
  echo 'the node is not available';
}