removing an element from xml using saxon xquery

懵懂的女人 提交于 2020-07-22 04:26:09

问题


Hi I have an xml like this:

<ns1:books><ns2:book category="WEB"><ns3:title lang="en">Learning XML</ns3:title>
<ns3:author>Erik T. Ray</ns3:author><ns3:year>2003</ns3:year><ns3:price>39.95</price>

Using Saxon xquery I want to remove the ns3:author element, could anyone let me know how can I do it ?

I know there is a fn:remove(..) method but I don't want to use that as it takes an integer as a 2nd argument

Any complete code will be much appreciated

Also with saxon I am facing the error like

Caused by: net.sf.saxon.trans.StaticError: XQuery syntax error in #...nt-node() external; copy $temp#: Unexpected token "$" beyond end of query

and I used the xml

declare namespace soapenv="http://www.w3.org/2003/05/soap-envelope"; declare namespace reg="http://www.mycompany.com/internal/xsd/registrationservice"; declare namespace reg1="http://www.mycompany.com/internal/xsd/registrationtypes";

declare variable $RegistrationServiceRequest := <reg:RegistrationServiceRequest><reg1:RegistrationAttributes>       <reg1:Username>fdf</reg1:Username><reg1:Password>passwdfdford1</reg1:Password>      <reg1:Title>Mdfdfr</reg1:Title><reg1:FirstName>fdsfdsfsd</reg1:FirstName>
            </reg1:RegistrationAttributes><reg1:AutoLogin> <reg1:AutoLogin1>trtrtrt</reg1:AutoLogin1></reg1:AutoLogin>
        </reg:RegistrationServiceRequest>;
        copy $temp := $RegistrationServiceRequest 
modify delete node $temp/reg1:AutoLogin
return $temp

but when i tried with zorba it worked fine , the only problem is with the saxon and the error is at copy line

Any idea how to fix this ?

Thanks S


回答1:


If you want to make a small change to a document, XQuery is not your best choice. You can do it with XQuery update, or you can do it with XSLT. Doing it with XQuery alone is possible, but very cumbersome.




回答2:


You can use XQuery Update to do this:

copy $n := $data modify delete node $n/ns2:book/ns3:author return $n 

You can try this example live at http://www.zorba-xquery.com/html/demo#JTJCK3mGQJgLav/2uOtgldoTYoM=

Or remove the element by using XQuery Scripting:

delete node $data/ns2:book/ns3:author; 
$data 

You can try this example live at http://www.zorba-xquery.com/html/demo#QEuf9N5OLfD87WarBscHKrRrwm0=



来源:https://stackoverflow.com/questions/12009311/removing-an-element-from-xml-using-saxon-xquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!