[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Blindstaring on descendent(-or-self), following(-

Subject: Re: Blindstaring on descendent(-or-self), following(-sibling) and the like, from inside an xsl:function
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 26 Sep 2006 13:30:41 -0400
descenden
Abel,

Ordinarily we use the self:: axis to determine the type of a node (except attributes), as in

$someparam[1][self::text()] returns $someparam[1] if it's a text node, but nothing if it's not.

In your case, it appears to me this would map to:

<xsl:when test="$current-node/node()[1][self::text()]">
     <xsl:text>NO TAG, JUST TEXT, WRITE FONT_DECL</xsl:text>
</xsl:when>

... but given your case, don't you want the font_decl *any* time you have a text node child (even in the middle or at the end)? So "$current-node/text()" would be the test?

If it passes the test, I think you probably want to shift into a special mode:

<xsl:template match="*" mode="font-wrapper">
<!-- an element processed in the mode needs what it needs to do otherwise -->
<xsl:apply-templates select="."/>
</xsl:template>


<xsl:template match="text()" mode="font-wrapper">
  <font stuff...>
    <xsl:value-of select="."/>
  </font>
</xsl:template>

... and indeed, if you do it that way you don't need a test outside at all (since template matching does the work) -- just shift into the mode for all cases.

In any case I suspect that even if this doesn't do the trick, a special mode offers you a much nicer and more general solution to your problem.

Similarly, I wonder if you really want or need a function to do this. In XSLT we can "micro-pipeline", using templates to process the results of using templates; the function looks to me like it's only adding extra overhead.

Please forgive me if I've understood the problem incorrectly.

Cheers,
Wendell

At 12:56 PM 9/26/2006, you wrote:
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:
&lt;ins> A Textnode &lt;u> another text node &lt;/u>&lt;/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,

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Cast Your Vote

We need your help – Vote for DataDirect XML Products!

  • Best SOA or XML site

Winners and finalists announced at SOA World Conference in November.

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.