Reputation: 1336
I have many XML files like that :
File1.xml :
<Document xmlns="Forward">
<Id>123456789</Id>
</Document>
File2.xml :
<Document xmlns="Forward">
<Id>4568844</Id>
</Document>
I would like to concat these files and add prefix to the namespace like that :
Output :
<?xml version="1.0" encoding="UTF-8"?>
<mes:Fichier xmlns:mes="message">
<mes:Rcvr>RECEIVE</mes:Rcvr>
<doc:Document xmlns:doc="Forward">
<doc:Id>123456789</doc:Id>
</doc:Document>
<doc:Document xmlns:doc="Forward">
<doc:Id>4568844</doc:Id>
</doc:Document>
</mes:Fichier>
My XLS :
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mes="message" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="Rcvr"/>
<xsl:param name="fileList"/>
<xsl:template match="/">
<xsl:copy>
<mes:Fichier xmlns:mes="message" >
<mes:Rcvr><xsl:value-of select="$Rcvr"/></mes:Rcvr>
<xsl:apply-templates select="@*|node()" />
</mes:Fichier>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 159
Reputation: 167696
So you have two tasks, reading in a bunch of files and transforming them. Assuming Saxon 9 as your XSLT processor you can replace
<xsl:template match="/">
<xsl:copy>
<mes:Fichier xmlns:mes="message" >
<mes:Rcvr><xsl:value-of select="$Rcvr"/></mes:Rcvr>
<xsl:apply-templates select="@*|node()" />
</mes:Fichier>
</xsl:copy>
</xsl:template>
with
<xsl:template match="/">
<mes:Fichier xmlns:mes="message" >
<mes:Rcvr><xsl:value-of select="$Rcvr"/></mes:Rcvr>
<xsl:apply-templates select="collection('.?select=file*.xml')/node()" />
</mes:Fichier>
</xsl:template>
to read in and process all files named file*.xml
.
Then for your transformation you want to add e.g.
<xsl:template match="*">
<xsl:element name="doc:{local-name()}" namespace="Forward">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
If there are more namespace involved you might need to restrict that match to the namespace you want to add the prefix to. And it makes sense to move the namespace declaration of your new root element to the root element of the stylesheet.
Upvotes: 1