Learning XSLT a hands-on introduction to XSLT and Xpath


Learning XSLT: A Hands-On Introduction to XSLT and XPath
By 作者:Michael Fitzgerald
pages 页数: 370 pages
Publisher Finelybook 出版社: O'Reilly Media; 1 edition (Nov. 24 2003)
Language 语言: English
ISBN-10 书号:0596003277
ISBN-13 书号:9780596003272
The Book Description robot was collected from Amazon and arranged by Finelybook
Product description
Review
"I think this book is very well put together and succeeds in making what could be a very tedious subject relatively interesting. I am sure that anyone starting to work with XSLT will find it most helpful but you will need to get Xalan going as well as downloads the examples." - John Collins, news@UK, March
Excerpt. © Reprinted by permission. All rights reserved.
Chapter 2 - Building New Documents with XSLT
In the first chapter of this book, you got acquainted with the basics of how XSLT works. This chapter will take you a few steps further by showing you how to add text and markup to your result tree with XSLT templates.

First, you’ll add literal text to your output. Then you’ll work with literal result elements, that is, elements that are represented literally in templates. You’ll also learn how to add content with the text, element, attribute, attribute-set, comment, and processing-instruction elements. In addition, you’ll get your first encounter with attribute value templates, which provide a way to define templates inside attribute values.

Outputting Text
You can put plain, literal text into an XSLT template, and it will be written to a result tree when the template containing the text is processed. You saw this work in the very first example in the book (msg.xsl in Chapter 1). I’ll go into more detail about adding literal text in this section.

Look at the single-element document text.xml in examples/ch02 (this directory is where all example files mentioned in this chapter can be found):

You can easily add text to your output.

With text.xml in mind, consider the stylesheet txt.xsl:

Message:

When applied to text.xml, here is what generally happens, although the actual order of events may vary internally in a processor:

1. The template rule in txt.xsl matches the root node (/), the beginning point of the source document.
2. The implicit, built-in template for elements then matches message.
3. The text "Message: " (including one space) is written to the result tree.
4. apply-templates processes the text child node of a message using the built-in template for text.
5. The built-in template for text picks up the text node "You can easily add text to your output."
6. The output is serialized.

Apply txt.xsl to text.xml using Xalan:
xalan text.xml txt.xsl

This gives you the following output:
Message: You can easily add text to your output.

The txt.xsl stylesheet writes the little tidbit of literal text, "Message: ", from its template onto the output, and also grabs some text out of text.xml, and then ultimately puts them together in the result tree. You can do the same thing with the XSLT instruction element text.

Using the text Element
Instead of literal text, you can use XSLT’s text instruction element to write text to a result tree. Instruction elements, you’ll remember, are elements that are legal only inside templates. Using the text element gives you more control over result text than literal text can.

The template rule in lf.xsl contains some literal text, including whitespace:

Message:

When you apply lf.xsl to text.xml with Xalan like this:

xalan text.xml lf.xsl

the whitespace—a linefeed and some space—is preserved in the result:

Message:
You can easily add text to your output.

The XSLT processor sees the whitespace in the stylesheet as literal text and outputs it as such. The XSLT instruction element text allows you to take control over the whitespace that appears in your template.

下载地址 DOWNLOAD隐藏内容需400积分,请先!没有帐号? 注 册 一个!
}
觉得文章有用就打赏一下文章作者
未经允许不得转载:finelybook » Learning XSLT a hands-on introduction to XSLT and Xpath
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