gpt4 book ai didi

mysql 存储过程 - 验证输入 xml

转载 作者:行者123 更新时间:2023-11-30 23:31:01 26 4
gpt4 key购买 nike

我有一个 mysql 5.1 存储过程,它接受应该包含 XML 的文本输入。

我可以成功解析 XML 并完成所需的一切。现在我正在处理错误处理部分,我需要知道如何确保输入的 XML 格式正确且有效?

谢谢

最佳答案

我不知道在 MySQL 的 XML 支持中有什么说“这是格式正确的 XML”,但你可以伪造它。 MySQL 有一个 ExtractValue功能:

ExtractValue() takes two string arguments, a fragment of XML markup xml_frag and an XPath expression xpath_expr (also known as a locator); it returns the text (CDATA) of the first text node which is a child of the element(s) matched by the XPath expression.

但是您也可以使用它来检查 XML,因为:

If no matching text node is found for the expression (including the implicit /text())—for whatever reason, as long as xpath_expr is valid, and xml_frag consists of elements which are properly nested and closed—an empty string is returned.
[...]
NULL is returned if xml_frag contains elements which are not properly nested or closed, and a warning is generated...

因此,如果您向 ExtractValue 传递任何有效的 XPath 表达式和任何有效的 XML 片段,那么您将得到一个非 NULL 的结果,但如果您使用值 XPath 和无效的 XML,您将得到一个 NULL。因此,您需要做的就是构建一个使用 ExtractValue 检查 XML 和 signal 的 INSERT 和 UPDATE 触发器。如果得到 NULL 则引发异常:

select ExtractValue(new.your_xml_column, '/*') into @xml_check;
if @xml_check is null then
signal sqlstate '45000';
end if;

关于mysql 存储过程 - 验证输入 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10646431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com