Re: [xsl] formatting text with nested features

Subject: Re: [xsl] formatting text with nested features
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Fri, 24 Oct 2003 05:20:57 -0600
Dont have an FO parser on this machine but in standard HTML format the
following has been tested in IE using msxml4 ( i believe thats whats on here
but it may just as well be msxml3.)

XML...

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

<?xml-stylesheet type="text/xsl" href="test.xslt" ?>

<main>
  <body>
    <p>
      normal normal <b>bold bold <i>bold-italic</i> bold bold<sub>3</sub>
</b> normal<sup>5</sup> normal
    </p>
  </body>
</main>


Parsed with the following XSLT...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
 <!ENTITY nbsp "&#160;">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:template match="/">

    <xsl:apply-templates/>

</xsl:template>

<xsl:template match="p">
 <p>
   <xsl:apply-templates/>
 </p>
</xsl:template>

<xsl:template match="b">
 <b>
   <xsl:apply-templates/>
 </b>
</xsl:template>

<xsl:template match="i">
 <i>
   <xsl:apply-templates/>
 </i>
</xsl:template>

<xsl:template match="sub">
 <sub>
   <xsl:apply-templates/>
 </sub>
</xsl:template>

<xsl:template match="sup">
 <sup>
   <xsl:apply-templates/>
 </sup>
</xsl:template>

</xsl:stylesheet>

