Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 41795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:14:14+00:00 2026-05-10T15:14:14+00:00

Update: giving a much more thorough example. The first two solutions offered were right

  • 0

Update: giving a much more thorough example.

The first two solutions offered were right along the lines of what I was trying to say not to do. I can’t know location, it needs to be able to look at the whole document tree. So a solution along these lines, with /Books/ specified as the context will not work:

SELECT x.query('.') FROM @xml.nodes('/Books/*[not(@ID) or @ID = 5]') x1(x) 

Original question with better example:

Using SQL Server 2005’s XQuery implementation I need to select all nodes in an XML document, just once each and keeping their original structure, but only if they are missing a particular attribute, or that attribute has a specific value (passed in by parameter). The query also has to work on the whole XML document (descendant-or-self axis) rather than selecting at a predefined depth.

That is to say, each individual node will appear in the resultant document only if it and every one of its ancestors are missing the attribute, or have the attribute with a single specific value.

For example:

If this were the XML:

    DECLARE @Xml XML     SET @Xml =     N' <Library>   <Novels>     <Novel category="1">Novel1</Novel>     <Novel category="2">Novel2</Novel>     <Novel>Novel3</Novel>     <Novel category="4">Novel4</Novel>   </Novels>   <Encyclopedias>     <Encyclopedia>       <Volume>A-F</Volume>       <Volume category="2">G-L</Volume>       <Volume category="3">M-S</Volume>       <Volume category="4">T-Z</Volume>     </Encyclopedia>   </Encyclopedias>   <Dictionaries category="1">     <Dictionary>Webster</Dictionary>     <Dictionary>Oxford</Dictionary>   </Dictionaries> </Library>     ' 

A parameter of 1 for category would result in this:

<Library>   <Novels>     <Novel category="1">Novel1</Novel>     <Novel>Novel3</Novel>   </Novels>   <Encyclopedias>     <Encyclopedia>       <Volume>A-F</Volume>     </Encyclopedia>   </Encyclopedias>   <Dictionaries category="1">     <Dictionary>Webster</Dictionary>     <Dictionary>Oxford</Dictionary>   </Dictionaries> </Library> 

A parameter of 2 for category would result in this:

<Library>   <Novels>     <Novel category="2">Novel2</Novel>     <Novel>Novel3</Novel>   </Novels>   <Encyclopedias>     <Encyclopedia>       <Volume>A-F</Volume>       <Volume category="2">G-L</Volume>     </Encyclopedia>   </Encyclopedias> </Library> 

I know XSLT is perfectly suited for this job, but it’s not an option. We have to accomplish this entirely in SQL Server 2005. Any implementations not using XQuery are fine too, as long as it can be done entirely in T-SQL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:14:15+00:00Added an answer on May 10, 2026 at 3:14 pm

    It’s not clear for me from your example what you’re actually trying to achieve. Do you want to return a new XML with all the nodes stripped out except those that fulfill the condition? If yes, then this looks like the job for an XSLT transform which I don’t think it’s built-in in MSSQL 2005 (can be added as a UDF: http://www.topxml.com/rbnews/SQLXML/re-23872_Performing-XSLT-Transforms-on-XML-Data-Stored-in-SQL-Server-2005.aspx).

    If you just need to return the list of nodes then you can use this expression:

    //Book[not(@ID) or @ID = 5] 

    but I get the impression that it’s not what you need. It would help if you can provide a clearer example.

    Edit: This example is indeed more clear. The best that I could find is this:

    SET @Xml.modify('delete(//*[@category!=1])') SELECT @Xml 

    The idea is to delete from the XML all the nodes that you don’t need, so you remain with the original structure and the needed nodes. I tested with your two examples and it produced the wanted result.

    However modify has some restrictions – it seems you can’t use it in a select statement, it has to modify data in place. If you need to return such data with a select you could use a temporary table in which to copy the original data and then update that table. Something like this:

    INSERT INTO #temp VALUES(@Xml) UPDATE #temp SET data.modify('delete(//*[@category!=2])') 

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What about calling the methods that replace one cell with… May 11, 2026 at 10:37 pm
  • Editorial Team
    Editorial Team added an answer I have a UserControl that just stores the RGB values… May 11, 2026 at 10:37 pm
  • Editorial Team
    Editorial Team added an answer I've seen some jQuery plugins out there that convert <select>'s… May 11, 2026 at 10:36 pm

Related Questions

I have a Silverlight application that communications with an ASP.NET backend through WCF. I
We have been using Scrum for around 9 months and it has largely been
First, some background to my problem. There are many versions of Internet Explorer 6
SQL Server 2005 I have 10 million rows in DB, and run a select

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.