简体   繁体   中英

Remove a string from the Element Name of XML using XSLT?

My XML looks like this.

<?xml version="1.0" encoding="utf-8"?>
 <ns0:Respuesta xmlns:ns0="http://www.siebel.com/xml/SBL_EAI_RC13_SC03_HPFMS_ConsultaTajetadeCreditoenBlackList_29004_Respuesta">
  <ns0:Error>
   <ns0:Codigo>30</ns0:Codigo>
   <ns0:Descripcion>Numero no encontrado en la lista</ns0:Descripcion>
   <ns0:Sistema>adaptadorCFMS</ns0:Sistema>
   <ns0:Tipo>Adaptador</ns0:Tipo>
   <ns0:TimeStamp>2014-06-26T14:40:42</ns0:TimeStamp>
   <ns0:IdHost>medusa10</ns0:IdHost>
  </ns0:Error>
 </ns0:Respuesta>

I need to remove the 'ns0:' part of elements. It doesnt necessarily need to be 'ns0:', it can be 'ns1:' or 'ns2: , hence all those that look like that starting with 'ns' needs to be removed.

The 'ns0:' is also present as a suffix in the namespace xmlns attribute which needs to be removed too.

so the output xml should look like this.

<?xml version="1.0" encoding="utf-8"?>
 <Respuesta xmlns="http://www.siebel.com/xml/SBL_EAI_RC13_SC03_HPFMS_ConsultaTajetadeCreditoenBlackList_29004_Respuesta">
 <Error>
  <Codigo>30</Codigo>
  <Descripcion>Numero no encontrado en la lista</Descripcion>
  <Sistema>adaptadorCFMS</Sistema>
  <Tipo>Adaptador</Tipo>
  <TimeStamp>2014-06-26T14:40:42</TimeStamp>
  <IdHost>medusa10</IdHost>
 </Error>
</Respuesta>

Can you please help me with this? I would really appreciate your help.

Write a template that strips namespace prefixes from element nodes using eg

<xsl:template match="*">
   <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
     <xsl:apply-templates select="@* | node()"/>
   </xsl:element>
</xsl:template>

then add a template copying attributes, comments, text

<xsl:template match="@* | text() | processing-instruction() | comment()">
  <xsl:copy/>
</xsl:template>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM