Comment obtenir la valeur de l'attribut d'entité non analysée auprès de XSLT?
Question
J'ai un problème avec XSLT et une entité non analysée en XML. Voici un scénario fictif. J'ai d'abord obtenu un fichier XML nommé doc.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE document [
<!ELEMENT document (employee)*>
<!ELEMENT employee (lastname, firstname)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT firstname (#PCDATA)>
<!NOTATION FOO SYSTEM 'text/xml'>
<!ENTITY ATTACHMENT SYSTEM 'attach.xml' NDATA FOO>
<!ATTLIST employee
detail ENTITY #IMPLIED>
]>
<document>
<employee detail="ATTACHMENT">
<lastname>Bob</lastname>
<firstname>Kevin</firstname>
</employee>
</document>
Dans ce fichier XML, j'utilise une entité non analysée (NDATA) pour l'attribut " detail " de l'élément " employé " ;. Le fichier attach.xml est:
<?xml version="1.0" encoding="UTF-8"?>
<name>Bob Kevin</name>
Ensuite, je souhaite utiliser XSLT pour générer une sortie avec le fichier attach.xml incorporé. Mon fichier XSLT est nommé doc.xsl:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="document">
<Document>
<xsl:apply-templates select="employee"/>
</Document>
</xsl:template>
<xsl:template match="employee">
Employee is: <xsl:value-of select="@detail"/>
</xsl:template>
</xsl:stylesheet>
Enfin, je cours avec Xalan 2.7.1:
java -jar xalan.jar -IN doc.xml -XSL doc.xsl -OUT docout.xml
Le résultat est:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is: ATTACHMENT
</Document>
Ce n’est pas ce que je veux. Je veux que la sortie ressemble à ceci:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is: <name>Bob Kevin</name>
</Document>
Comment dois-je réécrire le script XSLT pour obtenir le résultat correct?
La solution
La solution dans XSLT 2.0 :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="document">
<Document>
<xsl:apply-templates select="employee"/>
</Document>
</xsl:template>
<xsl:template match="employee">
Employee is: <xsl:value-of select=
"unparsed-text(unparsed-entity-uri(@detail))"/>
</xsl:template>
</xsl:stylesheet>
Notez ce qui suit:
-
L'utilisation des fonctions XSLT
unparsed-text ()
et < code> unparsed-entity-uri () . -
Le texte du fichier attach.xml sera échappé dans la sortie . Si vous voulez le voir sans échapper, utilisez le
& c; cdata-section- éléments "
" de l'attribut< xsl: sortie / >
instruction.
Autres conseils
Merci Dimitre Novatchev. Selon votre réponse, j'ai obtenu mon résultat dans XSLT 1.0. Pour ceux qui pourraient être intéressés, veuillez vous référer à http://www.xml.com/lpt/ a / 1243 pour une discussion. Voici ma solution:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="document">
<Document>
<xsl:apply-templates select="employee"/>
</Document>
</xsl:template>
<xsl:template match="employee">
Employee is: <xsl:copy-of select="document(unparsed-entity-uri(@detail))"/>
</xsl:template>
</xsl:stylesheet>
Notez la ligne suivante en haut:
<xsl:copy-of select="document(unparsed-entity-uri(@detail))"/>