1

How i can parse the following XML to get output like below name = Business, Value = XYZ name = Product, Value = STANDARD name = Trend, Value = Active,Active New,Sparse etc Business,Product,Trend data can be dynamic,some xml do contain, some do not,if it exists then only value should be shown.

Can you please help me with this.

<Filter>
<Expression>
<Expression Name="Business">
<Path>
<RolePathItem>
<RoleID>Gcea34afc</RoleID>
</RolePathItem>
</Path>
<AttributeRef>
<AttributeID>3d0534a20d19</AttributeID>
</AttributeRef>
</Expression>
<Expression>
<Literal>
<DataType>String</DataType>
<Value>XYZ</Value>
</Literal>
</Expression>
</Expression>




<Expression>
<Function>
<FunctionName>Equals</FunctionName>
<Arguments>
<Expression Name="Product">
<Path>
<RolePathItem>
<RoleID>6a99c8cd92fc</RoleID>
</RolePathItem>
<RolePathItem>
<RoleID>011e01b51ba0</RoleID>
</RolePathItem>
</Path>
</Expression>
<Expression>
<Literal>
<DataType>String</DataType>
<Value>STANDARD</Value>
</Literal>
</Expression>
</Arguments>
</Function>
</Expression>



<Expression>
<Function>
<FunctionName>In</FunctionName>
<Arguments>
<Expression Name="Trend">
<Path>
<RolePathItem>
<RoleID>6a99c8cd92fc</RoleID>
</RolePathItem>
<RolePathItem>
<RoleID>dad362a5a954</RoleID>
</RolePathItem>
</Path>
</Expression>
<Expression>
<Literal>
<DataType>String</DataType>
<Values>
<Value>Active</Value>
<Value>Active New</Value>
<Value>New</Value>
<Value>Sparse</Value>
</Values>
</Literal>
</Expression>
</Arguments>
</Function>
</Expression>
</Filter>
2
  • Xml string has errors. For example first <RoleID> not have close tag. Could you correct? Commented Sep 4, 2017 at 18:47
  • Sorry for that, I have updated the XML. Commented Sep 5, 2017 at 6:39

1 Answer 1

2

Because of dynamic content, I had to use some string functions, but it looks working.

DECLARE @xmlData XML = '
<Filter>
    <Expression>
        <Expression Name="Business">
            <Path>
                <RolePathItem>
                    <RoleID>Gcea34afc</RoleID>
                </RolePathItem>
            </Path>
            <AttributeRef>
                <AttributeID>3d0534a20d19</AttributeID>
                </AttributeRef>
        </Expression>
        <Expression>
            <Literal>
                <DataType>String</DataType>
                <Value>XYZ</Value>
            </Literal>
        </Expression>
    </Expression>
    <Expression>
        <Function>
            <FunctionName>Equals</FunctionName>
            <Arguments>
                <Expression Name="Product">
                    <Path>
                        <RolePathItem>
                            <RoleID>6a99c8cd92fc</RoleID>
                        </RolePathItem>
                        <RolePathItem>
                            <RoleID>011e01b51ba0</RoleID>
                        </RolePathItem>
                    </Path>
                </Expression>
                <Expression>
                    <Literal>
                        <DataType>String</DataType>
                        <Value>STANDARD</Value>
                    </Literal>
                </Expression>
            </Arguments>
        </Function>
    </Expression>
    <Expression>
        <Function>
            <FunctionName>In</FunctionName>
            <Arguments>
                <Expression Name="Trend">
                    <Path>
                        <RolePathItem>
                            <RoleID>6a99c8cd92fc</RoleID>
                        </RolePathItem>
                        <RolePathItem>
                            <RoleID>dad362a5a954</RoleID>
                        </RolePathItem>
                    </Path>
                </Expression>
                <Expression>
                    <Literal>
                        <DataType>String</DataType>
                        <Values>
                            <Value>Active</Value>
                            <Value>Active New</Value>
                            <Value>New</Value>
                            <Value>Sparse</Value>
                        </Values>
                    </Literal>
                </Expression>
            </Arguments>
        </Function>
    </Expression>
</Filter>'

;WITH XmlRows AS (
    SELECT 
        CONVERT(VARCHAR(MAX),ref.query('*')) XMLString
    FROM @xmlData.nodes('/Filter/Expression') x ( ref )
)
,NameAndValueXml AS 
    ( SELECT CONVERT(XML, 
                SUBSTRING(XMLString, 
                    CHARINDEX('<Expression Name=',XMLString),
                    ( CHARINDEX('</Expression>',XMLString) - CHARINDEX('<Expression Name=',XMLString) + LEN('</Expression>') )  ) 
                ) AS NameXml 
            ,CONVERT(XML, 
                SUBSTRING(XMLString, 
                    CHARINDEX('<Literal>',XMLString),
                    ( CHARINDEX('</Literal>',XMLString) - CHARINDEX('<Literal>',XMLString) + LEN('</Literal>') )  ) 
                ) 
                AS ValueXml 
            FROM XmlRows
)
SELECT 
    NameXml.value('(./Expression/@Name)[1]', 'nvarchar(255)') Name, 
    CASE 
        WHEN ValueXml.exist('(/Literal/Values/*)') = 1 THEN 
            REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(max),ValueXml.query('(/Literal/Values/*)')),'</Value><Value>' , ','),'</Value>',''),'<Value>','')
        ELSE 
            ValueXml.value('(./Literal/Value)[1]', 'nvarchar(max)') 
        END AS Value
FROM NameAndValueXml

result:

Name        Value
--------    ----------------------------
Business    XYZ
Product     STANDARD
Trend       Active,Active New,New,Sparse
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.