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

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.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
DECLARE @idoc int

EXEC sp_xml_preparedocument @idoc output, '<MainElement>
		<LineBusiness id="12345">
 		<LOBCd>WORK</LOBCd> 
		<RateState>
 			<StateProvCd>IN</StateProvCd> 
		<LocInfo>
			<RateClass id="abc">
  			<RatingClassificationCd>1234</RatingClassificationCd> 
  			<Exposure>100</Exposure> 
			<RatingClassificationDesc>CLASS TEST 1</RatingClassificationDesc>
  			</RateClass>
  		</LocInfo>
  		</RateState>
		<RateState>
  			<StateProvCd>IL</StateProvCd> 
		<LocInfo>
			<RateClass id="def">
  			<RatingClassificationCd>5678</RatingClassificationCd> 
  			<Exposure>500</Exposure> 
  			<RatingClassificationDesc>CLASS TEST 2</RatingClassificationDesc>  
  			</RateClass>
  		</LocInfo>
  		</RateState>
  		</LineBusiness></MainElement>'

			
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

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

This should work:
1:
WITH (StateProvCd char(2) 'ancestor::RateState/StateProvCd',
Random Solutions  
 
programming4us programming4us