Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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中给定节点的子元素出现的次数?_Php_Xml - Fatal编程技术网

如何计算php中给定节点的子元素出现的次数?

如何计算php中给定节点的子元素出现的次数?,php,xml,Php,Xml,我有以下XML文件,我想知道标记“ing”在每个“ing div”节点中出现的确切次数。这将有助于我确定每个配料标题的配料数量(即水果有4种配料,海绵蛋糕有4种配料,…)。有什么建议吗 <?xml version="1.0" encoding="UTF-8"?> <recipeml version="0.5"> <recipe> <head> <title>1,000 Calorie-A-Bite Trifle

我有以下XML文件,我想知道标记“ing”在每个“ing div”节点中出现的确切次数。这将有助于我确定每个配料标题的配料数量(即水果有4种配料,海绵蛋糕有4种配料,…)。有什么建议吗

<?xml version="1.0" encoding="UTF-8"?>

<recipeml version="0.5">
  <recipe>
    <head>
      <title>1,000 Calorie-A-Bite Trifle</title>
      <categories>
        <cat>Desserts</cat>
        <cat>Usenet</cat></categories>
      <yield>1</yield></head>
    <ingredients>
      <ing-div>
        <title>FRUIT</title>
        <ing>
          <amt>
            <qty>3</qty>
            <unit/></amt>
          <item>Pears</item></ing>
        <ing>
          <amt>
            <qty>8</qty>
            <unit>ounces</unit></amt>
          <item>Raspberries (tinned or fresh)</item></ing>
        <ing>
          <amt>
            <qty>1</qty>
            <unit/></amt>
          <item>Passion fruit</item></ing>
        <ing>
          <amt>
            <qty/>
            <unit/></amt>
          <item>Dry sherry (1 bottle)</item></ing></ing-div>
      <ing-div>
        <title>SPONGE CAKE</title>
        <ing>
          <amt>
            <qty>1/2</qty>
            <unit>cups</unit></amt>
          <item>Butter</item></ing>
        <ing>
          <amt>
            <qty>10</qty>
            <unit>tablespoons</unit></amt>
          <item>Sugar, castor</item></ing>
        <ing>
          <amt>
            <qty>1 1/4</qty>
            <unit>cups</unit></amt>
          <item>Flour, self-raising</item></ing>
        <ing>
          <amt>
            <qty>2</qty>
            <unit/></amt>
          <item>Eggs (slightly whisked)</item></ing></ing-div>
      <ing-div>
        <title>CUSTARD</title>
        <ing>
          <amt>
            <qty>2</qty>
            <unit/></amt>
          <item>Eggs</item></ing>
        <ing>
          <amt>
            <qty>1</qty>
            <unit>pinch</unit></amt>
          <item>Salt</item></ing>
        <ing>
          <amt>
            <qty>1</qty>
            <unit>pinch</unit></amt>
          <item>Nutmeg</item></ing>
        <ing>
          <amt>
            <qty>10</qty>
            <unit>ounces</unit></amt>
          <item>Double cream (or use whipping cream)</item></ing></ing-div>
      <ing-div>
        <title>TOPPING</title>
        <ing>
          <amt>
            <qty>10</qty>
            <unit>ounces</unit></amt>
          <item>Double cream</item></ing>
        <ing>
          <amt>
            <qty/>
            <unit/></amt>
          <item>Roast almonds</item></ing></ing-div></ingredients>
    <directions>
      <step>  Peel and slice pears, drain raspberries if tinned, and scoop out passion
  fruit.  Place fruit in large trifle bowl and add an ample quantity of
  sherry. Leave for twenty-four hours to soak in the refrigerator.

  Preheat oven to 350 degrees F.  Cream butter and sugar until light and
  fluffy. Add eggs and about 2 T of flour and beat. Fold in rest of flour.
  Bake in 7-inch square tin for 25-30 mins until brown. Let cool. Slice into
  fingers and arrange on top of fruit. More sherry may be added at this
  point.

  Pour one large glass of sherry.  Mix eggs and add all ingredients to small
  bowl.  Place bowl in pan of simmering water. Stir continuously with wooden
  spoon, sipping sherry, until custard thickens. This takes about ten
  minutes. Pour custard on top of sponge. Chill in fridge. Whip cream until
  stiff and smooth over top of custard. Arrange almonds decoratively.

  NOTES:

  *  The title says it all -- This recipe is my own invention.

  : Difficulty:  moderate
  : Time:  1 hour preparation, 1 day waiting, 10 minutes cooking.
  : Precision:  no need to measure.

  : Angi Lamb
  : Department of Computer Science, University of York, UK
  : ukc!minster!angi

  : Copyright (C) 1986 USENET Community Trust

  From Gemini's MASSIVE MealMaster collection at www.synapse.com/~gemini

</step></directions></recipe></recipeml>

