Add where condition for check attribute value in XML nodes

Ali Soltani

I have this SQL query:

DECLARE @process TABLE(ID INT IDENTITY, workflowXML XML, Name nvarchar(250),Description nvarchar(MAX));
INSERT INTO @process(workflowXML,Name,Description) VALUES
('<process>
    <Event type="start" id="StartEvent_1">
      <outgoing>SequenceFlow_1uj46ib</outgoing>
    </Event>
    <Task type="service" id="Task_0uurv2v">
      <incoming>SequenceFlow_1uj46ib</incoming>
      <outgoing>SequenceFlow_051szgj</outgoing>
    </Task>
    <Task type="user" id="Task_1yh7nak">
      <incoming>SequenceFlow_051szgj</incoming>
      <TaskUsers>
        <TaskUser RoleName="myFirstRole" />
        <TaskUser RoleName="mySecondRole" />
      </TaskUsers>      
    </Task>
  </process>'
  ,'Process1'
  ,'test Process 1')
,('<process>
    <Event type="start" id="StartEvent_1" name="Start">
      <outgoing>SequenceFlow_0z7u86p</outgoing>
      <outgoing>SequenceFlow_1onkt3z</outgoing>
    </Event>
    <Task type="service" id="Task_0a7vu1x">
      <incoming>SequenceFlow_108ajnm</incoming>
      <incoming>SequenceFlow_1onkt3z</incoming>
      <outgoing>SequenceFlow_01clcmz</outgoing>
    </Task>
    <Task type="user" id="Task_00ijt4n">
      <incoming>SequenceFlow_17q1ecq</incoming>
      <incoming>SequenceFlow_0q9j3et</incoming>
      <outgoing>SequenceFlow_1ygvv8b</outgoing>
      <outgoing>SequenceFlow_02glv1g</outgoing>
    </Task>
    <Task type="service" id="Task_1rnuz4y">
      <incoming>SequenceFlow_1ygvv8b</incoming>
      <incoming>SequenceFlow_0z7u86p</incoming>
      <outgoing>SequenceFlow_108ajnm</outgoing>
      <outgoing>SequenceFlow_17q1ecq</outgoing>
      <outgoing>SequenceFlow_075iuj9</outgoing>
    </Task>                
    <Task type="user" id="Task_1d4ykor">
      <incoming>SequenceFlow_01clcmz</incoming>
      <incoming>SequenceFlow_075iuj9</incoming>
      <incoming>SequenceFlow_1djp3tu</incoming>
      <outgoing>SequenceFlow_0q9j3et</outgoing>
      <TaskUsers>
        <TaskUser RoleName="myFirstRole" />
        <TaskUser RoleName="mySecondRole" />
      </TaskUsers> 
    </Task>        
    <Task type="user" id="Task_1sembw4">
      <incoming>SequenceFlow_02glv1g</incoming>
      <outgoing>SequenceFlow_1djp3tu</outgoing>
      <TaskUsers>
        <TaskUser RoleName="myFirstRole" />
        <TaskUser RoleName="mySecondRole" />
      </TaskUsers> 
    </Task>    
</process>'
  ,'Process2'
  ,'test Process 2')
,('<process>
    <Event type="start" id="StartEvent_0bivq0x">
      <outgoing>SequenceFlow_0q5ik20</outgoing>
      <outgoing>SequenceFlow_147xk2x</outgoing>
    </Event>
    <Task type="service" id="Task_141buye">
      <incoming>SequenceFlow_0q5ik20</incoming>
      <incoming>SequenceFlow_0wg37hn</incoming>
      <outgoing>SequenceFlow_1pvpyhe</outgoing>
      <outgoing>SequenceFlow_10is4pe</outgoing>
    </Task>
    <Task type="service" id="Task_1n3p00i" >
      <incoming>SequenceFlow_147xk2x</incoming>
      <incoming>SequenceFlow_10is4pe</incoming>
      <outgoing>SequenceFlow_18ks1jr</outgoing>
      <outgoing>SequenceFlow_08gxini</outgoing>
    </Task>
    <Task type="user" id="Task_0olxqpp">
      <incoming>SequenceFlow_1pvpyhe</incoming>
      <outgoing>SequenceFlow_03eekq0</outgoing>
    </Task>
    <Task type="user" id="Task_0zjgfkf">
      <incoming>SequenceFlow_18ks1jr</incoming>
      <incoming>SequenceFlow_03eekq0</incoming>
      <outgoing>SequenceFlow_0wg37hn</outgoing>
      <TaskUsers>
            <TaskUser RoleName="mythirdRole" />
      </TaskUsers>
    </Task>
    <Task type="service" id="Task_1q71efy">
      <incoming>SequenceFlow_08gxini</incoming>
    </Task>
</process>'
  ,'Process3'
  ,'test Process 3')
