Subject:removing unwanted attributes resulting from database conversion to xml Author:david mare Date:20 Feb 2008 08:38 PM
Hi,
I have an XQuery which reads data from a FOXPRO data source. The XQuery is returning some data type attributes in the first occurrence or a row of the output XML. Can I configure my XQuery scenario so it doesn't insert this information? Here's my XQuery:
<dataroot>
{
for $row1 in $table1
return
<MYTABLE>
{ $row1/FIELD1 }
{ $row1/FIELD2 }
{ $row1/FIELD3 }
</MYTABLE>
}
</dataroot>
Here's the resulting XML file:
<dataroot>
<MYTABLE>
<FIELD1 type="C" length="4">content in field1</FIELD1>
<FIELD2 type="D" length="8">content in field2</FIELD2>
<FIELD3 type="C" length="10">content in field3</FIELD3>
</MYTABLE>
<MYTABLE>
<FIELD1>content in field1</FIELD1>
<FIELD2>content in field2</FIELD2>
<FIELD3>content in field3</FIELD3>
</MYTABLE>
<MYTABLE>
<FIELD1>content in field1</FIELD1>
<FIELD2>content in field2</FIELD2>
<FIELD3>content in field3</FIELD3>
</MYTABLE>
</dataroot>
I know I could do a second pass and strip those attributes, but I'd like to avoid that.
Subject:removing unwanted attributes resulting from database conversion to xml Author:david mare Date:20 Feb 2008 09:45 PM
Thanks, and yes you are absolutely right, that will do the trick. However I was hoping not to have to code up all the tag names (this is just a small demo of a much larger more complex file).
Subject:removing unwanted attributes resulting from database conversion to xml Author:Minollo I. Date:20 Feb 2008 10:56 PM
You can clean up attributes using a function like this (remove all attributes named "attr1" and "attr2"):
declare function local:clean-element($el as element()) as element() {
element { $el } {
for $attr in $el/@*
let $attrName := local-name($attr)
return
if ($attrName != "attr1" and $attrName != "attr2") then $attr else ()
}
};
...assuming there are attributes you want to preserve; then you would do...
for $row1 in $table1
return
<MYTABLE>
{ local:clean-element($row1/FIELD1) }
{ local:clean-element($row1/FIELD2) }
{ local:clean-element($row1/FIELD3) }
</MYTABLE>
If you want to remove all attributes, it's of course even easier. And of course you don't need to explicitly list FIELD1, FIELD2, ... if you just want to return all of them (or if you just want to remove a few from a long list); you can use the same approach of local:clean-element().