XML output code snippets

Developed In: SQL — Contributed by: Erik Wetterberg

Sql stored functions to generate xml fragments from your select. For examples, go to http://eriksdiary.blogspot.com/2007/07/xml-output-from-mysql.html
Erik Wetterberg
SQL
  1. DELIMITER $$
  2.  
  3. DROP FUNCTION IF EXISTS `xml_escape` $$
  4. CREATE FUNCTION `xml_escape`( tagvalue VARCHAR(2000))
  5. RETURNS varchar(2000)
  6. BEGIN
  7. IF (tagvalue IS NULL) THEN
  8. RETURN NULL;
  9. END IF;
  10. RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
  11. tagvalue,'&','&'),
  12. '<','&lt;'),
  13. '>','&gt;'),
  14. '"','&quot;'),
  15. '\'','&apos;');
  16. END $$
  17.  
  18. DROP FUNCTION IF EXISTS `xml_attr` $$
  19. CREATE FUNCTION `xml_attr`(tagname VARCHAR(2000),
  20. tagvalue VARCHAR(2000))
  21. RETURNS varchar(2000)
  22. BEGIN
  23. IF (tagvalue IS NULL) THEN
  24. RETURN NULL;
  25. END IF;
  26. RETURN CONCAT(' ', tagname ,'="',xml_escape(tagvalue),'" ');
  27. END $$
  28.  
  29. DROP FUNCTION IF EXISTS `xml_tag` $$
  30. CREATE FUNCTION `xml_tag`(tagname VARCHAR(2000),
  31. tagvalue VARCHAR(2000),
  32. attrs VARCHAR(2000),
  33. subtags VARCHAR(2000))
  34. RETURNS varchar(2000)
  35. BEGIN
  36. DECLARE result VARCHAR(2000);
  37. SET result = CONCAT('<' , tagname);
  38. IF attrs IS NOT NULL THEN
  39. SET result = CONCAT(result,' ', attrs);
  40. END IF;
  41. IF (tagvalue IS NULL AND subtags IS NULL) THEN
  42. RETURN CONCAT(result,' />');
  43. END IF;
  44. RETURN CONCAT(result ,'>',ifnull(xml_escape(tagvalue),''),
  45. ifnull(subtags,''),'</',tagname, '>');
  46. END $$
  47.  
  48. DELIMITER ;

Current Tags

You must be logged in to tag this tool

No Comments yet

Votes

Not yet rated.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment