L'ajout d'un namespace fait perdre un attribut
Bonjour,
J'ai un fichier XML de départ que je désire transformer en un autre fichier XML.
Au départ, je n'ai pas de namespace et je désire ajouter un namespace.
Voici mon fichier XML de départ
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="UTF-8"?>
<Document>
<PmtInf>
<ReqdColltnDt>2018-03-18</ReqdColltnDt>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>voice0116</EndToEndId>
</PmtId>
<!-- Attention cet element a un Attribut ! ! -->
<InstdAmt Ccy="EUR">1242.19</InstdAmt>
</DrctDbtTxInf>
</PmtInf>
<PmtInf>
<ReqdColltnDt>2018-03-19</ReqdColltnDt>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>voice0349</EndToEndId>
</PmtId>
<InstdAmt Ccy="EUR">1055.12</InstdAmt>
</DrctDbtTxInf>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>voice0143</EndToEndId>
</PmtId>
<InstdAmt Ccy="EUR">750.32</InstdAmt>
</DrctDbtTxInf>
</PmtInf>
</Document> |
Comme vous le voyez, la racine est simplement <Document>
Je désire obtenir une racine comme ceci <Document xmlns="urn:iso:std:iso:202:tech">
Pour y parvenir, j'applique la transformation xslt suivante
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- <xsl:strip-space elements="*"/> -->
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="*[ancestor::Document]">
<xsl:element name="{local-name()}" namespace="urn:iso:std:iso:202:tech">
<xsl:apply-templates select="*|text()"/>
</xsl:element>
</xsl:template>
<xsl:template match="Document|@*">
<Document xmlns="urn:iso:std:iso:202:tech">
<xsl:apply-templates select="*|text()"/>
</Document>
</xsl:template>
</xsl:stylesheet> |
Cela fonctionne plutôt bien si ce n'est que je perds l'attribut Ccy
Voici le résultat que j'obtiens :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?>
<Document xmlns="urn:iso:std:iso:202:tech">
<PmtInf>
<ReqdColltnDt>2018-03-18</ReqdColltnDt>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>voice0116</EndToEndId>
</PmtId>
<InstdAmt>1242.19</InstdAmt>
</DrctDbtTxInf>
</PmtInf>
<PmtInf>
<ReqdColltnDt>2018-03-19</ReqdColltnDt>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>voice0349</EndToEndId>
</PmtId>
<InstdAmt>1055.12</InstdAmt>
</DrctDbtTxInf>
<DrctDbtTxInf>
<PmtId>
<EndToEndId>voice0143</EndToEndId>
</PmtId>
<InstdAmt>750.32</InstdAmt>
</DrctDbtTxInf>
</PmtInf>
</Document> |
Mes noeuds InstdAmt ont perdu leur attribut !!
Là où j'ai <InstdAmt>1242.19</InstdAmt> ,
j'aurais dû garder<InstdAmt Ccy="EUR">1242.19</InstdAmt>
Comment puis-je faire pour garder l'attribut Ccy ?
Merci pour vos explications