XSLTから未解析のエンティティ属性の値を取得する方法は?
質問
XSLTとXMLの未解析エンティティに問題があります。これが架空のシナリオです。最初に、doc.xmlという名前の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>
このXMLファイルでは、属性<!> quot; detail <!> quot;に未解析のエンティティ(NDATA)を使用しています。要素の<!> quot; employee <!> quot;。 attach.xmlは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<name>Bob Kevin</name>
次に、XSLTを使用して、attach.xmlが埋め込まれた出力を生成します。 XSLTファイルの名前は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>
最後に、Xalan 2.7.1を使用して実行します:
java -jar xalan.jar -IN doc.xml -XSL doc.xsl -OUT docout.xml
出力は次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is: ATTACHMENT
</Document>
これは私が望むものではありません。出力は次のようになります。
<?xml version="1.0" encoding="UTF-8"?>
<Document>
Employee is: <name>Bob Kevin</name>
</Document>
正しい結果を得るために、XSLTスクリプトをどのように書き換えるべきですか?
解決
The solution 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>
Do note the following:
The use of the XSLT functions
unparsed-text()
andunparsed-entity-uri()
.The text of the attach.xml file will be escaped in the output. If you want to see it unescaped, use the
"cdata-section-elements"
attribute of the<xsl:output/>
instruction.
他のヒント
Thank you, Dimitre Novatchev. According to your answer, i got my result in XSLT 1.0. For those who may be interested, please refer to http://www.xml.com/lpt/a/1243 for a discussion. Here is my 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>
Note the the following line from above:
<xsl:copy-of select="document(unparsed-entity-uri(@detail))"/>