Reputation: 21
I have tried a few things, but with no success. I have an input document with declared file entities. I need to copy it exactly as is to output document. This is what it looks like (# is redacted symbol here):
<!DOCTYPE ##### PUBLIC "-##//EN"
"###.dtd" [
<!ENTITY Chap##-## SYSTEM "###.xml">
<!ENTITY Chap##-### SYSTEM "####.xml">
<!ENTITY Chap##-#### SYSTEM "#####.xml">
As it stands now, I use an identity template and a match ='/' to save the document to a variable and then use specific template matches to match all other XML elements and output them as specific HTML elements. The only thing that doesn't work is keeping that doctype statement in tact.
Upvotes: 2
Views: 1132
Reputation: 66783
One trick with XSLT 2.0 (or higher) would be use the function unparsed-text()
to read the source document as a string, and then apply a regex or use other string functions to select the DTD section from the top of the file to include in your output using xsl:value-of
with disable-output-escaping="yes"
For example, using replace()
with a regex pattern and a capture group:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:value-of disable-output-escaping="yes"
select="replace(
unparsed-text(base-uri()),
'.*?(\n?<!DOCTYPE\s.*\]>)\n?<.*',
'$1',
's')"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1