I have the following xml
declare @Obligaciones xml
set @Obligaciones = '<obligaciones>
<Obligacion id = "51" TipoCancelacionObligacionId = "1" > </Obligacion>
<Obligacion id = "52" TipoCancelacionObligacionId = "2"> </Obligacion>
<Obligacion id = "53" TipoCancelacionObligacionId = "2"> </Obligacion>
</obligaciones>'
I would like to get the following as the result of my query
<obligaciones>
<Obligacion id = "51" TipoCancelacionObligacionId = "1" > </Obligacion>
</obligaciones>
Can sombody help with the query? I been trying for a while without success.
Thanks in advance.
How about this:
SELECT
@Obligaciones.query('/obligaciones/Obligacion[@id="51"]')
FOR XML PATH (''),ROOT('obligaciones')
Gives me the desired output:
<obligaciones>
<Obligacion id="51" TipoCancelacionObligacionId="1" />
</obligaciones>
The @Obligaciones.query()
fetches the XML element with the id=51
attribute, and then I wrap the resulting line of XML into a XML root element called <obligaciones>
.
Update: if you're looking to get the first element regardless of its id
attribute - use this query instead:
SELECT
@Obligaciones.query('/obligaciones/Obligacion[1]')
FOR XML PATH (''),ROOT('obligaciones')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.