twfurst
twfurst

Reputation: 263

XSL-FO make text different color in single element

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>&lt;para>&lt;quantity>
&lt;quantityGroup quantityUnitOfMeasure="ft.lbf">&lt;quantityValue>10&lt;/quantityValue>
&lt;quantityTolerance quantityToleranceType="plusorminus" quantityUnitOfMeasure="ft.lbf">  2&lt;/quantityTolerance>&lt;/quantityGroup>
&lt;/quantity>.&lt;/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

Answers (1)

Daniel Haley
Daniel Haley

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>&lt;para>&lt;quantity>
&lt;quantityGroup quantityUnitOfMeasure="ft.lbf">&lt;quantityValue>10&lt;/quantityValue>
&lt;quantityTolerance quantityToleranceType="plusorminus" quantityUnitOfMeasure="ft.lbf">  2&lt;/quantityTolerance>&lt;/quantityGroup>
&lt;/quantity>.&lt;/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="(&lt;/?)([^\s>]+)(>?)">
                <xsl:matching-substring>
                    <fo:inline color="#0000FF"><xsl:value-of select="concat(regex-group(1),'&#xFEFF;',regex-group(2),
                        if (regex-group(3)) then '&#xFEFF;>' else '')"/></fo:inline>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:analyze-string select="." regex="(\s)([^=&lt;]+=['&quot;])([^'&quot;]+)(['&quot;])(>?)">
                        <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 '&#xFEFF;>' 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">&lt;para&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantity&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantityGroup</fo:inline> 
            <fo:inline color="#FF0000">quantityUnitOfMeasure="</fo:inline>ft.lbf<fo:inline color="#FF0000">"&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantityValue&gt;</fo:inline>10<fo:inline color="#0000FF">&lt;/quantityValue&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;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">"&gt;</fo:inline>  2<fo:inline color="#0000FF">&lt;/quantityTolerance&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;/quantityGroup&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;/quantity&gt;</fo:inline>.<fo:inline color="#0000FF">&lt;/para&gt;</fo:inline>
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

PDF Output (Using FOP 1.1)

enter image description here

Upvotes: 2

Related Questions