一口1000卡路里的小点心
甜点
Usenet
1.
果
3.
梨
8.
盎司
覆盆子(罐装或新鲜)
1.
西番莲
干雪利酒(1瓶)
海绵蛋糕
1/2
杯子
黄油
10
汤匙
糖,蓖麻
1 1/4
杯子
自养面粉
2.
鸡蛋(略带胡须)
蛋奶沙司
2.
鸡蛋
1.
捏
盐
1.
捏
肉豆蔻
10
盎司
双份奶油(或使用搅打奶油)
浇汁
10
盎司
双层奶油
烤杏仁
梨去皮切片,覆盆子罐头沥干水份,取出西番莲
水果。把水果放在一个小盆里,加入足量的水果
雪莉。在冰箱里浸泡24小时。
预热烤箱至350华氏度。将黄油和糖打成奶油状
毛茸茸的加入鸡蛋和2吨面粉,搅拌均匀。把剩下的面粉拌进去。
在7英寸见方的铁罐中烘焙25-30分钟,直到变成棕色。让我们冷静下来。切成
将手指和手指放在水果上。此时可能会添加更多雪利酒
指向
倒一大杯雪利酒。把鸡蛋混合,把所有的原料都加入小鸡蛋里
碗。将碗放入盛有沸水的平底锅中。用木棍连续搅拌
勺子,啜饮雪利酒,直到奶油冻变稠。这大约需要十分钟
分钟。将奶油冻倒在海绵上。在冰箱里冷藏。将奶油搅打至
在奶油冻的上面坚硬光滑。装饰性地摆放杏仁。
笔记:
*标题说明了一切——这个食谱是我自己发明的。
:难度:中等
:时间:1小时准备,1天等待,10分钟烹饪。
:精度:无需测量。
:安吉·兰姆
英国约克大学计算机科学系
:ukc!部长!安琪
:版权所有(C)1986 USENET社区信托
来自双子座的大量米尔马斯特收藏,网址:www.synapse.com/~Gemini
将XML字符串发送到此函数并检查结果,结果现在是一个易于使用的数组:

foreach($result['recipe'] as $recipe){
  echo $recipe['title'].' - '.count($recipe['ingredients']['ing']);
}
编辑:

只需为while循环添加一个迭代器

$xml = new DOMDocument();
$xml->load(path/to/file);
$xpath = new DOMXPath($xml);
$ingdiv = $xpath->query("/recipeml/recipe/ingredients/ing-div");
$length = $ingdiv->length;
$key=0;
// iterate over all <ing-div> from last to first
while($key<$length) {
 // number of <ing> in the specific <ing-div> 
  print $xpath->query("ing", $ingdiv->item($key))->length;
  $key++ 
}
$xml=newDOMDocument();
$xml->load(路径/到/文件);
$xpath=newdomxpath($xml);
$ingdiv=$xpath->query(“/recipeml/recipe/components/ingdiv”);
$length=$ingdiv->length;
$key=0;
//从最后一个迭代到第一个
while($keyquery(“ing”),$ingdiv->item($key))->length;
$key++
}

DomDocument有一个功能

如何使用getElementsByTagName:

$doc = new DOMDocument();
$doc->load(<xml>);
foreach( $doc->getElementsByTagName('ing') as $tag ) 
{
    // to iterate the children
    foreach( $tag->childNodes as $child ) 
    {
        // outputs the xml of the child nodes. Of course, there are any number of
        // things that you could do instead!
        echo $doc->saveXML($child);
    }
}
$doc=newDOMDocument();
$doc->load();
foreach($doc->getElementsByTagName('ing')作为$tag)
{
//重复孩子们
foreach($tag->childNodes作为$child)
{
//输出子节点的xml
//你可以做的事情!
echo$doc->saveXML($child);
}
}
看看如何将Xpath与一起使用。然后可以计算查询生成的结果数

php > $doc = new DOMDocument();
php > $doc->preserveWhiteSpace = false;
php > $doc->Load("/tmp/test.xml");
php > $xpath = new DOMXPath($doc);
php > $query = "//ing";
php > $ingredients = $xpath->query($query);
php > echo $ingredients->length;
int(14)

您可能需要修改Xpath查询以满足您的需要。

我在显示配方标题和配料标题时没有问题,问题是我想知道每个配料标题有多少配料。此代码从最后一个到第一个,但不是从第一个。有什么建议吗?$xml=new DOMDocument();$xml->load(path/to/file);$xpath=new-DOMXPath($xml);$ingdiv=$xpath->query(/recipeml/recipe/components/ing-div”);$length=$ingdiv->length;//在($length--){//特定打印中的数量$xpath->query($ing“,$ingdiv->->item($length))->length;//从上到下迭代哎呀,我错过了一些东西,我的原始代码,也修复了你的Hanks Trey,现在工作很好!谢谢大家的帮助!
php > $doc = new DOMDocument();
php > $doc->preserveWhiteSpace = false;
php > $doc->Load("/tmp/test.xml");
php > $xpath = new DOMXPath($doc);
php > $query = "//ing";
php > $ingredients = $xpath->query($query);
php > echo $ingredients->length;
int(14)