Change namespaces using XSLT - xslt

I'm writing a wrapper layer over an existing out of the box service. For this, I need to convert the namespaces of the xml that is returned by the service to my namespaces
The xml has different namespaces for different tags, and I need to map them to another set of namespaces
In other words, the prefix
ns1 to be modified to myns1
ns2 to myns2, and so on...
Here is how my XML looks like
<ns1:Response>
<ns2:task>..</ns2:task>
<ns2:address>..</ns2:address>
<ns2:pin>..</ns2:pin>
<ns3:address>
<ns4:add1>..</ns4:add1>
<ns4:add2>..</ns4:add2>
<ns4:add3>
<ns5:asdf>..</ns5:asdf>
<ns5:qwe>..</ns5:qwe>
</ns4:add3>
<ns4:add4>..</ns4:add4>
</ns3:address>
<ns2:query>..</ns2:query>
</ns1:Response>
And I want to change this to
<myns1:Response>
<myns2:task>..</myns2:task>
<myns2:address>..</myns2:address>
<myns2:pin>..</myns2:pin>
<myns3:address>
<myns4:add1>..</myns4:add1>
<myns4:add2>..</myns4:add2>
<myns4:add3>
<myns5:asdf>..</myns5:asdf>
<myns5:qwe>..</myns5:qwe>
</myns4:add3>
<myns4:add4>..</myns4:add4>
</myns3:address>
<myns2:query>..</myns2:query>
</myns1:Response>
Basically, just the namespace prefixes change from one set to another.
Can you please suggest me how to do this in xslt?
I've tried using apply-templates, but i'm not able to figure out a solution
Regards
Ravi

The following stylesheet worked!
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns1="http://www.w3.org/TR/html4/1"
xmlns:myns2="http://www.w3.org/TR/html4/2"
xmlns:myns3="http://www.w3.org/TR/html4/3"
xmlns:myns4="http://www.w3.org/TR/html4/4"
xmlns:myns5="http://www.w3schools.com/furniture"
xmlns:ns1="http://www.w3.org/TR/html4/1"
xmlns:ns2="http://www.w3.org/TR/html4/2"
xmlns:ns3="http://www.w3.org/TR/html4/3"
xmlns:ns4="http://www.w3.org/TR/html4/4"
xmlns:ns5="http://www.w3schools.com/furniture">
<xsl:template match="*">
<myns1:taskListResponse>
<xsl:apply-templates/>
</myns1:taskListResponse>
</xsl:template>
<xsl:template match="ns2:*">
<xsl:element name="myns2:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns3:*">
<xsl:element name="myns3:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns4:*">
<xsl:element name="myns4:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="ns5:*">
<xsl:element name="myns5:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
But seriously, I'm not able to understand how this works.
The snippet
<xsl:template match="ns2:*">
<xsl:element name="myns2:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
means match all elements that start with ns2: and replace with myns2:
But when I used the same logic for all, it gave only the first 2 level elements
Ex :
Stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myns1="http://www.w3.org/TR/html4/1"
xmlns:myns2="http://www.w3.org/TR/html4/2"
xmlns:myns3="http://www.w3.org/TR/html4/3"
xmlns:myns4="http://www.w3.org/TR/html4/4"
xmlns:myns5="http://www.w3schools.com/furniture"
xmlns:ns1="http://www.w3.org/TR/html4/1"
xmlns:ns2="http://www.w3.org/TR/html4/2"
xmlns:ns3="http://www.w3.org/TR/html4/3"
xmlns:ns4="http://www.w3.org/TR/html4/4"
xmlns:ns5="http://www.w3schools.com/furniture">
<xsl:template match="/">
<myns1:taskListResponse>
<xsl:apply-templates/>
</myns1:taskListResponse>
</xsl:template>
<xsl:template match="ns2:*">
<xsl:element name="myns2:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="ns3:*">
<xsl:element name="myns3:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="ns4:*">
<xsl:element name="myns4:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="ns5:*">
<xsl:element name="myns5:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
returned
<?xml version="1.0" encoding="UTF-8"?><myns1:taskListResponse xmlns:ns2="http://www.w3.org/TR/html4/2" xmlns:ns3="http://www.w3.org/TR/html4/3" xmlns:ns4="http://www.w3.org/TR/html4/4" xmlns:ns5="http://www.w3schools.com/furniture" xmlns:myns1="http://www.w3.org/TR/html4/1" xmlns:myns2="http://www.w3.org/TR/html4/2" xmlns:myns3="http://www.w3.org/TR/html4/3" xmlns:myns4="http://www.w3.org/TR/html4/4" xmlns:myns5="http://www.w3schools.com/furniture" xmlns:ns1="http://www.w3.org/TR/html4/1">
<myns2:task>..</myns2:task>
<myns2:address>..</myns2:address>
<myns2:pin>..</myns2:pin>
<myns3:address>
..
..
..
..
..
</myns3:address>
<myns2:query>..</myns2:query>
</myns1:taskListResponse>
Here, you can see that ns4 & ns5 elements are completely missed. Where for the same template, instead of <xsl:value-of select="." />, If i use <xsl:apply-templates/>, it worked, meaning replaced all the level nodes. I seriously dont understand this as the match I used is * in the template.
Appreciate your response.
Regards
Ravi

