0

I have an xml file and I need to select (not remove) an specific node with its children. I know how to remove one with its children, but not how to remove all but these.

The input xml file looks like this

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:UsernameToken wsu:Id="UsernameToken-8"> <wsse:Username>john</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password> </wsse:UsernameToken> </wsse:Security> </soapenv:Header> <soapenv:Body> <seq:Request xmlns:seq="http://schemas.mycompany.com/schema_v1_0"> <seq:StoreId>6</seq:StoreId> <seq:Concept>2</seq:Concept> <seq:ProductGr> <seq:ProductGroupID>10</seq:ProductGroupID> <seq:Seq>150</seq:Seq> <seq:Update>1</seq:Update> </seq:ProductGr> </seq:Request> </soapenv:Body> </soapenv:Envelope> 

and I want only to obtain the following chunk of code

 <seq:Request xmlns:seq="http://schemas.mycompany.com/schema_v1_0"> <seq:StoreId>6</seq:StoreId> <seq:Concept>2</seq:Concept> <seq:ProductGr> <seq:ProductGroupID>10</seq:ProductGroupID> <seq:Seq>150</seq:Seq> <seq:Update>1</seq:Update> </seq:ProductGr> </seq:Request> 

Can you help me? In addition, the prefix of the namespace can vary, altough not the namespace itself.

Thanks for your help.

1 Answer 1

1

The simplest approach would be something like

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:copy-of select="soap:Envelope/soap:Body/*[1]" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" /> </xsl:template> </xsl:stylesheet> 

which will extract the first child element of the Body, whatever its name.

Sign up to request clarification or add additional context in comments.

5 Comments

It works great, but it adds the namespace xmlns:soapenv to the node selected, like this. <seq:Request xmlns:seq="schemas.mycompany.com/schema_v1_0" xmlns:soapenv="schemas.xmlsoap.org/soap/envelope">
@ingenierocps that is correct, because that namespace declaration is in force at that point in the original tree. It doesn't do any harm to have it declared but not used.
@ingenierocps if you want to get rid of unused namespace declarations for aesthetic reasons you can do it in XSLT 2.0 by adding copy-namespaces="no" to the copy-of, but in XSLT 1.0 you need to do the copying with templates instead of using copy-of
Thank you very much. It has been really helpful. I was afraid because of the namespace. After processing this xml, I send it and it must be validated against an xsd and I thougth the namespace could mess this validation, but not at all. It has worked!!!
I hadn't seen your last comment. Thats the frosting on the cake, it really works, and now with no namespaces. Thks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.