XML extraction (XQuery) mod. to add the root element and avoid to add namespaces?

With the XML parser Saxon (+tagsoup and with declared default namespace), if I make an extraction with the XPATH "//span" over an html webpage I get this correct result:

<?xml version="1.0" encoding="UTF-8"?>
<span xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">My Stuff</span>
<span xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">Search</span>
<span xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">What's Hot</span>
<span xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">Live Action</span>
<span xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">Community</span>

Open in new window



However, the problem is that I want to further parse this XML with a not tagsoupped parser/visualizer and I can't because this is not valid XML since it it says that it has many (span) root elements.
How can I tell in the XQUERY to add a predefined root element?

Furthermore, to make the XML more compact and easier for human lecture, is it possible to avoid that automatic addition of all those namespaces? (not there in the original html)
lucavillaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
do something like this

return
<spans xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">
{
// your original return goes here
}

</spans>

Open in new window

0
lucavillaAuthor Commented:
Do you mean to modify the XQUERY like this?

declare default element namespace 'http://www.w3.org/1999/xhtml';
declare option saxon:output 'method=xml';
return
<root>
{
doc('FILE:///C:/original.html')//span
}
</root>

Open in new window


Is my syntax correct?
0
Geert BormansInformation ArchitectCommented:
that should work, but you should put the namespace declarations in the root element in order to not have the namespace nodes in every span
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

lucavillaAuthor Commented:
ah now I understood why you put it there again.

I tried with it:
declare default element namespace 'http://www.w3.org/1999/xhtml';
declare option saxon:output 'method=xml';
return
<root xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">
{
doc('FILE:///C:/original.html')//span
}
</root>

Open in new window



however I get this error:
"Error on line 4 column 7 of xquery.xq:
  XPST0003 XQuery syntax error near #...thod=xml'; return <root xmlns=#:
    Unexpected token name "xmlns" beyond end of query
Static error(s) in query"
0
Geert BormansInformation ArchitectCommented:
returning without a let of for is generaly not a good idea

try this

declare default element namespace 'http://www.w3.org/1999/xhtml';
declare option saxon:output 'method=xml';

let $doc := doc('FILE:///C:/original.html')
return
<root xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">
{
    $doc//span
}
</root> 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lucavillaAuthor Commented:
Fabulous! it works!  Thanks Gertone!

Result:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:html="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">
   <span>My Stuff</span>
   <span>Search</span>
   <span>What's Hot</span>
   <span>Live Action</span>
   <span>Community</span>
</root>

Open in new window

0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.