Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transforming XSLT

Status
Not open for further replies.

callado4

Programmer
Joined
Aug 24, 2007
Messages
1
Location
US
I have created some XSLT stylesheets to convert some XML into an XSL-FO document. I now have a requirement to move any font or font-size attributes for fo:blocks into a child fo:inline element.

So say I have something like this:

<fo:block font-size="6pt">
This font is 6 points
</fo:block>
<fo:block font="8px Arial">
This font is Arial 8 points
</fo:block>
<fo:block>Just text</fo:block>
<fo:block>
<fo:external-graphic src="graphic.gif"/>
</fo:block>

I would like to create an xslt stylesheet to transform it to:

<fo:block>
<fo:inline font-size="6pt">
This font is 6 points
</fo:inline>
</fo:block>
<fo:block>
<fo:inline font="8px Arial">
This font is Arial 8 points
</fo:inline>
</fo:block>
<fo:block>Just text</fo:block>
<fo:block>
<fo:external-graphic src="graphic.gif"/>
</fo:block>
 
You can construct the xsl document like this : importing an identity transformation and then adding the specific template, shown below, for the purpose.
[tt]
<xsl:template match="fo:block">
<xsl:copy>
<xsl:if test="@font-size|@font">
<fo:inline>
<xsl:if test="@font-size">
<xsl:attribute name="font-size">
<xsl:value-of select="@font-size" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@font">
<xsl:attribute name="font">
<xsl:value-of select="@font" />
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node()|text()|comment()|processing-instruction()" />
</fo:inline>
</xsl:if>
<xsl:if test="not(@font-size|@font)">
<xsl:apply-templates select="node()|text()|comment()|processing-instruction()" />
</xsl:if>
<xsl:apply-templates select="@*[(name()!='font-size') or (name()!='font')]" />
</xsl:copy>
</xsl:template>
[/tt]
 
Slightly simplified:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format">[/URL]
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="fo:block[@font-size|@font]">
    <xsl:copy>
      <fo:inline>
        <xsl:for-each select="@*">
          <xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates/>
      </fo:inline>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top