on 12-07-2006 4:45 AM
Hi Friends ,
I need some help in XSLT programming.
My requirement is something like this:
<b>Input XML:</b>
<?xml version="1.0" encoding="UTF-8"?>
<ns0:GovTalkMessage xmlns:ns0="http://www.govtalk.gov.uk/CM/envelope">
<ns0:EnvelopeVersion>2.0</ns0:EnvelopeVersion>
<ns0:Header>
<ns0:MessageDetails>
<ns0:Class>IR-PAYE-EOY</ns0:Class>
<ns0:Transformation>XML</ns0:Transformation>
<ns0:GatewayTest>1</ns0:GatewayTest>
</ns0:MessageDetails>
</ns0:Header>
<ns0:Body>
<ns1:IRenvelope xmlns:ns1="http://www.govtalk.gov.uk/taxation/EOY/06-07/1">
<ns1:IRheader>
<ns1:DefaultCurrency>GBP</ns1:DefaultCurrency>
<ns1:Sender>Company</ns1:Sender>
</ns1:IRheader>
</ns1:IRenvelope>
</ns0:Body>
</ns0:GovTalkMessage>
<b>I want the OUTPUT xml in the following format:</b>
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
<EnvelopeVersion>2.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>IR-PAYE-EOY</Class>
<Qualifier>request</Qualifier>
<Function>submit</Function>
<CorrelationID/>
<Transformation>XML</Transformation>
<GatewayTest>1</GatewayTest>
</MessageDetails>
</Header>
<Body>
<IRenvelope xmlns="http://www.govtalk.gov.uk/taxation/EOY/06-07/1">
<IRheader>
<DefaultCurrency>GBP</DefaultCurrency>
<Sender>Company</Sender>
</IRheader>
</IRenvelope>
</Body>
</GovTalkMessage>
That is basically I need the ns0 and ns1 tags to be removed without affecting the Namespaces
Looking forward for you reply.
Best regards,
Chandan
Message was edited by:
Chandan A C
Try this stylesheet.....and let me know if this is OK
<b><i>
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://www.govtalk.gov.uk/CM/envelope" xmlns:b="http://www.govtalk.gov.uk/taxation/EOY/06-07/1">
<xsl:template match="/">
<GovTalkMessage>
<EnvelopeVersion>
<xsl:value-of select="a:GovTalkMessage/a:EnvelopeVersion"/>
</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>
<xsl:value-of select="a:GovTalkMessage/a:Header/a:MessageDetails/a:Class"/>
</Class>
<Transformation>
<xsl:value-of select="a:GovTalkMessage/a:Header/a:MessageDetails/a:Transformation"/>
</Transformation>
<GatewayTest>
<xsl:value-of select="a:GovTalkMessage/a:Header/a:MessageDetails/a:GatewayTest"/>
</GatewayTest>
</MessageDetails>
</Header>
<Body>
<IRenvelope>
<IRheader>
<DefaultCurrency>
<xsl:value-of select="a:GovTalkMessage/a:Body/b:IRenvelope/b:IRheader/b:DefaultCurrency"/>
</DefaultCurrency>
<Sender>
<xsl:value-of select="a:GovTalkMessage/a:Body/b:IRenvelope/b:IRheader/b:Sender"/>
</Sender>
</IRheader>
</IRenvelope>
</Body>
</GovTalkMessage>
</xsl:template>
</xsl:stylesheet></i></b>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Himadri,
Thanks for the reply, eventho This is removing the tags ns0 and ns1, however the namespace is not coming properly...
the out is coming as: as you can see the namespaces are not coming as i require it .Also the XML structure what i gave you is a sample file and the actual XML file it very very complex.. and hence if i can get some generic XSLT solution it will make my work easier .
Output:
<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns:a="http://www.govtalk.gov.uk/CM/envelope" xmlns:b="http://www.govtalk.gov.uk/taxation/EOY/06-07/1">
<EnvelopeVersion>2.0</EnvelopeVersion>
<Header>
<MessageDetails>
<Class>IR-PAYE-EOY</Class>
<Transformation>XML</Transformation>
<GatewayTest>1</GatewayTest>
</MessageDetails>
</Header>
<Body>
<IRenvelope>
<IRheader>
<DefaultCurrency>GBP</DefaultCurrency>
<Sender>Company</Sender>
</IRheader>
</IRenvelope>
</Body>
</GovTalkMessage>
Hi Chandan,
I am not getting exactly what is the problem now...
If you are bothered by the two refenece namespaces :
<i><?xml version="1.0" encoding="UTF-8"?>
<b><GovTalkMessage xmlns:a="http://www.govtalk.gov.uk/CM/envelope" xmlns:b="http://www.govtalk.gov.uk/taxation/EOY/06-07/1"></b></i><u></u>
It is required as your source message has elements from different namespaces ...
I don't think it will cause any problem in your application ....
-
As for the complexity of the actual message ... you will not get any generic XSLT .. unless your source and target are also generic... you have to work with the XSLT to handle the complexity.
Hope this helps...
Cheers,
Himadri
Hi Himadri,
I have a XSLT Code , which is a generic code which just takes out the namespace from the any XML file... the only problem here is that i am not able to insert a new namspaces the way i want ....would it be possible to improvise this to cater to my situation ?????
<b>XSLT CODE:</b><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
This can surely be done.
You "just" have to include a check whether the current element has a certain name, and handle such cases accordingly.
For the exact syntax, you should ask in an XSLT-centered forum. There, you have the experts on this topic, who can certainly help you.
Kind regards,
Dennis
User | Count |
---|---|
76 | |
9 | |
8 | |
7 | |
6 | |
5 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.