produces the following result (not sure if it will make it through the list
processor correctly but I assure you it gives you the result you want.  Just
replace my HTML with the correct FO elements and youll be all set.

normal normal bold bold bold-italic bold bold3 normal5 normal

If you want to have special cases for specific nested elements such as
<b><i>foo</i><sub>2</sub></b>  then just add additional templates that
specify the pattern in the match attribute of the template.

So the XML...

<main>
  <body>
    <p>
      normal normal <b>bold bold <i>bold-italic</i> bold bold<sub>3</sub>
</b> normal<sup>5</sup> normal
    </p>
    <p>
      normal normal <b>bold bold</b> <b><i>bold-italic</i></b> <b>bold
bold<sub>3</sub></b> normal<sup>5</sup> normal
    </p>
  </body>
</main>

Processed with this XSLT...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
 <!ENTITY nbsp "&#160;">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my="urn:my-scripts">
<xsl:template match="/">

    <xsl:apply-templates/>

</xsl:template>

<xsl:template match="p">
 <p>
   <xsl:apply-templates/>
 </p>
</xsl:template>

<!-- special case template for <b> elements that contain <i> elements but
not if they contain a <sub> element as well -->
<xsl:template match="b[i][not(sub)]">
 <h1>
   <xsl:apply-templates/>
 </h1>
</xsl:template>

<xsl:template match="b">
 <b>
   <xsl:apply-templates/>
 </b>
</xsl:template>

<xsl:template match="i">
 <i>
   <xsl:apply-templates/>
 </i>
</xsl:template>

<xsl:template match="sub">
 <sub>
   <xsl:apply-templates/>
 </sub>
</xsl:template>

<xsl:template match="sup">
 <sup>
   <xsl:apply-templates/>
 </sup>
</xsl:template>

</xsl:stylesheet>

Will produce the following result...

normal normal bold bold bold-italic bold bold3 normal5 normal

normal normal bold bold

bold-italic
bold bold3 normal5 normal



Again, may not make it through the list processor in the proper format.  To
test it just save the XML as a test.xml file and the XSLT as test.xslt in
the same directory and open the xml file in IE.

Hope this helps!

Best regards,

M.


----- Original Message ----- 
From: "Kloeck, Erwin" <Erwin.Kloeck@xxxxxx>
To: "xsl-list@Lists. mulberrytech. com (E-Mail)"
<xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Friday, October 24, 2003 3:56 AM
Subject: [xsl] formatting text with nested features


>
> Hi,
>
> I have the following xml that I want to format in the obvious way with
xsl:fo.
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <main>
>   <body>
>     <p>
>       normal normal <b>bold bold <i>bold-italic</i> bold bold<sub>3</sub>
</b> normal<sup>5</sup> normal
>     </p>
>   </body>
> </main>
>
>
> Currently my xsl looks like shown below. In it I select all text() nodes
within a paragraph and
> then check for the combinations of attributes that hold. This worked OK
for bold and italic, but
> when more attributes are used, I get overwhelmed by the combinations. So
what is the clever way
> to do this?
>
> Thanks a lot.
>
> Erwin
>
>
>
> <<?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format";>
>   <xsl:output method="xml" indent="yes"/>
>   <xsl:template match="/">
>     <xsl:apply-templates/>
>   </xsl:template>
>   <xsl:template match="/main/body">
>     <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format";>
>       <fo:layout-master-set>
>         <fo:simple-page-master master-name="DINA4"
>                     page-height="297mm"
>                     page-width="210mm"
>                     margin-top="25.0mm"
>                     margin-bottom="25.0mm"
>                     margin-left="25.0mm"
>                     margin-right="25.0mm">
>           <fo:region-body/>
>         </fo:simple-page-master>
>         <fo:page-sequence-master master-name="master-sequence">
>           <fo:repeatable-page-master-reference master-reference="DINA4"/>
>         </fo:page-sequence-master>
>       </fo:layout-master-set>
>       <fo:page-sequence master-reference="master-sequence">
>         <fo:flow flow-name="xsl-region-body">
>           <xsl:for-each select="//p">
>             <fo:block font-family="Times-Roman" space-before="10pt">
>               <xsl:for-each select=".//text()">
>                 <xsl:choose>
>                   <xsl:when test="ancestor::i and ancestor::b">
>                     <fo:inline font-style="oblique" font-weight="bold"
space-after="8pt" color="green">
>                       <xsl:value-of select="."/>
>                     </fo:inline>
>                   </xsl:when>
>                   <xsl:when test="ancestor::i and not(ancestor::b)">
>                     <fo:inline font-style="oblique"  space-after="8pt"
color="cyan">
>                       <xsl:value-of select="."/>
>                     </fo:inline>
>                   </xsl:when>
>                   <xsl:when test="not(ancestor::i) and ancestor::b">
>                     <fo:inline font-weight="bold" space-after="8pt"
color="magenta">
>                       <xsl:value-of select="."/>
>                     </fo:inline>
>                   </xsl:when>
>                   <xsl:when test="not(ancestor::i) and not(ancestor::b)">
>                     <fo:inline space-after="8pt" color="black">
>                       <xsl:value-of select="."/>
>                     </fo:inline>
>                   </xsl:when>
>                   <xsl:when test="ancestor-or-self::sub">
>                     <fo:inline font-family="Times-Roman" font-size="8pt"
vertical-align="sub" color="red">
>                       <xsl:value-of select="."/>
>                     </fo:inline>
>                   </xsl:when>
>                   <xsl:when test="ancestor-or-self::sup">
>                     <fo:inline font-family="Times-Roman" font-size="8pt"
vertical-align="super">
>                       <xsl:value-of select="."/>
>                     </fo:inline>
>                   </xsl:when>
>                   <xsl:otherwise>
>                     <xsl:value-of select="."/>
>                   </xsl:otherwise>
>                 </xsl:choose>
>               </xsl:for-each>
>             </fo:block>
>           </xsl:for-each>
>         </fo:flow>
>       </fo:page-sequence>
>     </fo:root>
>   </xsl:template>
> </xsl:stylesheet>
>
>
> Erwin Kloeck
> Produktentwicklung
>
> Oestreicher + Wagner
> Medientechnik GmbH
> Frankenthaler Strasse 20
> D-81539 Muenchen
>
> Fon   +49 (0)89-68961 216
> Fax   +49 (0)89-68961 271
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread