1

I have one XML String as below :

<XML>
   <ProductDetail>
        <Barcode>170604658</Barcode>
       <PropertyDetail>
             <PropertyKey>Size</PropertyKey>
             <PropertyValue>XXL</PropertyValue>
             <PropertyKey>ProdTaxType</PropertyKey>
             <PropertyValue>5%</PropertyValue>
       </PropertyDetail>
   </ProductDetail>
</XML>

i m reading the XML Data via below small query in sp:

 SELECT  *
   FROM OPENXML(@hDoc, 'XML/ProductDetail/PropertyDetail', 2) WITH
   (
            PropertyKey VARCHAR(200) ,
            PropertyValue VARCHAR(200) 
   )XML

BUT , FOR above i m getting one row data as below :

PropertyKey PropertyValue
    Size    XXL

BUT , as per XML Defined i want to get the data as per below :

PropertyKey     PropertyValue
 Size              XXL
 ProdTaxType       5%

FOR the same chile node name i am getting only one record from xml string. So, please suggest in above sp.

3
  • Tag the dbms you're using. (Most products have their own versions of stored procedures.) Commented Aug 4, 2017 at 7:40
  • MS SQL SERVER... Commented Aug 4, 2017 at 7:42
  • Aren't you missing calling EXEC sp_xml_preparedocument @idoc OUTPUT, @doc; in your sample? ` Commented Aug 4, 2017 at 9:05

1 Answer 1

2

Because your XML is not formed well, it should be like this:

DECLARE @hdoc varchar(max) = '<XML>
      <ProductDetail>
        <Barcode>150604658</Barcode>
       <PropertyDetail>
                     <PropertyKey>Size</PropertyKey>
                     <PropertyValue>XXL</PropertyValue>
       </PropertyDetail>
       <PropertyDetail>
                    <PropertyKey>ProdTaxType</PropertyKey>
                    <PropertyValue>5%</PropertyValue>
       </PropertyDetail>
   </ProductDetail>
</XML>'

In your example the PropertyDetail has duplicate attributes.

It could be also re-written like this:

DECLARE @hdoc varchar(max) = '<XML>
      <ProductDetail Barcode="150604658">
       <PropertyDetail PropertyKey="Size" PropertyValue="XXL" />
       <PropertyDetail PropertyKey="ProdTaxType" PropertyValue="5%" />
     </ProductDetail>
</XML>'

Now you would not think of writing:

<PropertyDetail PropertyKey="Size" PropertyValue="XXL" 
                PropertyKey="ProdTaxType" PropertyValue="5%" />

but the meaning is the same as the XML you have - and duplicate attributes are not allowed.

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.