Question

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!

Was it helpful?

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).

OTHER TIPS

I have this XML:

<List>
    <Attribute>
        <Id>Name</Id>
        <Value>JOHN</Value>
    </Attribute>
</List>

Below is query:

SELECT EXTRACTVALUE(xml, "List/Attribute[Id[text()='NAME']]/Value") FROM xx;

But getting error as Unknown column 'List/Attribute[Id[text()='COUNTRY']]/Value' in 'field list'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top