Jump to content

A Newbie needs help in XSLT/XPATH


Recommended Posts

Hi guys!


I'm having a problem that it's driving me crazy. It must be simple for most of you, but for me it has been a nightmare.


Basically, I need to clean up the namespaces in a XML file for a more friendly reading. I am learning to use XSLT for that, but I am not understanding how to apply the correct XPATH sentence in the XSLT.


The original XML file looks like that:

<?xml version="1.0" encoding="utf-16"?><cResultado xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  <Servicos xmlns="http://tempuri.org/">    <cServico>      <Codigo>40010</Codigo>      <PrazoEntrega>1</PrazoEntrega>      <EntregaDomiciliar>S</EntregaDomiciliar>      <EntregaSabado>S</EntregaSabado>      <Erro />      <MsgErro />      <obsFim />    </cServico>    <cServico>      <Codigo>50020</Codigo>      <PrazoEntrega>3</PrazoEntrega>      <EntregaDomiciliar>S</EntregaDomiciliar>      <EntregaSabado>S</EntregaSabado>      <Erro />      <MsgErro>TESTE</MsgErro>      <obsFim />    </cServico>  </Servicos></cResultado>

I would like to transform it on that:

<?xml version="1.0" encoding="utf-16"?><cResultado>  <Servicos>    <cServico>      <Codigo>40010</Codigo>      <PrazoEntrega>1</PrazoEntrega>      <EntregaDomiciliar>S</EntregaDomiciliar>      <EntregaSabado>S</EntregaSabado>      <Erro />      <MsgErro />      <obsFim />    </cServico>    <cServico>      <Codigo>50020</Codigo>      <PrazoEntrega>3</PrazoEntrega>      <EntregaDomiciliar>S</EntregaDomiciliar>      <EntregaSabado>S</EntregaSabado>      <Erro />      <MsgErro>TESTE</MsgErro>      <obsFim />    </cServico>  </Servicos></cResultado>

I am using this XSLT to produce the desired XML file, but It doesn't work:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" ><xsl:output method="xml" indent="yes"/><xsl:template match="/">    	<Servicos>	    <xsl:for-each select="//*[local-name() = 'cServico']">		<cServico>		    <Codigo><xsl:value-of select="Codigo"/></Codigo>		    <PrazoEntrega><xsl:value-of select="PrazoEntrega"/></PrazoEntrega>		    <EntregaDomiciliar><xsl:value-of select="EntregaDomiciliar"/></EntregaDomiciliar>		    <EntregaSabado><xsl:value-of select="EntregaSabado"/></EntregaSabado>		    <Erro><xsl:value-of select="Erro"/></Erro>		    <MsgErro><xsl:value-of select="MsgErro"/></MsgErro>		    <obsFim><xsl:value-of select="obsFim"/></obsFim>		</cServico>	    </xsl:for-each>	</Servicos>    </xsl:template></xsl:stylesheet> 

Probably I am not using the correct XPATH sentence in the XSLT because of the namespaces. Could you help with that?




Euler Almeida



Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...