3

I have mysql table (say TestSuite), holding xml content (although as long text) in TestSuiteDefinition column,

 <test_suite id="368">
   <name>TestSuite1</name>
   <description>TestSuite</description>
   <test_case id="141" version="" />
   <test_case id="142" version="" />
   <test_case id="143" version="" />
   <test_case id="144" version="" />
</test_suite>

now, I want to retrieve the value of attributes ("id" in this case). I know how to do it in MS SQL e.g:

SELECT TestSuiteDefinition.query('data(/test_suite/test_case/@id)') as name FROM TestSuite WHERE TestSuiteId='368'

But not able to figure it out in MySQL. Note: Tried MySQL function ExtractValue() but no success on retrieving element attributes. Thanks

2
  • Can you show your attempt using MySQL ExtractValue? Commented Mar 30, 2015 at 9:52
  • 1
    SELECT ExtractValue('<test_case @id></test_case>','/test_case[@id]') as name FROM TestSuite WHERE TestCaseId=''368" Commented Mar 30, 2015 at 9:56

2 Answers 2

2
$rows = $mysqli->query(<<<EOQ
    SELECT ExtractValue(TestSuiteDefinition,'//test_case/@id') as name
    FROM   TestSuite
    WHERE  TestCaseId=368
EOQ
) or die($mysqli->error);

print_r($rows->fetch_all());

Output:

Array
(
    [0] => Array
        (
            [0] => 141 142 143 144
        )

)
Sign up to request clarification or add additional context in comments.

Comments

2

I haven't tested this but give this a try

SELECT ExtractValue(TestSuiteDefinition,'/test_case[1]/@id') as name FROM TestSuite WHERE TestCaseId=''368"

This will get you the first one back. You will need to do multiple ExtractValue calls to get each attribute back I think

2 Comments

Thank for the effort Mike. but here I am trying to retrieve attribute value instead of node values. So the query should return 141 142 143 144
Updated the answer- also untested as I am on my phone but should be pretty close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.