Related

Split large xslt into smaller ones

I have a large xslt file that is giving problems during deployment
com.sun.org.apache.bcel.internal.generic.ClassGenException: Branch target offset too large for short
at com.sun.org.apache.bcel.internal.generic.BranchInstruction.dump(BranchInstruction.java:99)
at com.sun.org.apache.bcel.internal.generic.InstructionList.getByteCode(InstructionList.java:980)
at com.sun.org.apache.bcel.internal.generic.MethodGen.getMethod(MethodGen.java:616)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Mode.compileNamedTemplate(Mode.java:556)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Mode.compileTemplates(Mode.java:566)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Mode.compileApplyTemplates(Mode.java:818)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.compileModes(Stylesheet.java:615)
at com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet.translate(Stylesheet.java:730)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:370)
at com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC.compile(XSLTC.java:445)
For this, I need to split this large xslt into smaller ones.
I've seen xsl:include tag, but seems like this works for seperate templates.
In my case, its a single parent tag with multiple assignments like this
<xsl:template match="/">
<ns5:taskListResponse>
<xsl:for-each select="/tns:taskListResponse/task:task">
<ns7:task>
<xsl:if test="task:title">
<ns7:title>
<xsl:value-of select="task:title"/>
</ns7:title>
</xsl:if>
<xsl:if test="task:taskDefinitionURI">
<ns7:taskDefinitionURI>
<xsl:value-of select="task:taskDefinitionURI"/>
</ns7:taskDefinitionURI>
</xsl:if>
<xsl:if test="task:creator">
<ns7:creator>
<xsl:value-of select="task:creator"/>
</ns7:creator>
</xsl:if>
........100 more tags like this.....
...................
</xsl:for-each>
</ns5:taskListResponse>
How can I split this xsl?
I want to put some tags in another file and include those inside the
Appreciate your help
Regards
Ravi
I would consider splitting this up into separate templates, for example each of the if tests could be replaced by apply-templates, and the following template to do the work:
<xsl:template match="task:*">
<xsl:element name="ns7:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
If you don't need to re-order the children then the entire stylesheet boils down to
<xsl:template match="/">
<ns5:taskListResponse>
<xsl:apply-templates select="/tns:taskListResponse/task:task" />
</ns5:taskListResponse>
</xsl:template>
<xsl:template match="task:task">
<ns7:task><xsl:apply-templates select="*" /></ns7:task>
</xsl:template>
<xsl:template match="task:*">
<xsl:element name="ns7:{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
It gets slightly more complex if you do need to re-order things, then you'll need 100 separate <xsl:apply-templates select="task:foo" /> in place of the <xsl:apply-templates select="*" />, but it's still smaller and more modular.

Prevent <cite> tag appearing within RSS Feed