;

WITH DerivedTable AS
(
    SELECT prTbl.ID AS tblID
          ,nd.value('local-name(.)','nvarchar(max)') AS NodeName
          ,nd.value('@type','nvarchar(max)') AS [Type]
          ,nd.value('@id','nvarchar(max)') AS Id
          ,nd.query('.') AS Task
          ,prTbl.Name AS [Name]
          ,prTbl.Description AS [Description]
    FROM @process AS prTbl
    CROSS APPLY prTbl.workflowXML.nodes('process') AS A(pr)
    CROSS APPLY pr.nodes('*') AS B(nd)
)
,AllIncoming AS
(
    SELECT tblId
          ,NodeName 
          ,[Type]
          ,Id 
          ,[Name]
          ,[Description]
          ,i.value('.','nvarchar(max)') AS [Target] 
    FROM DerivedTable
    CROSS APPLY Task.nodes('Task/incoming') AS A(i)

    WHERE NodeName='Task'
)
,recCTE AS
(
    SELECT tblID,NodeName,[Type],Id,[Name],[Description],Task,1 AS Step,' | ' +CAST(Id AS NVARCHAR(MAX)) AS NodePath
    FROM DerivedTable 
    WHERE [Type]='start'

    UNION ALL

    SELECT nxt.tblID,nxt.NodeName,nxt.[Type],nxt.Id,nxt.[Name],nxt.Description,nxt.Task,r.Step+1,r.NodePath + ' | ' + nxt.Id
    FROM recCTE AS r
    INNER JOIN DerivedTable AS nxt ON nxt.Id IN(SELECT x.Id 
                                                FROM AllIncoming AS x 
                                                WHERE x.[Target] IN (SELECT o.value('.','nvarchar(max)')
                                                                     FROM r.Task.nodes('*/outgoing') AS A(o)
                                                                    )
                                                )
    WHERE r.[Type]<>'user' 
      AND r.NodePath NOT LIKE '%| ' + nxt.Id + '%' 
      AND r.Step<=10 
)
select a.tblID as ProcessID,[Name],[Description],a.NodePath,a.Id as TaskID
from
(
    SELECT t.tblID 
      ,t.[Name] 
      ,t.Description
      ,t.NodePath
      ,t.Id
    FROM recCTE AS t
    WHERE t.[Type]='user'
        AND t.Step<=ISNULL((SELECT MIN(x.Step) FROM recCTE AS x WHERE x.tblID=t.tblID AND x.[Type]='user' AND x.NodeName='Task'),10000)  
) a
ORDER BY a.tblID

This returns Task nodes that are first node can be seen from start event (Event type="start"). NodePath explains path from start event to target node. Result of this query is like this:

result1

I need to new query that select from result where RoleName

<TaskUsers>
   <TaskUser RoleName="myFirstRole" />
   <TaskUser RoleName="mySecondRole" />
</TaskUsers> 

exists in output of SplitbyDelimiter function.

SplitbyDelimiter function splits a string by ,. For example:

 select * from SplitbyDelimiter('myFirstRole,mySecondRole',',')

returns

split

I edited DerivedTable part like this:

WITH DerivedTable AS
(
    SELECT prTbl.ID AS tblID
          ,nd.value('local-name(.)','nvarchar(max)') AS NodeName
          ,nd.value('@type','nvarchar(max)') AS [Type]
          ,nd.value('@id','nvarchar(max)') AS Id
          ,nd.query('.') AS Task
          ,prTbl.Name AS [Name]
          ,prTbl.Description AS [Description]
          --,t.c.value('@RoleName','nvarchar(max)') as [Role]
    FROM @process AS prTbl
    CROSS APPLY prTbl.workflowXML.nodes('process') AS A(pr)
    CROSS APPLY pr.nodes('*') AS B(nd)
    CROSS APPLY prTbl.workflowXML.nodes('process/Task/TaskUsers/TaskUser') AS t(c)
    where t.c.value('@RoleName','nvarchar(max)') in (select * from SplitbyDelimiter('myFirstRole,mySecondRole',','))
)

But It is not correct and return 216 records!(reason:Existence extra columns with different values).Also I added inner join result in end of query but this is not working fine.

Update

I used this query:

