ingenierocps
ingenierocps

Reputation: 59

Selecting a node and its children from XML with XSLT

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.

Upvotes: 0

Views: 123

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

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.

Upvotes: 1

Related Questions