Im using doctype: XHTML Mobile Profile 1.2, XML version="1.0 and Content-Type "application/xhtml+xml"
Is it possible to disable or prevent <cite> tag appearing within RSS feed, Due to the fact that I keep getting this error on the Page itself.
error on line 24 at column 70: expected '>'
Below is a rendering of the page up to the first error.
I am using an external feed from another site, which is not mine to control or edit.
I am using an XSLT and ColdFusion file to read the external RSS file, and display it the way I want within my XSLT, I already have in place disable-output-escaping="yes" to prevent lose code showing up within the feed. My XSLT works when this tag is not there
I have tried to get around it, but no luck. Is it actually possible to do this?
CFM
<cfcontent type="application/xhtml+xml" reset="yes">
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">
<cfhttp method="Get" url="http://www.animenewsnetwork.com/news/rss.xml">
<cfset xmlInput = CFHTTP.FileContent>
<cfset MyXslFile = Expandpath("animenewsrss.xsl")>
<cffile action="READ" variable="xslInput" file="#MyXslFile#">
<cfset xmlOutput = XMLTransform(xmlInput, xslInput)>
<cfoutput>#xmloutput#</cfoutput>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="rss/channel">
<xsl:element name="html">
<xsl:element name="head">
<xsl:element name="title">Anime News</xsl:element>
</xsl:element>
<xsl:element name="body">
<xsl:element name="div"><xsl:attribute name="id"><xsl:value-of select="'hstyle'"/></xsl:attribute>Media Events UK - Anime News</xsl:element>
<xsl:element name="div"><xsl:attribute name="id"><xsl:value-of select="'nstyle'"/>
</xsl:attribute><xsl:element name="a"><xsl:attribute name="href">index.cfm</xsl:attribute>Home</xsl:element> -
<xsl:element name="a"><xsl:attribute name="href">listings.cfm</xsl:attribute>Listings</xsl:element> -
<xsl:element name="a"><xsl:attribute name="href">venue.cfm</xsl:attribute>Venues</xsl:element>
</xsl:element>
<xsl:apply-templates select="item[position() < 6]" />
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="item[position() < 6]">
<div class="rsstyle">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="link"/>
</xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
<xsl:element name="div">
<xsl:value-of select="pubDate" />
</xsl:element>
<xsl:element name="div">
<xsl:value-of select="concat(substring(description, 1, 50), '...')" disable-output-escaping="yes"/>
</xsl:element>
</div>
</xsl:template>
</xsl:stylesheet>
With this line
<xsl:value-of select="concat(substring(description, 1, 50), '...')" disable-output-escaping="yes"/>
you cut the content of <description>, which contains cite elements in some cases. This leads to lines like the following in your result HTML:
<div><cite>Ni No Kuni</cite>, <cite>Tales of Xillia</ci...</div>
As you can see, the cite element is not closed properly anymore, because you cut the content of the description element at 50 characters. If you counted the characters, you'd notice that the content of description stops at 50, then "..." is inserted.
If you describe your intent behind applying substring to decription elements, SO can help you find a good alternative to this.
My guess is that you need to take into account the possibility that description contains not only text, but also elements (like cite). Then, it makes sense to use substring only on the text content of description, like this:
concat(substring(description/text(),1,50),'...')
Then go on to catch the child elements of description, e.g. in a separate template:
<xsl:template match="cite[parent::description]">
<!--Deal with cite elements-->
</xsl:template>
EDIT: I adapted your stylesheet to process cite elements as children of description. There are 2 additional templates that process text nodes and cite nodes, both being children of description.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />
<xsl:template match="rss/channel">
<!--I left this template unchanged!-->
</xsl:template>
<xsl:template match="item[position() < 6]">
<div class="rsstyle">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="link"/>
</xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
<xsl:element name="div">
<xsl:value-of select="pubDate" />
</xsl:element>
<xsl:element name="div">
<xsl:apply-templates select="description"/>
</xsl:element>
</div>
</xsl:template>
<xsl:template match="description">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[parent::description]">
<xsl:copy/>
</xsl:template>
<xsl:template match="cite[parent::description]">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>

XSL Copy nodes without xmlns

