mysqlxmlxpathmariadbextract-value

MySQL ExtractValue following-sibling alternative


I'm trying to select a node's value from XML in a table in MySQL/MariaDB

Acoording to the MySQL docs, following-sibling is not supported as an XPath axis in MySQL. Is there an alternative?

Docs: http://dev.mysql.com/doc/refman/5.1/en/xml-functions.html#function_extractvalue

My XML structure looks something like:

<fields>
    <record>
        <id>10</id>
        <value>Foo</value>
    </record>
    <record>
        <id>20</id>
        <value>Bar</value>
    </record>
</fields>

I need to find the record with ID 10, and get the text in <value></value>.

Valid XPath would be /fields/record/id[text()=10]/following-sibling::value/text() which would return Foo

What are my options?

Thanks!


Solution

  • In this simple case you do not need the following-sibling. Try this instead:

    /fields/record[id[text()=10]]/value/text()
    

    Using the tag id inside the brackets leaves your context at record so that the following slash descends to the corresponding sibling of id (having the same parent as id).