1

I am working in XPATH using PHP. I have done it by hard coded. but I made a lot of googling for making xpath queries dynamic. Is there any solution for making xpath queries dynamic?

I have the following xml code:

<information>
<field name="secondtitle">ABCt</field>
<field name="author">XYZ</field>
<field name="ISBN">9780712617611</field>
<field name="publisher">abc</field>
</information>

the other file's data is in the following:

<information>
<field name="deliveryCosts">1.95</field>
<field name="deliveryTime">3 - 5 Werkdagen</field>
<field name="EAN">9789021142523</field>
<field name="ISBN">9789021142539</field>
<field name="subcategories">abc</field>
<field name="auteur">IJK</field>
<field name="type">xyz</field>
</information>

only the attributes are differ now m trying to access them in a single php file. but all of my queries are hard coded but i want to access them dynamically.

following is my php code that i made hard coded:

$auth = $xml->xpath("/products/product[$i]/additional/field[@name='auteur']");
$type = $xml->xpath("/products/product[$i]/additional/field[@name='type']");

foreach($auth as $au)
{
foreach($type as $ty)
{
echo $au = mysql_real_escape_string($au);
echo $ty = mysql_real_escape_string($ty);
}}

This code is in working with the second code of xml that I have paste above.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shahai Ali
  • 15
  • 5
  • Your xml has 'information' element but xpath has 'additional'. Shouldn't it be /products/product/information/field[@name='auteur']" or something like that? – Himanshu Oct 20 '12 at 07:55
  • the most obvious approach would be to use if/else and string concatenate the queries. See http://stackoverflow.com/questions/12804692/creating-an-xpath-search-app for an example. – Gordon Oct 20 '12 at 08:09
  • @Himanshu, its the part of the complete file – Shahai Ali Oct 20 '12 at 11:27
  • @Gordon thnx i have done this with the same manner – Shahai Ali Oct 20 '12 at 11:28

2 Answers2

0

You can have a "skelleton" expression and make an "executable instance". I don't know PHP, but in C# this can be done like this:

string XpathExpression 
   = string.Format("/products/product[$i]/additional/field[@name='{0}']",
                    myName)
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
0

XPATH provides some operator hopefully you are using them. As | operator is user for taking union of two queries and also there is an operator for taking intersection of two or more queries.

Here in the following I'm suggesting you use:

$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);

}}

It's working here in case of the author, and for the problem of the type of the product follow my following code.

  $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';
}
halfer
  • 19,824
  • 17
  • 99
  • 186
AliMohsin
  • 329
  • 1
  • 5
  • 10