Bonjour à tous,
J'utilise le Framework 1.1 (et oui) et passe par la classe XslTransform pour modifier le format d'un XML en un autre. Le code de transformation est le suivant (notez qu'il faut passer par un StreamWriter et non un XmlTextWriter car ce dernier ne gère pas la balise xsl:output) :
Le fichier XSL est le suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 // Load the Xml doc XPathDocument myXPathDoc = new XPathDocument(strInputXmlPath); XslTransform myXslTrans = new XslTransform(); // Load the Xsl myXslTrans.Load(strXslPath); // Create the output stream writer StreamWriter myWriter = new StreamWriter(strOutputXmlPath, false, System.Text.UTF8Encoding.UTF8); // Do the actual transform of Xml myXslTrans.Transform(myXPathDoc, null, myWriter, null); myWriter.Close();
Je recopie donc le noeud racine en me contentant de le renommer, les attributs restant inchangés. Je renomme également le noeud enfant, et recopie la totalité de son contenu. Mon problème est le suivant : les noeuds directement situés sous "NewRootChild" se retrouvent avec un attribut "xmlns : xsi" que j'aimerais éviter d'avoir. Quelque chose du genre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 <?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" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="Root"> <NewRoot><xsl:copy-of select="@*"/> <xsl:apply-templates/> </NewRoot> </xsl:template> <xsl:template match="RootChild"> <NewRootChild> <xsl:copy-of select="*"/> <xsl:apply-templates/> </NewRootChild> </xsl:template> </xsl:stylesheet>
Comment m’affranchir de ces attributs? Je signale que ces derniers n'apparaissent que lorsque je fais la transformation via mon code C#. Ce n'est pas le cas lorsque je la fais via XMLSpy, mon éditeur XML.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <NewRoot <!-- Attributs copiés correctement --> > <NewRootChild> <NewRootChildChild <!-- Attributs copiés correctement --> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- Noeuds enfants copiés correctement --> </NewRootChildChild> <NewRootChildChild <!-- Attributs copiés correctement --> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- Noeuds enfants copiés correctement --> </NewRootChildChild> <!-- Etc ... --> </NewRootChild> </NewRoot>
D'avance merci pour votre aide !![]()
Partager