Search code examples
xqueryxquery-update

Xquery delete type query using lookup list


Need help constructing xquery to delete data from XML File that contains data for Google Products data feed.

I have a long list of ID numbers in a spreadsheet column that need to be removed - these are numeric.

Below is the xml schema. I need to select all the records where <g:id> = "my string of numbers" , then delete them from the file.

Thanks for the help!

<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
 <channel>
   <title></title>
   <link></link>
   <description></description>
  <item> 
    <title></title>
    <link></link>
    <description></description>
    <g:id></g:id>
    <g:condition></g:condition>
    <g:price></g:price>
    <g:availability></g:availability>
    <g:image_link></g:image_link>
    <g:brand></g:brand>
    <g:mpn></g:mpn>
    <g:product_type></g:product_type>
  </item>
 </channel>
</rss>

Solution

  • This XQuery:

    declare namespace g = "http://base.google.com/ns/1.0";
    
    <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
      <channel>
      {
        let $vIds := (1,3)
         return
           /*/*/*[not(g:id = $vIds)]
       }
      </channel>
    </rss>  
    

    when applied on the provided XML document:

      <rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
      <channel>
        <title></title>
        <link></link>
        <description></description>
        <item> 
          <title>Item 1</title>
          <link></link>
          <description></description>
          <g:id>1</g:id>
          <g:condition></g:condition>
          <g:price></g:price>
          <g:availability></g:availability>
          <g:image_link></g:image_link>
          <g:brand></g:brand>
          <g:mpn></g:mpn>
          <g:product_type></g:product_type>
        </item>
        <item> 
          <title>Item 2</title>
          <link></link>
          <description></description>
          <g:id>2</g:id>
          <g:condition></g:condition>
          <g:price></g:price>
          <g:availability></g:availability>
          <g:image_link></g:image_link>
          <g:brand></g:brand>
          <g:mpn></g:mpn>
          <g:product_type></g:product_type>
        </item>
        <item> 
          <title>Item 3</title>
          <link></link>
          <description></description>
          <g:id>3</g:id>
          <g:condition></g:condition>
          <g:price></g:price>
          <g:availability></g:availability>
          <g:image_link></g:image_link>
          <g:brand></g:brand>
          <g:mpn></g:mpn>
          <g:product_type></g:product_type>
        </item>
      </channel>
    </rss>
    

    produces the wanted, correct result:

    <?xml version="1.0" encoding="UTF-8"?>
    <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
       <channel>
          <title/>
          <link/>
          <description/>
          <item> 
             <title>Item 2</title>
             <link/>
             <description/>
             <g:id>2</g:id>
             <g:condition/>
             <g:price/>
             <g:availability/>
             <g:image_link/>
             <g:brand/>
             <g:mpn/>
             <g:product_type/>
          </item>
       </channel>
    </rss>