Querying XML when node names are the same

Hi all

In the below XML I'm trying to extract the /CustomFields/Field/CustomFieldValue value of 'Inbound' WHERE  /CustomFields/Field/CustomFieldName = 'Inbound/Outbound'.  

The problem I'm having is with multiple <field> notes, I'm not handling the 'Get the /CustomFields/Field position where /CustomFields/Field/CustomFieldName = 'Inbound/Outbound', then return the /CustomFields/Field position of the above/CustomFieldValue value.   AND In the below example it would be [3], but it could be anywhere in other rows.

<CustomFields>
  <Field>
    <CustomFieldName>Product</CustomFieldName>
    <CustomFieldValue>Fuzzy Moose Slippers</CustomFieldValue>
  </Field>
  <Field>
    <CustomFieldName>Business Partner</CustomFieldName>
    <CustomFieldValue>Slippers R Us</CustomFieldValue>
  </Field>
  <Field>
    <CustomFieldName>Inbound/Outbound</CustomFieldName>    -- FIND THIS
    <CustomFieldValue>Inbound</CustomFieldValue>           -- GRAB THIS
  </Field>

Open in new window


Right now I have the below query.  The WHERE clause works, but it doesn't store the [?] position where it found it, so the SELECT needs to change so that [1] reflect wherever 'Inbound/Outbound' is.
SELECT TOP 100 
       a.snActivityId, 
       a.DateCreated, 
       a.CustomFields, 
       InboundOutbound = s.CustomFields.value('(/CustomFields/Field/CustomFieldValue)[1]', 'varchar(25)') 
FROM Activity a
WHERE s.CustomFields IS NOT NULL
       AND s.CustomFields.exist('/CustomFields/Field/CustomFieldValue[text() = "Inbound/Outbound"]') = 1

Open in new window


Thanks in advance.
Jim
LVL 67
Jim HornMicrosoft SQL Server Data DudeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Like

 
DECLARE @Xml XML = N'
<CustomFields>
  <Field>
    <CustomFieldName>Product</CustomFieldName>
    <CustomFieldValue>Fuzzy Moose Slippers</CustomFieldValue>
  </Field>
  <Field>
    <CustomFieldName>Business Partner</CustomFieldName>
    <CustomFieldValue>Slippers R Us</CustomFieldValue>
  </Field>
  <Field>
    <CustomFieldName>Inbound/Outbound</CustomFieldName>    -- FIND THIS
    <CustomFieldValue>Inbound</CustomFieldValue>           -- GRAB THIS
  </Field>
</CustomFields>
';

SELECT  Field.value('CustomFieldValue[1]', 'varchar(25)')
FROM    @Xml.nodes('/CustomFields/Field') CF ( Field )
WHERE   Field.exist('CustomFieldName[text() = "Inbound/Outbound"]') = 1;

Open in new window


And you know my mantra? Post a concise and complete example.. ;)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeAuthor Commented:
That'll work.  The below also works that I received from a co-worker, but I didn't want to delete a question I just asked.

Don’t depend on order, use XQuery WHERE syntax to find the value regardless of order.  It will return null if the field isn’t found.  Thanks.

SELECT TOP 10 snContactId, DateUpdated,
       field.value('(Field[CustomFieldName="Inbound/Outbound"]/CustomFieldValue)[1]','varchar(MAX)') value
FROM mkt.snActivity
CROSS APPLY CustomFields.nodes ('/CustomFields') fields(field)
WHERE Activity = 'call'
ORDER BY snActivityId DESC

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.