libxml2 を使用した iOS アプリで、この HTML 部分を解析している間 (これは大きなページの一部です) -
...
<span class="ingredient">
<span class="amount">
<span class="value">500 </span>
<span class="type">g</span>
</span>
<a href="...">bread flour</a>
or
<span class="ingredient">
<span class="amount">
<span class="value">500 </span>
<span class="type">g</span>
</span>
<span class="name">
<a href="...">all-purpose flour</a>
</span>
</span>
</span>
...
「500 g パン粉または 500 g 中力粉」というテキストだけを抽出する必要があります。
//span[@class="ingredient"]
返された XPath クエリの解析済み NSDictionary 結果-
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = ingredient;
}
);
nodeChildArray = (
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = amount;
}
);
nodeChildArray = (
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = value;
}
);
nodeContent = 500;
nodeName = span;
},
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = type;
}
);
nodeContent = g;
nodeName = span;
}
);
nodeContent = "";
nodeName = span;
},
{
nodeAttributeArray = (
{
attributeName = href;
nodeContent = "http://www.food.com/library/flour-64";
}
);
nodeContent = "bread flour";
nodeName = a;
},
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = ingredient;
}
);
nodeChildArray = (
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = amount;
}
);
nodeChildArray = (
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = value;
}
);
nodeContent = 500;
nodeName = span;
},
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = type;
}
);
nodeContent = g;
nodeName = span;
}
);
nodeContent = "";
nodeName = span;
},
{
nodeAttributeArray = (
{
attributeName = class;
nodeContent = name;
}
);
nodeChildArray = (
{
nodeAttributeArray = (
{
attributeName = href;
nodeContent = "http://www.food.com/library/flour-64";
}
);
nodeContent = "all-purpose flour";
nodeName = a;
}
);
nodeContent = "";
nodeName = span;
}
);
nodeContent = "";
nodeName = span;
}
);
nodeContent = or;
nodeName = span;
}
問題は、ディクショナリ ルートの「nodeContent」がテキスト「または」であり、すべてのタグがルート ノードの子として配置されているため、断片の順序が失われていることです。すべてのテキストを連結すると、次の文字列が得られます:「または 500 g パン粉 500 g 万能粉」。
純粋なテキストを 1 つの XPath クエリで抽出する方法、または代わりに XPath エンジンを使用して要素の順序付きリストを読み取る方法を見つけられる人はいますか?