Come ottenere il valore dell'attributo entità non analizzato da XSLT?
Domanda
Ho un problema con XSLT e entità non analizzata in XML. Ecco uno scenario immaginario. Per prima cosa ho ottenuto un file XML chiamato 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>
In questo file XML, utilizzo un'entità non analizzata (NDATA) per l'attributo " dettaglio " dell'elemento " employee " ;. Attach.xml è:
<?xml version="1.0" encoding="UTF-8"?>
<name>Bob Kevin</name>
Quindi voglio usare XSLT per generare un output insieme a attach.xml incorporato. Il mio file XSLT si chiama 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>
Infine, corro usando Xalan 2.7.1:
java -jar xalan.jar -IN doc.xml -XSL doc.xsl -OUT docout.xml
L'output è:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is: ATTACHMENT
</Document>
Questo non è quello che voglio. Voglio che l'output sia simile al seguente:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is: <name>Bob Kevin</name>
</Document>
Come devo riscrivere lo script XSLT per ottenere il risultato corretto?
Soluzione
La soluzione in 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>
Nota quanto segue:
L'uso delle funzioni XSLT
unparsed-text () e < code> unparsed-entità-uri () .Il testo del file attach.xml verrà salvato nell'output . Se vuoi vederlo senza caratteri di escape, utilizza la
" cdata-section- elementi "
dell'attributo< xsl: output / > istruzioni.
Altri suggerimenti
Grazie, Dimitre Novatchev. Secondo la tua risposta, ho ottenuto il mio risultato in XSLT 1.0. Per coloro che potrebbero essere interessati, fai riferimento a http://www.xml.com/lpt/ a / 1243 per una discussione. Ecco la mia soluzione:
<?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>
Nota la seguente riga dall'alto:
<xsl:copy-of select="document(unparsed-entity-uri(@detail))"/>