Reputation: 263
I have searched all morning for an answer to this, with no luck. Maybe I'm just not searching correctly, I don't know. I am displaying XML markup in a PDF using the following tag set:
<verbatimText><para><quantity>
<quantityGroup quantityUnitOfMeasure="ft.lbf"><quantityValue>10</quantityValue>
<quantityTolerance quantityToleranceType="plusorminus" quantityUnitOfMeasure="ft.lbf"> 2</quantityTolerance></quantityGroup>
</quantity>.</para></verbatimText>
I have no issue displaying this as an all BOLD, single color block in the PDF. What I want to try and accomplish is to make the XML syntax colored, much like syntax highlighting in a text editor, where the element names would be blue, the attributes red, and element content black.
I would think there is a way to format substrings of text, but am having trouble finding a good starting spot on this one.
Upvotes: 2
Views: 5991
Reputation: 52858
Here's an example of how you could use XSLT 2.0 (xsl:analyze-string
) to add color.
It could use some tweaking, but illustrates what I was thinking...
XML Input
<verbatimText><para><quantity>
<quantityGroup quantityUnitOfMeasure="ft.lbf"><quantityValue>10</quantityValue>
<quantityTolerance quantityToleranceType="plusorminus" quantityUnitOfMeasure="ft.lbf"> 2</quantityTolerance></quantityGroup>
</quantity>.</para></verbatimText>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="verbatimText">
<fo:block>
<xsl:analyze-string select="." regex="(</?)([^\s>]+)(>?)">
<xsl:matching-substring>
<fo:inline color="#0000FF"><xsl:value-of select="concat(regex-group(1),'',regex-group(2),
if (regex-group(3)) then '>' else '')"/></fo:inline>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="(\s)([^=<]+=['"])([^'"]+)(['"])(>?)">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<fo:inline color="#FF0000"><xsl:value-of select="regex-group(2)"/></fo:inline>
<xsl:value-of select="regex-group(3)"/>
<fo:inline color="#FF0000"><xsl:value-of select="concat(regex-group(4),
if (regex-group(5)) then '>' else '')"/></fo:inline>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</fo:block>
</xsl:template>
</xsl:stylesheet>
XSL-FO (Using Saxon-HE 9.5)
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:inline color="#0000FF"><para></fo:inline>
<fo:inline color="#0000FF"><quantity></fo:inline>
<fo:inline color="#0000FF"><quantityGroup</fo:inline>
<fo:inline color="#FF0000">quantityUnitOfMeasure="</fo:inline>ft.lbf<fo:inline color="#FF0000">"></fo:inline>
<fo:inline color="#0000FF"><quantityValue></fo:inline>10<fo:inline color="#0000FF"></quantityValue></fo:inline>
<fo:inline color="#0000FF"><quantityTolerance</fo:inline>
<fo:inline color="#FF0000">quantityToleranceType="</fo:inline>plusorminus<fo:inline color="#FF0000">"</fo:inline>
<fo:inline color="#FF0000">quantityUnitOfMeasure="</fo:inline>ft.lbf<fo:inline color="#FF0000">"></fo:inline> 2<fo:inline color="#0000FF"></quantityTolerance></fo:inline>
<fo:inline color="#0000FF"></quantityGroup></fo:inline>
<fo:inline color="#0000FF"></quantity></fo:inline>.<fo:inline color="#0000FF"></para></fo:inline>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
PDF Output (Using FOP 1.1)
Upvotes: 2