php - MySql 查询以检索 xml 的元素属性的值

标签 php mysql xml

我有 mysql 表(比如 TestSuite),在 TestSuiteDefinition 列中保存 xml 内容(尽管是长文本),

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

现在,我想检索属性值(在本例中为“id”)。我知道如何在 MS SQL 中执行此操作,例如:

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

但无法在 MySQL 中计算出来。 注意:尝试了 MySQL 函数 ExtractValue() 但没有成功检索元素属性。 谢谢

最佳答案

$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());

输出:

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

)

关于php - MySql 查询以检索 xml 的元素属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29343016/

相关文章:

php - ASP.net 与 PHP(选择什么)

mysql - SQL过程使用/临时表优化

mysql - SQL查询没有正确使用索引

如果 xml.Marshal 为空,则忽略结构

php - mysqli 绑定(bind)方法和 call_user_func_array

php - 使用 PHP 从 MySQL 数据库中仅检索今天的记录

php - 单引号内的单引号 PHP

java - 使用 Spring-Data Repositories 和 JPQL 查询的 JPA 鉴别器

XML 转换 - XSL 模板匹配多个条件

c# - 如何在不将整个文档加载到内存的情况下处理 Xml 文件?