Yuan Yijun (bbbush) wrote,
Yuan Yijun
bbbush

  • Mood:

xslt 1.0

原来如此,xslt 循环的另一种办法,创建无限的空节点凑数

http://sources.redhat.com/ml/xsl-list/2001-04/msg01493.html

Re: What are these methods?</p>

    * To: "Yang" <sfyang at unisvr dot net dot tw>
    * Subject: Re: [xsl] What are these methods?
    * From: Jeni Tennison <mail at jenitennison dot com>
    * Date: Sun, 29 Apr 2001 13:50:45 +0100
    * CC: xsl-list at lists dot mulberrytech dot com
    * Organization: Jeni Tennison Consulting Ltd
    * References: <000901c0d04d$17d1d2a0$1501800a@hqlab.unisvr>
    * Reply-To: xsl-list at lists dot mulberrytech dot com


Hi Sun-fu,


> Would anyone be kind to direct me to the reference about these
> methods which Jeni specifically mentioned in her XSLT UK 2001
> Report.


These are detailed in my paper from the conference - Sebastian, Dave -
are you intending to put them on the web at all?


> patterns in instructions such as Wendell Piez's method for
> repetition


The Piez's Method is for iterating a number of times. As you know,
there's no way that you can do a for loop in XSLT in the same way as
you would in a procedural programming language - you can only iterate
over a number of nodes. However, what you can do is choose the number
of nodes you iterate over, and then use the position() of that node to
indicate the number of the iteration. So you set up a random set of
nodes (I usually use nodes in the stylesheet itself):


<xsl:variable name="random-nodes" select="document('')//node()" />


Then you pick from them the number that you want, and iterate over
them:


<xsl:for-each select="$random-nodes[position() &lt; $number]">
   ...
</xsl:for-each>


Whatever you want to do, held in the xsl:for-each, is repeated $number
times.


(Note: You can do the same thing with a recursive template, and the
Piez Method can be tricky if you can't find enough random nodes to
use, and can take up a lot of memory if you collect too many random
nodes, but most of the time it's a lot less bother than writing a
recursive template.)


> and David Allouche's method for normalizing strings


The Allouche Method helps you control whitespace in your result. When
the XSLT processor reads in the stylesheet, it strips out any
whitespace-only text nodes (text nodes that are made up purely of
whitespace), but it leaves in any text nodes that have non-whitespace
characters in them.  Usually you'd get around this by wrapping the
text that you actually want added within an xsl:text element.  So for
example:


  <xsl:text>(</xsl:text>
  <xsl:value-of select="$expression" />
  <xsl:text>)</xsl:text>


Rather than doing that, you can use an empty xsl:text element (or
indeed any other XSLT element, but xsl:text is good because it's short
and it doesn't give you any output) to delimit the whitespace that you
don't want.  So instead of the above, I could do:


  <xsl:text />(<xsl:value-of select="$expression" />)<xsl:text />


(Note: The other method for dealing with this kind of situation is to
wrap the text in a concat() in the xsl:value-of, e.g.:


  <xsl:value-of select="concat('(', $expression, ')')" />


That's probably a little more efficient and reduces the size of the
stylesheet node tree, but the Allouche Method is more general.)


I hope that helps,


Jeni


---
Jeni Tennison
http://www.jenitennison.com/


 


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




http://www.xslt.com/html/xsl-list/2005-08/msg00633.html

> >I know the idea, which is called the Piez method.
>
> *Some* of us call it the "Piez method"... :->
>
> Others of us call it "the ugly hack once suggested by Wendell as a
> workaround to avoid having to code a recursion, as if that were so hard".
> (But as the European Editor of the TEI once said in another forum, there
> are those who love recursion, and those who find it terrifying.)


Wendell,


I love recursion, but I also love original thinking.


The Piez method, in particular.


The XSLT 1.0 Period was  typical for that it required some ingenious
tricks to fill and supplement the terseness and incompleteness of the
XSLT 1.0 language.


While this thread shows how very easy it is to misunderstand and abuse
these inventions, people, who invented such tricks to help everyone
else are the heroes of the XSLT 1.0 era.



Cheers,
Dimitre.


>
> Oh well: that's the risk you run when you suggest tricky ways of
> accomplishing things, I guess. It'll serve me right if in another five
> years, people who learned the "Piez method" in school are still using it in
> their XSLT 2.0, where there's no excuse for it at all. I'll probably be the
> one that has to fix their code.
>
> So then, Mike writes
>
> >This is "trick programming", some would say it's a dirty trick, and it's a
> >matter of personal taste whether you use it.
>
> ... and I hasten to add "some, including some (one) who thought of it".
>
> Cheers,
> Wendell
>
>
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.                http://www.mulberrytech.com
> 17 West Jefferson Street                    Direct Phone: 301/315-9635
> Suite 207                                          Phone: 301/315-9631
> Rockville, MD  20850                                 Fax: 301/315-8285
> ----------------------------------------------------------------------
>   Mulberry Technologies: A Consultancy Specializing in SGML and XML
> ======================================================================
>
>
> --~------------------------------------------------------------------
> 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@xxxxxxxxxxxxxxxxxxxxxx>
> --~--
>
>



--
Cheers,
Dimitre Novatchev
---------------------------------------
Harry did not ask how Dumbledore knew; ...but Harry had long since
learned that bangs and smoke were more often the marks of ineptitude
than expertise.


--~------------------------------------------------------------------
XSL-List info and archive: 



XSLT UK 2001 Report by Jeni Tennison April 25, 2001

http://www.jenitennison.com/index.xml

update 20060214
http://www.xml.org/xml/xslt_efficient_programming_techniques.pdf
XSLT Cookbook 中提到的一本小册子,只有十页,却很有用,列举了各种重要的方法。我想 xslt 2.0 应该能创造出更多可能的代码,毕竟表达能力又增强了。集合的运算用得很多,还有 xsl:key 的用法,据说用 key 之后,像我做的表格结构转层次结构,会简单并且快许多。
Tags: 小东西
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments