Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I required output like below. But When I execute my code, I am not getting the product properly grouped under each parent (It should be like under "US" country code we need to have three products and under "FR" we need to have two products). I have placed code also here and Pls help me ASAP.

XML
<?xml version="1.0" encoding="utf-8"?>
   <ComProducts>
     <Country Code="US">
       <Product>
         <manufacturername>abc</manufacturername>
         <productname>xyz road</productname>
         <upc>RJ</upc>
       </Product>
       <Product>
         <manufacturername>temp</manufacturername>
         <productname>ppp road</productname>
         <upc>RJ</upc>
       </Product>
       <Product>
         <manufacturername>ccc</manufacturername>
         <productname>oli Com</productname>
         <upc>CL</upc>
       </Product>
     </Country>
     <Country Code="FR">
       <Product>
         <manufacturername>xxx</manufacturername>
         <productname>aaa road</productname>
         <upc>NY</upc>
       </Product>
       <Product>
         <manufacturername>eee</manufacturername>
         <productname>olkiu road</productname>
         <upc>CL</upc>
       </Product>
     </Country>
   </ComProducts>


Code :
SQL
DECLARE @Products TABLE   
            (   
               code             VARCHAR(10),   
               manufacturername VARCHAR(50),   
               productname      NVARCHAR(255),   
               upc              VARCHAR(100)  
     
            )   
      
          INSERT INTO @Products   
                select  'en-us', 'abc', 'xyz road', 'RJ' union all
                select  'en-us', 'temp', 'ppp road', 'RJ' union all
                select  'fr-fr', 'xxx', 'aaa road', 'NY' union all
                select  'en-us', 'ccc', 'oli Com', 'CL' union all
                select  'fr-fr', 'eee', 'olkiu road', 'CL' 
    
    
          SELECT 1    AS Tag,   
                 NULL AS Parent,   
                 NULL AS 'ComProducts!1!',   
                 NULL AS 'Country!2!locale',
                 NULL AS 'Products!3!',   
                 NULL AS 'Products!3!manufacturerName!Element',   
                 NULL AS 'Products!3!productName!cdata',   
                 NULL AS 'Products!3!upc!Element'
          UNION ALL   
          SELECT 2 AS Tag,   
                 1 AS Parent,   
                 NULL,   
                 code,
                 NULL,   
                 manufacturername,   
                 productname,   
                 upc
                 FROM   @Products  
          UNION ALL   
          SELECT 3 AS Tag,   
                 2 AS Parent,   
                 NULL,   
                 NULL,
                 NULL,   
                 manufacturername,   
                 productname,   
                 upc  
     
          FROM   @Products 
          FOR    xml explicit
Posted
Comments
Maciej Los 2-Mar-15 9:17am    
ANd the issue is...
md_azy 3-Mar-15 0:38am    
I am not getting xml output like what I mentioned above. It should be like under "US" country code we need to have three products and under "FR" we need to have two products).

1 solution

DECLARE @Products TABLE   
            (   
               code             VARCHAR(10),   
               manufacturername VARCHAR(50),   
               productname      NVARCHAR(255),   
               upc              VARCHAR(100)  
     
            )   
      
          INSERT INTO @Products   
                select  'en-us', 'abc', 'xyz road', 'RJ' union all
                select  'en-us', 'temp', 'ppp road', 'RJ' union all
                select  'fr-fr', 'xxx', 'aaa road', 'NY' union all
                select  'en-us', 'ccc', 'oli Com', 'CL' union all
                select  'fr-fr', 'eee', 'olkiu road', 'CL' 
  
  

SELECT p1.code as '@Code',
    (
     SELECT 1 as Tag,
            0 as Parent, 
            p2.manufacturername as [Product!1!manufacturername!ELEMENT],
            p2.productname as [Product!1!productname!CDATA], 
            p2.upc as [Product!1!upc!ELEMENT] 
     FROM @Products p2
     WHERE p1.code = p2.code
     FOR XML EXPLICIT
    )
FROM @Products p1 
GROUP BY p1.code
FOR XML PATH ('Country'), ROOT ('ComProducts')
 
Share this answer
 
v2
Comments
md_azy 3-Mar-15 0:36am    
Hi John,

Thanks for your response.
I have also done the above code but I need CDATA tag for productname and I am not able to do with this approach. for that reason only I went for XML explict approach.
Do you have any idea can we create the CDATA tag with XML PATH approach .

Thanks,
Mohamed.
John C Rayan 3-Mar-15 3:10am    
check the updated solution and let me know
John C Rayan 4-Mar-15 8:22am    
Did you post any reply? I got notification but can't see your post.
Maciej Los 3-Mar-15 2:23am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900