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

Chirag Dhyani
  • 863
  • 11
  • 24

2 Answers2

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

Mike Miller
  • 3,071
  • 3
  • 25
  • 32
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
        )

)
mhall
  • 3,671
  • 3
  • 23
  • 35