PostgreSQL/XML: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Created page with "==Query== {|class='wikitable mw-collapsible' style='width:100%;margin:3px 0' !scope='col' style='text-align:left' colspan='2'| XML Query |- |valign='top' style='width:50%'| <syntaxhighlight style='margin:3px 0' lang='sql'> SELECT xml_is_well_formed('<>') AS "<>", -- false xml_is_well_formed('<name >') AS "<name >", -- false xml_is_well_formed('<name />') AS "<name />", -- true xml_is_well_formed('academ..."
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Query==
==Query==
{|class='wikitable mw-collapsible' style='width:100%;margin:3px 0'
{|class='wikitable mw-collapsible'
!scope='col' style='text-align:left' colspan='2'|
!scope='col' style='text-align:left' colspan='2'|
XML Query
Query
|-
|-
|valign='top' style='width:50%'|
|valign='top' style='width:50%'|
<syntaxhighlight style='margin:3px 0' lang='sql'>
<syntaxhighlight lang='sql'>
SELECT
SELECT
     xml_is_well_formed('<>')        AS "<>",                  -- false
     xml_is_well_formed('<>')        AS "<>",                  -- false
Line 16: Line 16:


|valign='top' style='width:50%'|
|valign='top' style='width:50%'|
<syntaxhighlight style='margin:3px 0' lang='sql'>
<syntaxhighlight lang='sql'>
SELECT
SELECT
     xml_is_well_formed_content('<>')        AS "<>",          -- false
     xml_is_well_formed_content('<>')        AS "<>",          -- false
Line 27: Line 27:
|-
|-
|valign='top'|
|valign='top'|
<syntaxhighlight style='margin:3px 0' lang='sql'>
<syntaxhighlight lang='sql'>
SELECT
SELECT
     xml_is_well_formed_document('<>')        AS "<>",        -- false
     xml_is_well_formed_document('<>')        AS "<>",        -- false
Line 40: Line 40:
|-
|-
|valign='top'|
|valign='top'|
<syntaxhighlight style='margin:3px 0' lang='sql'>
<syntaxhighlight lang='sql'>
WITH xpath_node AS (
WITH xpath_node AS (
     SELECT
     SELECT
Line 60: Line 60:


|valign='top'|
|valign='top'|
<syntaxhighlight style='margin:3px 0' lang='sql'>
<syntaxhighlight lang='sql'>
WITH workflow_namespace AS (
WITH workflow_namespace AS (
     SELECT
     SELECT
Line 80: Line 80:
|-
|-
| valign="top" colspan="2"|
| valign="top" colspan="2"|
<syntaxhighlight style='margin:3px 0' lang='sql'>
<syntaxhighlight lang='sql'>
WITH xpath_ns AS (
WITH xpath_ns AS (
     SELECT
     SELECT
Line 126: Line 126:


==References==
==References==
{|class='wikitable mw-collapsible' style='width:100%;margin:3px 0'
{|class='wikitable mw-collapsible'
!scope='col' style='text-align:left' colspan='3'|
!scope='col' style='text-align:left' colspan='3'|
References
References
|-
|-
|valign='top' style='width:33%'|
|valign='top' style='width:33%'|
* [[PostgreSQL/Large Object|PostgreSQL » Large Object]]
* [[Dockerized PostgreSQL|PostgreSQL » Dockerized]]
* [[Dockerized PostgreSQL|PostgreSQL » Dockerized]]
* [[PostgreSQL/PgBouncer|PostgreSQL » PgBouncer]]
* [[PostgreSQL/PgBouncer|PostgreSQL » PgBouncer]]
* [[PostgreSQL/PgLoader|PostgreSQL » PgLoader]]
* [[PostgreSQL/PgLoader|PostgreSQL » PgLoader]]
* [[PostgreSQL/DBLink|PostgreSQL » DBLink]]
* [[PostgreSQL/JSON|PostgreSQL » JSON]]
* [[PostgreSQL/JSON|PostgreSQL » JSON]]
* [[PostgreSQL]]
* [[PostgreSQL]]


|valign='top' style='width:34%'|
|valign='top' style='width:34%'|
* [https://www.postgresql.org/docs/current/functions-json.html PostgreSQL » JSON Functions and Operators]
* [https://stackoverflow.com/questions/23490965/ PostgreSQL » Remove attribute from JSON]
* [https://stackoverflow.com/questions/58124750/ PostgreSQL » Turn a json array into rows]
* [https://stackoverflow.com/questions/22654170/ PostgreSQL » Explanation of JSONB]
* [https://stackoverflow.com/questions/65478350/ PostgreSQL » Varchar to JSON]


|valign='top' style='width:33%'|
|valign='top' style='width:33%'|
* [https://scalegrid.io/blog/using-jsonb-in-postgresql-how-to-effectively-store-index-json-data-in-postgresql/ PostgreSQL » Effectively Store & Index JSON Data]
* [https://www.postgresql.org/docs/12/xml2.html PostgreSQL » 12 » XML2 Functions]
* [https://medium.com/@mnu/update-a-postgresql-table-using-a-with-query-648eefaae2a6 PostgreSQL » Update a table using a WITH query]
* [https://www.postgresql.org/docs/9.1/functions-xml.html PostgreSQL » 9.1 » XML Functions]
* [https://stackoverflow.com/questions/24006291/ PostgreSQL » Return Result Set as JSON Array]
* [https://pgpedia.info/x/xmlelement.html PostgreSQL » XML <code>xmlelement</code>]
* [https://dba.stackexchange.com/questions/251133/ PostgreSQL » Update all values in JSON Array]
* [https://pgpedia.info/categories/xml.html PostgreSQL » XML » Categories]
* [https://stackoverflow.com/questions/53777508/ PostgreSQL » pgAdmin » Export to a CSV File]
 
* [https://stackoverflow.com/questions/44075557/ PostgreSQL » Nested JSON Arrays Query]
* [https://stackoverflow.com/questions/24944347/ PostgreSQL » Nested JSON Querying]
* [https://stackoverflow.com/questions/20272650/ PostgreSQL » Loop over JSON Arrays]
* [https://stackoverflow.com/questions/1517635/ PostgreSQL » Export to a CSV File]
* [https://www.postgresql.org/docs/14/queries-with.html PostgreSQL » WITH Queries]
|-
|-
|valign='top'|
|valign='top'|

Latest revision as of 20:48, 19 January 2026

Query

Query

SELECT
    xml_is_well_formed('<>')         AS "<>",                  -- false
    xml_is_well_formed('<name >')    AS "<name >",             -- false
    xml_is_well_formed('<name />')   AS "<name />",            -- true
    xml_is_well_formed('academia')   AS "academia",            -- true
    xml_is_well_formed('{academia')  AS "{academia",           -- true
    xml_is_well_formed('{academia}') AS "{academia}";          -- true
SELECT
    xml_is_well_formed_content('<>')         AS "<>",          -- false
    xml_is_well_formed_content('<name >')    AS "<name >",     -- false
    xml_is_well_formed_content('<name />')   AS "<name />",    -- true
    xml_is_well_formed_content('academia')   AS "academia",    -- true
    xml_is_well_formed_content('{academia')  AS "{academia",   -- true
    xml_is_well_formed_content('{academia}') AS "{academia}";  -- true
SELECT
    xml_is_well_formed_document('<>')         AS "<>",         -- false
    xml_is_well_formed_document('<name >')    AS "<name >",    -- false
    xml_is_well_formed_document('<name />')   AS "<name />",   -- true
    xml_is_well_formed_document('academia')   AS "academia",   -- false
    xml_is_well_formed_document('{academia')  AS "{academia",  -- false
    xml_is_well_formed_document('{academia}') AS "{academia}"; -- false
WITH xpath_node AS (
    SELECT
        cast(raw_xpath_node->>0 AS integer) "sn",
        cast(raw_xpath_node->>1 AS varchar) "node"
    FROM jsonb_array_elements('[
        [0, ""],
        [1, "bpmn:definitions"],
        [2, "bpmn:process"],
        [3, "bpmn:serviceTask"],
        [4, "bpmn:extensionElements"],
        [5, "camunda:connector"],
        [6, "camunda:inputOutput"],
        [7, "camunda:inputParameter"]
    ]'::jsonb) AS raw_xpath_node
)
SELECT string_agg(node, '/') AS "path" FROM xpath_node;
WITH workflow_namespace AS (
    SELECT
        cast(raw_workflow_namespace->>0 AS varchar) "prefix",
        cast(raw_workflow_namespace->>1 AS varchar) "namespace"
    FROM jsonb_array_elements('[
        ["bpmn",    "http://www.omg.org/spec/BPMN/20100524/MODEL"],
        ["bpmndi",  "http://www.omg.org/spec/BPMN/20100524/DI"],
        ["dc",      "http://www.omg.org/spec/DD/20100524/DC"],
        ["di",      "http://www.omg.org/spec/DD/20100524/DI"],
        ["modeler", "http://camunda.org/schema/modeler/1.0"],
        ["camunda", "http://camunda.org/schema/1.0/bpmn"]
    ]'::jsonb) AS raw_workflow_namespace
)
-- SELECT * FROM workflow_namespace;
-- SELECT array[prefix, namespace] FROM workflow_namespace;
SELECT array_agg(array[prefix, namespace]) FROM workflow_namespace;
WITH xpath_ns AS (
    SELECT
        cast(raw_xpath_ns->>0 AS varchar) "alias",
        cast(raw_xpath_ns->>1 AS varchar) "namespace"
    FROM jsonb_array_elements('[
        ["bpmn",    "http://www.omg.org/spec/BPMN/20100524/MODEL"],
        ["bpmndi",  "http://www.omg.org/spec/BPMN/20100524/DI"],
        ["dc",      "http://www.omg.org/spec/DD/20100524/DC"],
        ["di",      "http://www.omg.org/spec/DD/20100524/DI"],
        ["modeler", "http://camunda.org/schema/modeler/1.0"],
        ["camunda", "http://camunda.org/schema/1.0/bpmn"]
    ]'::jsonb) AS raw_xpath_ns
),
xpath_nss AS (
    SELECT
        array_agg(array[ns.alias, ns.namespace])
    FROM xpath_ns ns
),
stored_dataflow AS (
    SELECT
        id_ AS "id", rev_ AS "revision",
        generated_ AS "is_generated", tenant_id_ AS "tenant_id",
        type_ AS "type", create_time_ AS "created_on", root_proc_inst_id_ AS "root_proc_inst_id",
        removal_time_ AS "deleted_on", name_ AS "name", convert_from(bytes_, 'UTF8') AS "dataflow"
    FROM act_ge_bytearray
),
stored_workflow AS (
    SELECT
        sd.id, sd.name, sd.type, sd.revision, sd.is_generated,
        sd.created_on,  sd.deleted_on,  sd.root_proc_inst_id,
        CAST(sd.dataflow AS xml)  AS "workflow", sd.dataflow,
        (SELECT * FROM xpath_nss) AS "ns"
    FROM stored_dataflow sd
    WHERE xml_is_well_formed_document(sd.dataflow)
    ORDER BY sd.name ASC, sd.created_on DESC
)
SELECT
    sw.name,
    (xpath('count(/bpmn:definitions/bpmn:process)', sw.workflow, sw.ns))[1]::text::int,
    (xpath('/bpmn:definitions/bpmn:process', sw.workflow, sw.ns))[1], dataflow
FROM stored_workflow sw;

References

References