WITH DerivedTable AS
(
    SELECT prTbl.ID AS tblID
          ,nd.value('local-name(.)','nvarchar(max)') AS NodeName
          ,nd.value('@type','nvarchar(max)') AS [Type]
          ,nd.value('@id','nvarchar(max)') AS Id
          ,nd.query('.') AS Task
          ,prTbl.Name AS [Name]
          ,prTbl.Description AS [Description]
          ,t.c.value('@RoleName','nvarchar(max)') as [Role]
    FROM @process AS prTbl
    CROSS APPLY prTbl.workflowXML.nodes('process') AS A(pr)
    CROSS APPLY pr.nodes('*') AS B(nd)
    CROSS APPLY prTbl.workflowXML.nodes('process/Task/TaskUsers/TaskUser') AS t(c)      
)
,AllIncoming AS
(
    SELECT tblId
          ,NodeName 
          ,[Type]
          ,Id 
          ,[Name]
          ,[Description]
          ,i.value('.','nvarchar(max)') AS [Target] 
    FROM DerivedTable
    CROSS APPLY Task.nodes('Task/incoming') AS A(i)

    WHERE NodeName='Task'
)
,recCTE AS
(
    SELECT tblID,NodeName,[Type],Id,[Name],[Description],[Role],Task,1 AS Step,' | ' +CAST(Id AS NVARCHAR(MAX)) AS NodePath
    FROM DerivedTable 
    WHERE [Type]='start'

    UNION ALL

    SELECT nxt.tblID,nxt.NodeName,nxt.[Type],nxt.Id,nxt.[Name],nxt.Description,nxt.Role,nxt.Task,r.Step+1,r.NodePath + ' | ' + nxt.Id
    FROM recCTE AS r
    INNER JOIN DerivedTable AS nxt ON nxt.Id IN(SELECT x.Id 
                                                FROM AllIncoming AS x 
                                                WHERE x.[Target] IN (SELECT o.value('.','nvarchar(max)')
                                                                     FROM r.Task.nodes('*/outgoing') AS A(o)
                                                                    )
                                                )
    WHERE r.[Type]<>'user' 
      AND r.NodePath NOT LIKE '%| ' + nxt.Id + '%' 
      AND r.Step<=10 
)
select a.tblID as ProcessID,[Name],[Description],a.NodePath,a.Id as TaskID,count(*) as records
from
(
    SELECT t.tblID 
      ,t.[Name] 
      ,t.Description
      ,t.NodePath
      ,t.Id
      ,t.Role     

    FROM recCTE AS t
    WHERE t.[Type]='user'
        AND t.Step<=ISNULL((SELECT MIN(x.Step) FROM recCTE AS x WHERE x.tblID=t.tblID AND x.[Type]='user' AND x.NodeName='Task'),10000)  
) a

INNER JOIN  [dbo].[SplitbyDelimiter]('myFirstRole,mySecondRole',',') r 
    ON r.TheField = a.Role
    group by a.tblID,[Name],[Description],a.NodePath,a.Id
    ORDER BY a.tblID

This returns: result2

This query return too many same records and run time for it is 19s!!. It is not good query.

It would be very helpful if someone could explain solution for this query.

Shnugo

Your query

I used this query:

WITH DerivedTable AS
(
    SELECT prTbl.ID AS tblID
          ,nd.value('local-name(.)','nvarchar(max)') AS NodeName
          ,nd.value('@type','nvarchar(max)') AS [Type]
          ,nd.value('@id','nvarchar(max)') AS Id
          ,nd.query('.') AS Task
          ,prTbl.Name AS [Name]
          ,prTbl.Description AS [Description]
          ,t.c.value('@RoleName','nvarchar(max)') as [Role]
    FROM @process AS prTbl
    CROSS APPLY prTbl.workflowXML.nodes('process') AS A(pr)
    CROSS APPLY pr.nodes('*') AS B(nd)
    CROSS APPLY prTbl.workflowXML.nodes('process/Task/TaskUsers/TaskUser') AS t(c)    

) 

uses the wrong path in the last CROSS APPLY. You'll get all TaksUser elements. Hence the many rows... You might change this to

[...]
CROSS APPLY pr.nodes('*') AS B(nd)
OUTER APPLY nd.nodes('TaskUsers/TaskUser') AS t(c)   

to collect all TaskUser-elements below the current node...

But:

The whole approach to work down your WorkFlow would break, if you add rows to the original resultset!

What you can try:

Add the information as additional column and let the rest untouched. In my query I do this twice (TaskUsers and RoleNames), as I do not really understand, what you want to do with this:

