Search code examples
xpathquerypath

XPath is returning element including the tags


I am using xPath to read an XML file, but when trying to get the contents of a certain element, it returns it including the tags <> .

The XML file is structured like this:

<item>
    <attribute01>something</attribute01>
    <attribute02>something</attribute02>
    <attribute03>something</attribute03>
    <attribute04>
        <category>Some category name</category>
        <category>Some other category name</category>
    </attribute04>
</item>

I am working in the context of //item and then using attribute04//category to get the category element. However, this is what I'm getting back:

xpathparser:04 :
<Category>Bed &amp; Breakfast and Inns</Category>
xpathparser:04 :
<Category>Hotel</Category>
...etc...

It returns the entire element including the tags. Does anybody have an idea what is going wrong here?

I am using the Feeds xPath parser module for Drupal (https://drupal.org/project/feeds_xpathparser).

Thank you in advance.


Solution

  • In order to select the text portion of a node in an XPath expression you have to use the text() function as follows:

    attribute04/category/text()
    

    The expression

    attribute04/category
    

    will select the XML node instead resulting in the output of the bracings tags and the text.