xsl-list
[Top] [All Lists]

[xsl] Blindstaring on descendent(-or-self), following(-sibling) and the like, from inside an xsl:function

2006-09-26 09:56:43
Dear List,

This should be easy, I think, but I am tearing my hear out on this one... I must be overlooking something obvious...

Question: is the first item inside $someparam a text-node or any other node? Whitespace is considered a textnode also.

Input: I actually use saxon:parse to get XML out of a snippet like the following:
<ins> A Textnode <u> another text node </u></ins>

Saxon (parse) turns this in the more useful:
<ins> A Textnode <u> another text node </u></ins>

Here's my function, the way I have it currently (I know it is wrong). I cannot use current() or anything context-dependent there, it is illegal in a function.

   <xsl:function name="local:optional-new-font-tag">
       <xsl:param name="current-node" />
       <xsl:param name="stacked-styles" />
       <xsl:choose>
<!-- HOWTO: if node starts with text before next node, or only contains text -->
           <xsl:when test="$current-node[text() | *][position() = 1]">
               <xsl:text>NO TAG, JUST TEXT, WRITE FONT_DECL</xsl:text>
           </xsl:when>
           <xsl:otherwise>
               <xsl:text>ANOTHER TAG, DO NOTHING</xsl:text>
           </xsl:otherwise>
       </xsl:choose>
   </xsl:function>

Here's my calling template (also used for other tags):

   <xsl:template match="u|ins" mode="layout">
       <xsl:param name="style" />
<xsl:variable name="new-style" select="local:stack-lxf-style($style, 'underlined')" /> <xsl:variable name="font-tag" select="local:optional-new-font-tag(., $new-style)" />
       <xsl:value-of select="$font-tag" />
  </xsl:template>


Now, when there's a text-node following the "u" or "ins" nodes, I must open a new font-declaration (LXF, some really weird non/partial-xml format). In fact, I need to translate XHTML into this LXF format. In case you are wondering, here's some background:

Input:
<em>emph text
   <strong>some bold text and emph text
       <ins>underline, bold and emph</ins>
   </strong>
   only emph
</em>

Output (partially correct, it works like this now):
<lxf-font type="italic" />emph text
<lxf-font type="italic|bold" />some bold text and emph text
<lxf-font type="italic|bold|underlined" />underline, bold, and emph
<lxf-font type="normal" /> <!-- from i|b|u -->
<lxf-font type="normal" /> <!-- from i|b -->
<lxf-font type="italic" />only emph
<lxf-font type="normal" /> <!-- from i -->

This format, with only opening tags and no closing or enclosing tags (like in normal xml/html style) means that I have to stack the styles when I encounter them. Also, when I get more styles in a row, like the following example, I need to get only one statement, instead of three:

Input:
Normal text
   <em><strong><ins>
       underline, bold and emph text
   </ins></strong>
   only emph
</em>

Output (wrong):
Normal text
<lxf-font type="italic" />
<lxf-font type="italic|bold" />
<lxf-font type="italic|bold|underlined" />underline, bold, and emph text
<lxf-font type="normal" /> <!-- from i|b|u -->
<lxf-font type="normal" /> <!-- from i|b -->
<lxf-font type="italic" />only emph
<lxf-font type="normal" /> <!-- from i -->

Output (required, but I find it hard to get):
Normal text
<lxf-font type="italic|bold|underlined" />underline, bold, and emph text
<lxf-font type="italic" />only emph
<lxf-font type="normal" /> <!-- from i -->

In the correct output example, it show that return-to-normal is only done at the end. Opening a new <lxf-font> statement just means: use this font-style from here onwards. As you can see, winding up and unwinding is not equal, making it a bit hard to to.

Though I do have a system running, it has trouble with nested statements, like above. I think I can fix it as soon as I know how to find if a node starts with a text or with a normal node. I tried following-sibling::, ../ with local-name, following::, descendant:: (without self). But somewhere I lost the way to deal with it.

Any ideas are much appreciated,

Cheers!

Abel Braaksma
http://abelleba.metacarpus.com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--