WITH DerivedTable AS
(
    SELECT prTbl.ID AS tblID
          ,nd.value('local-name(.)','nvarchar(max)') AS NodeName
          ,nd.value('@type','nvarchar(max)') AS [Type]
          ,nd.value('@id','nvarchar(max)') AS Id
          ,nd.query('.') AS Task
          ,prTbl.Name AS [Name]
          ,prTbl.Description AS [Description]
          ,nd.query('./TaskUsers/TaskUser') AS TaskUsers
          ,nd.query('./TaskUsers/TaskUser').query('for $rn in /TaskUser return string($rn/@RoleName)').value('.','nvarchar(max)') AS RoleNames
    FROM @process AS prTbl
    CROSS APPLY prTbl.workflowXML.nodes('process') AS A(pr)
    CROSS APPLY pr.nodes('*') AS B(nd)
)
,AllIncoming AS
(
    SELECT tblId
          ,NodeName 
          ,[Type]
          ,Id 
          ,[Name]
          ,[Description]
          ,TaskUsers
          ,RoleNames
          ,i.value('.','nvarchar(max)') AS [Target] 
    FROM DerivedTable
    CROSS APPLY Task.nodes('Task/incoming') AS A(i)

    WHERE NodeName='Task'
)
,recCTE AS
(
    SELECT tblID,NodeName,[Type],Id,[Name],[Description],TaskUsers,RoleNames,Task,1 AS Step,' | ' +CAST(Id AS NVARCHAR(MAX)) AS NodePath
    FROM DerivedTable 
    WHERE [Type]='start'

    UNION ALL

    SELECT nxt.tblID,nxt.NodeName,nxt.[Type],nxt.Id,nxt.[Name],nxt.Description,nxt.TaskUsers,nxt.RoleNames,nxt.Task,r.Step+1,r.NodePath + ' | ' + nxt.Id
    FROM recCTE AS r
    INNER JOIN DerivedTable AS nxt ON nxt.Id IN(SELECT x.Id 
                                                FROM AllIncoming AS x 
                                                WHERE x.[Target] IN (SELECT o.value('.','nvarchar(max)')
                                                                     FROM r.Task.nodes('*/outgoing') AS A(o)
                                                                    )
                                                )
    WHERE r.[Type]<>'user' 
      AND r.NodePath NOT LIKE '%| ' + nxt.Id + '%' 
      AND r.Step<=10 
)
select a.tblID as ProcessID,[Name],[Description],TaskUsers,RoleNames,a.NodePath,a.Id as TaskID
from
(
    SELECT t.tblID 
      ,t.[Name] 
      ,t.Description
      ,t.TaskUsers
      ,t.RoleNames
      ,t.NodePath
      ,t.Id
    FROM recCTE AS t
    WHERE t.[Type]='user'
        AND t.Step<=ISNULL((SELECT MIN(x.Step) FROM recCTE AS x WHERE x.tblID=t.tblID AND x.[Type]='user' AND x.NodeName='Task'),10000)  
) a
ORDER BY a.tblID

The result looks like this

enter image description here

What ever you want to do with this additional information, any filter you want to apply, should be done as the last step...

But - to be honest - you've reached the limits of SQL-Server... This is not the right tool. Sooner or later there will be some more business logic to implement... If this has to be done within SQL Server you might think of a CLR-method...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove XML Nodes for matching attribute condition in SQLServer

How to check condition of xml value?

XML to Linq getting child nodes with attribute value

get child nodes xml attribute value

XML: Select nodes by it's value instead of attribute

powershell xml select attribute value where clause

Print attribute value based on condition attribute in same element (XML Python)

add nodes to XML from second document, based on matching attribute

How to add nodes/elements as attribute to XML node using xslt

XML Searching - which is fast, text within nodes or text as attribute value

Export specific attribute value of all xml nodes to another file

C#: Sort xml nodes and childnodes using attribute value

Select XML element by attribute value and add an element

count nodes with attribute value

PostgreSQL where clause condition check with value that contains single quote

Add additional nodes in XML

Update XML Nodes depending on Attribute

Search and extract XML nodes by an attribute

Check condition in WHERE clause

check or condition in where clause

Check if any XML nodes exists with Specific Attribute using LINQ C#

Jquery XML find where attribute equals a specific value?

Create where condition from XML value with multiple namespaces

How to transverse xml nodes using xslt to match values in form of where condition

Check attribute presence in XML

Bash - Remove XML nodes if the attribute value of a child node does not equal a specific value?

Angular ngIf to add selected attribute to option where value is string

Check if condition is true and if so add value to another column in sql

TypeORM, need to add "WHERE IN (...)" in query condition & only when there is a value for it

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive