How to correct the XPath Query in OpenXML statement to get the state for each row.

Posted on 2010-08-25
Last Modified: 2012-05-10
I am having trouble pulling the "StateProvCd" value for each RatingClassificationCd in the following script.  Have been playing with the Xpath and it always returns "IN" for both rows instead of "IN" for the first row and "IL" for the second row.

DECLARE @idoc int

EXEC sp_xml_preparedocument @idoc output, '<MainElement>
		<LineBusiness id="12345">
			<RateClass id="abc">
			<RatingClassificationDesc>CLASS TEST 1</RatingClassificationDesc>
			<RateClass id="def">
  			<RatingClassificationDesc>CLASS TEST 2</RatingClassificationDesc>  

SELECT RatingClassificationCd, RatingClassificationDesc, Exposure, StateProvCd
FROM   OPENXML (@idoc, '/MainElement/LineBusiness/RateState/LocInfo/RateClass', 3)
						WITH (StateProvCd				char(2) '../../../RateState/StateProvCd',
							 RatingClassificationCd 		char(5) 'RatingClassificationCd', 
							 RatingClassificationDesc 	varchar(250) 'RatingClassificationDesc',
							 Exposure					numeric(18,0) 'Exposure'							 

EXEC sp_xml_removedocument @idoc

Open in new window

Question by:EIHI-IT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

grepll earned 500 total points
ID: 33521907
This should work:
WITH (StateProvCd char(2) 'ancestor::RateState/StateProvCd',

Open in new window


Author Comment

ID: 33521947
Thank You!

Expert Comment

ID: 33522045

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question