I have a specific problem. I have to transform an XML structure to other, where the base XSD is same, different only the namespace definition. The first part is simple, because here I have to use field-mapping. The second part is the simple copy. And here is the problem. The copied "main" node contains the original xmlns attribute. I need to remove this "attribute".
The base xml:
<?xml version="1.0" encoding="UTF-8"?>
<S2SCTIcf:SCTIcfBlkCredTrf xmlns:S2SCTIcf="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf SCTIcfBlkCredTrf.xsd">
<S2SCTIcf:SndgInst>XXXXXXX0</S2SCTIcf:SndgInst>
<S2SCTIcf:RcvgInst>YYYYYYY0</S2SCTIcf:RcvgInst>
<S2SCTIcf:FileRef>2013111200800546</S2SCTIcf:FileRef>
<S2SCTIcf:SrvcId>SCT</S2SCTIcf:SrvcId>
<S2SCTIcf:TstCode>T</S2SCTIcf:TstCode>
<S2SCTIcf:FType>ICF</S2SCTIcf:FType>
<S2SCTIcf:FDtTm>2013-11-12T16:26:31</S2SCTIcf:FDtTm>
<S2SCTIcf:NumCTBlk>1</S2SCTIcf:NumCTBlk>
<S2SCTIcf:NumPCRBlk>0</S2SCTIcf:NumPCRBlk>
<S2SCTIcf:NumRFRBlk>0</S2SCTIcf:NumRFRBlk>
<S2SCTIcf:NumROIBlk>0</S2SCTIcf:NumROIBlk>
<S2SCTIcf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
<GrpHdr>
<MsgId>xxddccxxaaa</MsgId>
<CreDtTm>2013-11-12T16:26:31</CreDtTm>
<NbOfTxs>1</NbOfTxs>
<TtlIntrBkSttlmAmt Ccy="EUR">469.12</TtlIntrBkSttlmAmt>
<IntrBkSttlmDt>2013-11-13</IntrBkSttlmDt>
<SttlmInf>
<SttlmMtd>CLRG</SttlmMtd>
<ClrSys>
<Prtry>ST2</Prtry>
</ClrSys>
</SttlmInf>
<InstgAgt>
<FinInstnId>
<BIC>XXXXXXX0</BIC>
</FinInstnId>
</InstgAgt>
</GrpHdr>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>1114405599,1114382976</EndToEndId>
<TxId>F3232323232</TxId>
</PmtId>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<IntrBkSttlmAmt Ccy="EUR">469.12</IntrBkSttlmAmt>
<ChrgBr>SLEV</ChrgBr>
<Dbtr>
<Nm>ddffrrddsaasas</Nm>
<PstlAdr>
<Ctry>HU</Ctry>
<AdrLine>dssdsdsdsdsdaas</AdrLine>
</PstlAdr>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>HU26XXXXXXXXXXXXXX</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>CCCCHUH0</BIC>
</FinInstnId>
</DbtrAgt>
<CdtrAgt>
<FinInstnId>
<BIC>CVCVCVCVCVC</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>XXXXX</Nm>
<PstlAdr>
<Ctry>DE</Ctry>
</PstlAdr>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>DE12vvvvvvvhghhg</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>0000000000,0000000000 </Ustrd>
</RmtInf>
</CdtTrfTxInf>
</S2SCTIcf:FIToFICstmrCdtTrf>
</S2SCTIcf:SCTIcfBlkCredTrf>
The xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns0="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf"
xmlns:sw8="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"
xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" exclude-result-prefixes="xs ns0 ">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="var1_SCTIcfBlkCredTrf" select="ns0:SCTIcfBlkCredTrf"/>
<S2SCTScf:SCTScfBlkCredTrf
xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf">
<xsl:for-each select="$var1_SCTIcfBlkCredTrf">
<S2SCTScf:SndgInst>
<xsl:value-of select="string(ns0:RcvgInst)"/>
</S2SCTScf:SndgInst>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf">
<S2SCTScf:RcvgInst>
<xsl:value-of select="string(ns0:SndgInst)"/>
</S2SCTScf:RcvgInst>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf">
<S2SCTScf:SrvcId>
<xsl:value-of select="string(ns0:SrvcId)"/>
</S2SCTScf:SrvcId>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf">
<S2SCTScf:TstCode>
<xsl:value-of select="string(ns0:TstCode)"/>
</S2SCTScf:TstCode>
</xsl:for-each>
<S2SCTScf:FType>SCF</S2SCTScf:FType>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf">
<S2SCTScf:FileRef>
<xsl:value-of select="string(ns0:FileRef)"/>
</S2SCTScf:FileRef>
</xsl:for-each>
<S2SCTScf:RoutingInd>DIR</S2SCTScf:RoutingInd>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf">
<S2SCTScf:FileBusDt>
<xsl:value-of select="string(ns0:FDtTm)"/>
</S2SCTScf:FileBusDt>
</xsl:for-each>
<S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<GrpHdr>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<MsgId>
<xsl:value-of select="string(sw8:GrpHdr/sw8:MsgId)"/>
</MsgId>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<CreDtTm>
<xsl:value-of select="string(sw8:GrpHdr/sw8:CreDtTm)"/>
</CreDtTm>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<NbOfTxs>
<xsl:value-of select="string(sw8:GrpHdr/sw8:NbOfTxs)"/>
</NbOfTxs>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<xsl:variable name="var2_TtlIntrBkSttlmAmt"
select="sw8:GrpHdr/sw8:TtlIntrBkSttlmAmt"/>
<TtlIntrBkSttlmAmt>
<xsl:attribute name="Ccy" namespace="">
<xsl:value-of select="string($var2_TtlIntrBkSttlmAmt/#Ccy)"/>
</xsl:attribute>
<xsl:value-of
select="string(number(string($var2_TtlIntrBkSttlmAmt)))"/>
</TtlIntrBkSttlmAmt>
</xsl:for-each>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<IntrBkSttlmDt>
<xsl:value-of select="string(sw8:GrpHdr/sw8:IntrBkSttlmDt)"/>
</IntrBkSttlmDt>
</xsl:for-each>
<SttlmInf>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<SttlmMtd>
<xsl:value-of
select="string(sw8:GrpHdr/sw8:SttlmInf/sw8:SttlmMtd)"/>
</SttlmMtd>
</xsl:for-each>
<ClrSys>
<xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
<Prtry>
<xsl:value-of
select="string(sw8:GrpHdr/sw8:SttlmInf/sw8:ClrSys/sw8:Prtry)"
/>
</Prtry>
</xsl:for-each>
</ClrSys>
</SttlmInf>
<InstdAgt>
<FinInstnId>
<xsl:for-each
select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:GrpHdr/sw8:InstgAgt">
<BIC>
<xsl:value-of select="string(sw8:FinInstnId/sw8:BIC)"/>
</BIC>
</xsl:for-each>
</FinInstnId>
</InstdAgt>
</GrpHdr>
<xsl:copy-of select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" />
</xsl:for-each>
</S2SCTScf:FIToFICstmrCdtTrf>
</S2SCTScf:SCTScfBlkCredTrf>
</xsl:template>
</xsl:stylesheet>
The wrong part of output:
...
</InstdAgt>
</GrpHdr>
<CdtTrfTxInf xmlns:S2SCTIcf="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf">
<PmtId>
<EndToEndId>1114405599,1114382976</EndToEndId>
<TxId>F3232323232</TxId>
...
I don't want to get the xmlns:S2SCTIcf="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf" attribute this line.
Have someone any idea?
Thank you!
Feri
Your issue is that
<xsl:copy-of select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" />
copies the node from the original tree including its "namespace nodes", i.e. the namespace declarations that were in scope at that point in the original document. When this node is serialized any of these namespace nodes that are not already in force at this point in the output document will be declared by the serializer.
If you were able to use XSLT 2.0 then you could try setting copy-namespaces="no" on the copy-of but that isn't an option in XSLT 1.0. So instead of using copy-of you need to use templates to copy that node (and all its descendants recursively) without including the namespace nodes. The simplest way I can think of to do this is to declare two additional templates
<xsl:template match="*" mode="copy">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="#*|node()" mode="copy" />
</xsl:element>
</xsl:template>
<xsl:template match="#*|text()|comment()" mode="copy">
<xsl:copy/>
</xsl:template>
and then replace that copy-of with
<xsl:apply-templates mode="copy"
select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" />
The trick here is that xsl:element is creating a new element node that happens to have the same name and namespace as the original one, rather than copying the original node, so it doesn't copy the namespace nodes.
You can use a variant of the answer here to get what you want.
Basically, you would create a template to rebuild that element without any namespaces. So you would add the following two templates to your current XSLT:
<xsl:template match="*" mode="copy-no-namespaces">
<xsl:element name="{local-name()}">
<xsl:copy-of select="#*"/>
<xsl:apply-templates select="node()" mode="copy-no-namespaces"/>
</xsl:element>
</xsl:template>
<xsl:template match="comment()| processing-instruction()" mode="copy-no-namespaces">
<xsl:copy/>
</xsl:template>
And then update your copy-of to
<xsl:apply-templates select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" mode="copy-no-namespaces"/>

