How to get unparsed entity attribute value from XSLT?

I have a problem with XSLT and unparsed entity in XML. Here is a fictional scenario. First, I got an XML file called 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 this XML file, I use unparsed entity (NDATA) for the detail attribute of the employee element. File attach.xml:

<?xml version="1.0" encoding="UTF-8"?>

<name>Bob Kevin</name>

Then I want to use XSLT to generate output along with nested nested. My XSLT file is called 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>

Finally, I run using Xalan 2.7.1:

java -jar xalan.jar -IN doc.xml -XSL doc.xsl -OUT docout.xml

Output:

<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is:  ATTACHMENT
</Document>

This is not what I want. I want the result to look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is:  <name>Bob Kevin</name>
</Document>

How do I rewrite an XSLT script to get the correct result?

+3
source share
2 answers

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>

:

  1. XSLT unparsed-text() unparsed-entity-uri().

  2. attach.xml . , , "cdata-section-elements" <xsl:output/>.

+2

, . , XSLT 1.0. , , http://www.xml.com/lpt/a/1243 . :

<?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>

:

 <xsl:copy-of select="document(unparsed-entity-uri(@detail))"/>
+1

All Articles