LokaBanan
LokaBanan

Reputation: 5

Xquery recursion

I have a question in regards to Xpath, which is something that I am very experienced at. I am doing an assignment where I am supposed to invert all the elements so that the sub elements inside of the Music tag becomes attributes and so that the attributes become sub elements.

Is it possible to do this using Xpath, and how should I got about it? Or is it better to write a DTD of the XML file and write a solution in Xquery?

My XML looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<music>

<song genre="rocknroll" album="1134" artist="249" id="11992"><name>The Sound of Silence</name><nr>1</nr></song>
<song genre="rocknroll" album="4329" artist="932" id="34311"><name>Material Girl</name><nr>2</nr></song>
<song genre="hardrock" album="5293" artist="1167" id="54312"><name>Paradise City</name><nr>1</nr></song>
<song genre="classic" album="7002" artist="156" id="78340"><name>La Primavera</name><nr>1</nr></song>

<artist id="249" isband="yes">Simon and Garfunkel</artist>
<artist id="932" isband="no">Madonna</artist>
<artist id="1167" isband="yes">Guns N'Roses</artist>
<artist id="156" isband="no">Antonio Vivaldi</artist>

<album issued="1964" id="1134" label="Columbia Records">Wednesday Morning, 3 AM</album>
<album issued="1984" id="4392" label="Sire Records">Like A Virgin</album>
<album issued="1988" id="5293" label="Geffen Records">Appetite for Destruction</album>
<album issued="2004" id="7002" label="Naxos Records" performers="Wiener Philharmoniker">Le Quattro Stagioni</album>

</music>

Thanks in advance for any help.

Upvotes: 0

Views: 77

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

XPath can only select nodes in the input tree, or compute new atomic values (like strings and numbers). It cannot construct a new tree. For that you need XSLT or XQuery.

Writing a DTD might be useful for other reasons, but it won't make it any easier to produce the required XSLT or XQuery code for this problem.

Upvotes: 2

Related Questions