Use variable to store and output attributes

If have a big 'xsl:choose' chunk in which I need to set a number of defined sets of attributes on different elements.
I really do not like to repeat the definition of sets of attributes inside every branch of the 'choose'.
So I would like to work with a variable that contains those attributes.
A lot easier to maintain and less room for error...
So far I have not been able to call the attribute node out?
I thought they are just a node-set, so copy-of would do the trick.
But that gives me nothing on output.
Is this because attribute nodes are not really children?
But XSLT 1.O does not allow me to address them directly...<xsl:copy-of select="$attributes_body/#*/> returns an error
Here is the stylesheet fragment (reduced from original)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="list">
<xsl:for-each select="figure">
<xsl:variable name="attributes_body">
<xsl:attribute name="chapter"><xsl:value-of select="#chapter"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="#id"/></xsl:attribute>
</xsl:variable>
<xsl:variable name="attributes_other">
<xsl:attribute name="chapter"><xsl:value-of select="#book"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="#id"/></xsl:attribute>
</xsl:variable>
<xsl:choose>
<xsl:when test="ancestor::body">
<xsl:element name="entry">
<xsl:copy-of select="$attributes_body"/>
<xsl:text>Body fig</xsl:text>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="entry">
<xsl:copy-of select="$attributes_other"/>
<xsl:text>other fig</xsl:text>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
If this can not be done in XLST 1.0 would 2.0 be able to do this?
<xsl:variable name="attributes_body">
<xsl:attribute name="chapter"><xsl:value-of select="#chapter"/></xsl:attribute>
<xsl:attribute name="id"><xsl:value-of select="#id"/></xsl:attribute>
</xsl:variable>
You need to select the wanted attributes -- not to copy their contents in the body of the variable.
Remember: Whenever possible, try always to specify an XPath expression in the select attribute of xsl:variable -- avoid copying content in its body.
Solution:
Just use:
<xsl:variable name="attributes_body" select="#chapter | #id">
Here is a complete example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="x/a">
<xsl:variable name="vAttribs" select="#m | #n"/>
<newEntry>
<xsl:copy-of select="$vAttribs"/>
<xsl:value-of select="."/>
</newEntry>
</xsl:template>
</xsl:stylesheet>
when applied on this:
<x>
<a m="1" n="2" p="3">zzz</a>
</x>
produces:
<newEntry m="1" n="2">zzz</newEntry>
in my case, I was trying to store a tag attribute into a variable
to do so, use this syntax tag-name/#attribute-inside-tag-name
here is an example
<xsl:variable name="articleLanguage" select="/article/#language"/><!--the tricky part -->
//<!--now you can use this this varialbe as you like -->
<xsl:apply-templates select="front/article-meta/kwd-group[#language=$articleLanguage]"/>
and the xml was
<article article-type="research-article" language="es" explicit-lang="es" dtd-version="1.0">
.....
hope this help you

Changing a namespace value in an XSL transformation?

I'm not sure if that is possible, as I'm very new to XSLT and stuff, but maybe some of you could help me here? It's a bit tricky and I haven't found anything like it on the internet:
The problem is that I have an input xml with namespaces declared and all and I only need to make slight changes to it (adding or deleting attributes, or shifting them to other locations). But at the same time, I have to update the namespace references in the document's document tag. So, for example, the input xml might look something like this:
<order
xmlns="some.url.01"
xmlns:ns2="some.other.url"
xmlns:ns3="another.one"
>
<orderEntry>
<orderControl>
<mandant>test</mandant>
<businessUnit>test</businessUnit>
<inboundChannel>test</inboundChannel>
<timestamp>timestamp</timestamp>
<requestedDocuments>
<ns2:document>orderForm</ns2:document>
</requestedDocuments>
</orderControl>
</orderEntry>
</order>
the resulting xml should look like this:
<order
xmlns="some.url.02"
xmlns:ns2="some.other.url.02"
xmlns:ns3="another.one.02"
>
<orderEntry>
<orderControl>
<mandant>test</mandant>
<businessUnit>test</businessUnit>
<inboundChannel>test</inboundChannel>
<!-- deleted timestamp for example -->
<requestedDocuments>
<ns2:document>orderForm</ns2:document>
</requestedDocuments>
</orderControl>
</orderEntry>
</order>
but the only thing I get is:
<order
xmlns="some.url.02"
>
<orderEntry>
<orderControl>
<mandant>test</mandant>
<businessUnit>test</businessUnit>
<inboundChannel>test</inboundChannel>
<!-- deleted timestamp for example -->
<requestedDocuments>
<ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
</requestedDocuments>
</orderControl>
</orderEntry>
</order>
Now maybe for one or two of you it might not be that big a deal, but I have the restriction that the output document should look one-to-one the same as the input document except for the requested changes (namespace changes and deletion).
My XSLT looks a like this:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="some.url.02"
xmlns:ns2="some.other.url.02"
xmlns:ns3="another.one.02"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="name(.) != 'timestamp'">
<xsl:element name="{node-name(.)}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{node-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Can somebody please help? Namespaces are tricky :(
P.S.: Whoever edited my entry: Thanks :)
You can set the namespace on the output element with the namespace attribute:
<xsl:element name="{node-name(.)}" namespace="http://www.bar.org">
// ...
</xsl:element>
Note that the namespace must be a URI and although I expect you know this it's probably a good idea to use URIs in your example.
Here is a link to the excellent ZVON tutorial which has worked examples:
http://www.zvon.org/xxl/XSLTreference/Output/xslt_element_namespace.html
I agree that namespaces are tricky. As you know the prefix is semantically irrelevant, but many systems allow you to choose your prefix for aesthetic reasons. Also look at Saxon (http://saxon.sourceforge.net/)
EDIT I think you will find your answer here:
XSLT root tag namespace instead of element attribute namespace
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns1_src="some.url.01"
xmlns:ns2_src="some.other.url"
xmlns:ns3_src="another.one"
xmlns="some.url.02"
xmlns:ns2="some.other.url.02"
xmlns:ns3="another.one.02"
>
<!--
Note that all the source namespaces got their own new "*_src" prefix.
The target namespaces take over the original prefixes.
"some.url.02" is the new global namespace.
-->
<xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- the identity template to copy everything, unless
it has been declared otherwise -->
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<!-- three templates to handle elements -->
<xsl:template match="ns1_src:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node() | #*" />
</xsl:element>
</xsl:template>
<xsl:template match="ns2_src:*">
<xsl:element name="ns2:{local-name()}">
<xsl:apply-templates select="node() | #*" />
</xsl:element>
</xsl:template>
<xsl:template match="ns3_src:*">
<xsl:element name="ns3:{local-name()}">
<xsl:apply-templates select="node() | #*" />
</xsl:element>
</xsl:template>
<!-- three templates to handle attributes -->
<xsl:template match="#ns1_src:*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
<xsl:template match="#ns2_src:*">
<xsl:attribute name="ns2:{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
<xsl:template match="#ns3_src:*">
<xsl:attribute name="ns3:{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
<!-- timestamps will be ignored -->
<xsl:template match="ns1_src:timestamp" />
</xsl:stylesheet>
Output:
<order xmlns="some.url.02">
<orderEntry>
<orderControl>
<mandant>test</mandant>
<businessUnit>test</businessUnit>
<inboundChannel>test</inboundChannel>
<requestedDocuments>
<ns2:document xmlns:ns2="some.other.url.02">orderForm</ns2:document>
</requestedDocuments>
</orderControl>
</orderEntry>
</order>
<xsl:template match="a:*">
<xsl:element name="{local-name()}"
namespace="http://example.com/B">
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
It searches for any element in namespace with prefix a and replaces it with an element with the same name of namespace http://example.com/B. All attributes are copied 'as is' and then all children are evaluated.
Add your custom processing in or around that as needed.
Are you using Ant's XSLT task to do your transformation?
If the answer is yes, you may want to switch from the default XSLT engine that comes with Sun JDK 1.5+. Read this.
Also, read this article about namespaces in XSLT