XSLT Basics

http://www.learn-xslt-tutorial.com/

 

eXtensible Stylesheet Language

The eXtensible Stylesheet Language is divided into two sub-languages: eXtensible Stylesheet Language Transformations (XSLT) and eXtensible Stylesheet Language - Formatting Objects (XSL-FO). In this lesson, we will be looking at the basics of XSLT, which is used to transform XML documents.

XSLT documents are well-formed XML documents that describe how another XML document should be transformed. For XSLT to work, it needs an XML document to transform and an engine to make the transformation take place. In addition, parameters can be passed in to XSLTs providing further instructions on how to do the transformation.

The diagram below shows how this all works.

The Transformation Process

An XSLT looks at an XML document as a collection of nodes of the following types:

  • Root node
  • Element nodes
  • Attribute nodes
  • Text nodes
  • Processing instruction nodes
  • Comment nodes

An XSLT document contains one or more templates, which are created with the <xsl:template />  tag. When an XML document is transformed with an XSLT, the XSLT processor reads through the XML document starting at the root , which is one level above the document element, and progressing from top to bottom, just as a person would read the document. Each time the processor encounters a node, it looks for a matching template in the XSLT.

If it finds a matching template it applies it; otherwise, it uses a default template as defined by the XSLT specification. The default templates are shown in the table below.

Default Templates
Node TypeDefault Template
Root Apply templates for child nodes.
Element Apply templates for child nodes.
AttributeOutput attribute value.
TextOutput text value.
Processing InstructionDo nothing.
CommentDo nothing.

In this context, attributes are not considered children of the elements that contain them, so attributes get ignored by the XSLT processor unless they are explicitly referenced by the XSLT document.

 

Code Sample: XsltBasics/Demos/Beatle.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html"/>

<xsl:template match="child::person">

 <html>
  <head>
   <title>
    <xsl:value-of select="descendant::firstname" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="descendant::lastname" />

   </title>
  </head>
  <body>
   <xsl:value-of select="descendant::firstname" />
   <xsl:text> </xsl:text>
   <xsl:value-of select="descendant::lastname" />

  </body>
 </html>
</xsl:template>
</xsl:stylesheet>

Code Explanation

Note that the document begins with an XML declaration. This is because XSLTs are XML documents themselves. As with all XML documents, the XML declaration is optional.

The second line (shown below) is the document element of the XSLT. It states that this document is a version 1.0 XSLT document.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

The third line (shown below) indicates that the resulting output will be HTML.

<xsl:output method="html"/>

The fourth line is an open  <xsl:template>   element. The  match   attribute of this tag takes an  XPath , which indicates that this template applies to the  person   node of the XML document . Because  person   is the document element of the source document, this template will only run once.

There are then a few lines of HTML tags followed by two  <xsl:value-of />   elements separated by one<xsl:text>   element. The  <xsl:value-of />   tag has a  select   attribute , which takes an XPath pointing to a specific element or group of elements within the XML document. In this case, the two  <xsl:value-of /> tags point to  firstname   and  lastname   elements, indicating that they should be output in the  title   of the HTML page. The <xsl:text> element is used to create a space between the first name and the last name elements.

<xsl:value-of select="descendant::firstname" />
<xsl:text> </xsl:text>
<xsl:value-of select="descendant::lastname" />

There are then some more HTML tags followed by the same XSLT tags, re-outputting the first and last name of the Beatle in the body of the HTML page.

xsl:template

The  xsl:template   tag is used to tell the XSLT processor what to do when it comes across a matching node. Matches are determined by the XPath expression in the  match   attribute of the  xsl:template   tag.

Code Sample: XsltBasics/Demos/FirstName.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="firstname">
  We found a first name! 
 </xsl:template>

</xsl:stylesheet>
Code Explanation

If we use this XSLT to transform  XsltBasics/Demos/FirstName.xml , which has the same XML asXsltBasics/Demos/Paul.xml   , the result will be:

 We found a first name!
McCartneySingerMale

The text "We found a first name!" shows up only once, because only one match is found . The text "McCartneySingerMale" shows up because the XSLT engine will continue looking through an XML document until it is explicitly told to stop. When it reaches a text node with no matching template, it uses the default template, which simply outputs the text. "McCartney", "Singer" and "Male" are the text node values inside the elements that follow the  FirstName   element.

 

Let's see what happens if we transform an XML document with multiple  FirstName   elements against this same XSLT. Take, for example, the XML document below.

Code Sample: XsltBasics/Demos/Beatles.xml

<?xml version="1.0"?>
<?xml-stylesheet href="FirstName.xsl" type="text/xsl"?>
<beatles>
 <beatle link="http://www.paulmccartney.com">
  <name>
   <firstname>Paul</firstname>
   <lastname>McCartney</lastname>
  </name>
 </beatle>
 <beatle link="http://www.johnlennon.com">
  <name>
   <firstname>John</firstname>
   <lastname>Lennon</lastname>
  </name>
 </beatle>
 <beatle link="http://www.georgeharrison.com">
  <name>
   <firstname>George</firstname>
   <lastname>Harrison</lastname>
  </name>
 </beatle>
 <beatle link="http://www.ringostarr.com">
  <name>
   <firstname>Ringo</firstname>
   <lastname>Starr</lastname>
  </name>
 </beatle>
 <beatle link="http://www.webucator.com" real="no">
  <name>
   <firstname>Nat</firstname>
   <lastname>Dunn</lastname>
  </name>
 </beatle>
</beatles>
Code Explanation

The resulting output will be as follows.

 We found a first name!
McCartney
 We found a first name!
Lennon
 We found a first name!
Harrison
 We found a first name!
Starr
 We found a first name!
Dunn

Each time a  firstname   element is found in the source XML document, the text "We found a first name!" is output. For the other elements with text (in this case, only  lastname   elements), the actual text is output .

xsl:value-of

The  xsl:value-of   element is used to output the text value of a node. To illustrate, let's look at the following example.

Code Sample: XsltBasics/Demos/ValueOf1.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="firstname">
  We found a first name and it's <xsl:value-of select="."/>

 </xsl:template>
</xsl:stylesheet>
Code Explanation

If we use this XSLT to transform  XsltBasics/Demos/Beatles-VO1.xml , which has the same XML asXsltBasics/Demos/Beatles.xml   , the result will be:

 We found a first name and it's Paul
McCartney
 We found a first name and it's John
Lennon
 We found a first name and it's George
Harrison
 We found a first name and it's Ringo
Starr
 We found a first name and it's Nat
Dunn

The  select   attribute takes an XPath, which is used to indicate which node's value to output. A single dot (. ) refers to the current node (i.e, the node that was matched by the template).

Don't let the last names at the end of each line confuse you. These are not part of the output of thexsl:value-of   element. They are there as a result of the default template, which outputs the text value of the element found .

 

To illustrate this, let's look at another example.

Code Sample: XsltBasics/Demos/ValueOf2.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="firstname">
  We found a first name and it's <xsl:value-of select="."/>
 </xsl:template>
 <xsl:template match="lastname">
  We found a last name and it's <xsl:value-of select="."/>
 </xsl:template>

</xsl:stylesheet>
Code Explanation

If we use this XSLT to transform  XsltBasics/Demos/Beatles-VO2.xml , which has the same XML asXsltBasics/Demos/Beatles.xml   , the result will be:

 We found a first name and it's Paul
 We found a last name and it's McCartney
 We found a first name and it's John
 We found a last name and it's Lennon
 We found a first name and it's George
 We found a last name and it's Harrison
 We found a first name and it's Ringo
 We found a last name and it's Starr
 We found a first name and it's Nat
 We found a last name and it's Dunn

This XSLT has a template for both  firstname   and  lastname   and so it never uses the default template.

Whitespace and xsl:text

Whitespace in an XSLT template is output literally. If you're not careful, this can lead to undesirable results. To illustrate, let's look at the following XML and XSLT documents.

Code Sample: XsltBasics/Demos/WhiteSpace1.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="WhiteSpace1.xsl"?>

<example>
 <blurb>
  Hello World!
 </blurb>
</example>

Code Sample: XsltBasics/Demos/WhiteSpace1.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="blurb">
  Literal   Text
 </xsl:template>

</xsl:stylesheet>

When  XsltBasics/Demos/WhiteSpace1.xml   is transformed against  XsltBasics/Demos/WhiteSpace1.xsl , the output looks like this:

There is an empty line before and after the text. There are also two tabs preceding the text. This is because the whitespace between  <xsl:template match="blurb">   and  Literal Text   and the whitespace betweenLiteral Text   and  </xsl:template>   is output literally.

If you did not want that extra whitespace to show up in the output, you could use the following XSLT instead.

Code Sample: XsltBasics/Demos/WhiteSpace2.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="blurb">
  <xsl:text>
Literal   Text</xsl:text>

 </xsl:template>
</xsl:stylesheet>
Code Explanation

When  XsltBasics/Demos/WhiteSpace2.xml , which has the same XML as  XsltBasics/Demos/WhiteSpace1.xml   , is transformed against  XsltBasics/Demos/WhiteSpace2.xsl , the output looks like this:

Because whitespace between open tags (e.g, between  <xsl:template match="blurb">   and<xsl:text> ) and whitespace between close tags (e.g,  </xsl:text>   and  </xsl:template> ) is ignored, the only content that is output is the text between the open and close  xsl:text   tags.

Inserting Whitespace with xsl:text

The examples above illustrate how  xsl:text   can be used to remove whitespace. It can also be used to add whitespace where there otherwise wouldn't be any. Let's take a look at another example.

Code Sample: XsltBasics/Demos/WhiteSpace3.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="name">
  <xsl:value-of select="firstname"/>
  <xsl:value-of select="lastname"/>

 </xsl:template>
</xsl:stylesheet>
Code Explanation

When  XsltBasics/Demos/Beatles-WS3.xml , which has the same XML as  XsltBasics/Demos/Beatles.xml   , is transformed against  XsltBasics/Demos/WhiteSpace3.xsl , the output looks like this:

Clearly, this isn't the desired output. We'd like to have each Beatle on a separate line and spaces between their first and last names like this:

However, because whitespace between two open tags and between two close tags is ignored , we don't get any whitespace in the output. We can use  xsl:text   to fix this by explicitly indicating how much whitespace we want in each location.

Code Sample: XsltBasics/Demos/WhiteSpace4.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="name">
  <xsl:value-of select="firstname"/>
  <xsl:text> </xsl:text>

  <xsl:value-of select="lastname"/>
  <xsl:text>
</xsl:text>

 </xsl:template>
</xsl:stylesheet>
Code Explanation

Notice that the second close  xsl:text   hugs the left border. If we were to put any space before it on that line, that space would show up in the output. To see the desired result, transform  XsltBasics/Demos/Beatles-WS4.xml   against  XsltBasics/Demos/WhiteSpace4.xsl .

Output Types

The type of output is determined by the  method   attribute of the  xsl:output   element , which must be a direct child of the  xsl:stylesheet   element.

Text

<xsl:output method="text"/>

As we have seen in the examples thus far, XSLT can be used to output plain text. However, XSLT is much more commonly used to transform one XML structure into another XML structure or into HTML.

XML

<xsl:output method="xml"/>

The default output method (in most cases) is XML. That is, an XSLT with no  xsl:output   element will output XML. In this case, XSLT tags are intermingled with the output XML tags. Generally, the XSLT tags are prefixed with  xsl: , so it is easy to tell them apart. The output XML tags may also be prefixed, but not with the same prefix as the XSLT tags.

Literal Result Elements

The output XML tags are called literal result elements. That is because they are output literally rather than used by the XSLT to determine what or how something should be output.

Useful xsl:output Attributes When Outputting XML
AttributeDescription
method Should be set to  xml .
indent yes   or  no . If yes, the resulting XML document will be pretty printed. The default is usuallyno .
omit-xml-declaration yes   or  no . If no, the resulting XML document will begin with an XML declaration. The default is usually  no .
version Specifies the XML version of the resulting XML document.

The documents below show how XSLT can be used to output XML.

Code Sample: XsltBasics/Demos/Name.xml

<?xml version="1.0"?>
<?xml-stylesheet href="Name.xsl" type="text/xsl"?>
<person>
 <name>
  <firstname>Paul</firstname>
  <lastname>McCartney</lastname>
 </name>
</person>

Code Sample: XsltBasics/Demos/Name.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" version="1.0"/>

 <xsl:template match="name">
  <Matches>
   <Match>We found a name!</Match>
   <Name><xsl:value-of select="."/></Name>
  </Matches>
 </xsl:template>
</xsl:stylesheet>

Code Sample: XsltBasics/Demos/NameResult.xml

<?xml version="1.0" encoding="UTF-8"?>
<Matches>
 <Match>We found a name!</Match>
 <Name>PaulMcCartney</Name>
</Matches>

HTML

<xsl:output method="html"/>

It is very common to use XSLT to output HTML. In fact, XSLT even has a special provision for HTML: if the document element of the resulting document is html (not case sensitive) then the default method is changed to HTML. However, for the sake of clarity, it is better code to specify the output method with the xsl:output tag.

The documents below show how XSLT can be used to output HTML.

Code Sample: XsltBasics/Demos/NameHTML.xml

<?xml version="1.0"?>
<?xml-stylesheet href="NameHTML.xsl" type="text/xsl"?>
<person>
 <name>
  <firstname>Paul</firstname>
  <lastname>McCartney</lastname>
 </name>
</person>

Code Sample: XsltBasics/Demos/NameHTML.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>

 <xsl:template match="name">
  <html>
   <head>
    <title>We found a name!</title>
   </head>
   <body>
    <h1>
     <xsl:value-of select="firstname"/>
     <xsl:text> </xsl:text>
     <xsl:value-of select="lastname"/>
    </h1>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>
Code Explanation

If you open  XsltBasics/Demos/NameHTML.xml   in Internet Explorer, you'll see the following result.

Elements and Attributes

When outputting XML or HTML, you will need to output tags and attributes. We saw earlier that you can output tags using literal result elements.

xsl:element

The  xsl:element   tag can be used to explicitly specify that an element is for output. You will notice that thexsl:element   tag takes the  name   attribute, which is used to specify the name of the element being output.

Also, as shown,  xsl:element   tags can be nested within each other.

Code Sample: XsltBasics/Demos/Name2.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" version="1.0"/>
 <xsl:template match="name">
  <xsl:element name="Matches">
   <xsl:element name="Match">We found a name!</xsl:element>
   <xsl:element name="Name">
    <xsl:value-of select="."/>
   </xsl:element>
  </xsl:element>

 </xsl:template>
</xsl:stylesheet>

When  XsltBasics/Demos/Name2.xml , which has the same XML as  XsltBasics/Demos/Name.xml   , is transformed against  XsltBasics/Demos/Name2.xsl , the output looks the same as we saw when we used literal result elements earlier in the lesson.

The  xsl:element   tag is most useful when the tag name itself is generated. If the tag name is not generated, it is generally easier to use literal result elements.

xsl:attribute

The  xsl:attribute   element is similar to the  xsl:element   element. It is used to explicitly output an attribute. However, it is more commonly used than  xsl:element . To see why, let's first look at an example in which an attribute is output literally.

Code Sample: XsltBasics/Demos/NameLREwithAtt.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" version="1.0"/>
 <xsl:template match="name">
  <Matches>
   <Match Name="Some Name">We found a name!</Match>
  </Matches>
 </xsl:template>

</xsl:stylesheet>
Code Explanation

Notice that the value of the Name attribute is just "Some Name". We would like to generate this value from the source XML document by doing something like this:

<!--THIS IS POORLY FORMED-->
<Match Name="<xsl:value-of select='.'/>">
 We found a name!
</Match>

Code Sample: XsltBasics/Demos/NameWithAtt.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" version="1.0"/>
 <xsl:template match="name">
  <Matches>
   <Match>
    <xsl:attribute name="Name">
     <xsl:value-of select="firstname"/>
     <xsl:text> </xsl:text>
     <xsl:value-of select="lastname"/>
    </xsl:attribute>
We found a name!</Match>
  </Matches>
 </xsl:template>
</xsl:stylesheet>
Code Explanation

The  xsl:attribute   tag applies to the element that it is nested within; in this case, the  Match   element. When  XsltBasics/Demos/NameWithAtt.xml , which has the same XML as  XSLTBasics/Demos/Name.xml   , is transformed against  XsltBasics/Demos/NameWithAtt.xsl , the output looks like this:

Code Sample: XsltBasics/Demos/NameWithAttResult.xml

<?xml version="1.0" encoding="UTF-8"?>
<Matches>
 <Match Name="Paul McCartney">We found a name!</Match>
</Matches>

Attributes and Curly Brackets

Because using  xsl:attribute   can be a bit laborious, an abbreviated syntax is available. An XPath within curly brackets ({} ) can be embedded as the value of an attribute in the open tag. The following example illustrates this.

Code Sample: XsltBasics/Demos/NameWithAttAbbr.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" version="1.0"/>
 <xsl:template match="name">
  <Matches>
   <Match Name="{firstname} {lastname}
">We found a name!</Match>
  </Matches>
 </xsl:template>
</xsl:stylesheet>
Code Explanation

This will have the exact same result as  XsltBasics/Demos/NameWithAtt.xsl   , but it is much quicker and easier to write. To see the result, transform  XsltBasics/Demos/NameWithAttAbbr.xml   against this XSLT.

XSLT Basics Conclusion

In this lesson of the XML tutorial, you have learned the basics of creating XSLTs to transform XML instances into text, HTML and other XML structures. You are now ready to learn more advanced features of XSLT.

To continue to learn XML go to the   top of this page   and click on the next lesson in this XML Tutorial's Table of Contents.

内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
本次的学生体质健康信息管理网站,按照用户的角色可以分为教师与学生,后台设置管理员角色来对学生的信息进行管理。,设计如下: 1、后台管理系统 后台管理系统主要是为该系统的管理员提供信息管理服务的系统,具体包括的功能模块如下: (1)管理员信息管理 (2)教师信息管理 (3)学生信息管理 (4)健康信息统计(图形化进行健康,亚健康等学生的信息数量统计) 2、教师角色的功能模块设计 教师角色所需要的功能模块主要包括了如下的一些内容: (1)个人资料修改 (2)学生体质健康管理:录入相关数据,包括但不限于身高、体重、肺活量、视力等生理指标以及运动能力、身体成分、骨密度等健康指标,并且设置健康,亚健康状态 (3)学生健康建议:根据体质信息,进行学生健康的建议 (4)健康预警:对健康出问题的学生,进行健康预警 (5)饮食和锻炼情况管理,查看 3、学生角色 学生角色可以通过该信息网站看到个人的基本信息,能够看到教师给与学生的健康建议等,功能模块设计如下: (1)个人资料修改 (2)我的健康建议查看 (3)我的健康预警 (4)饮食和锻炼情况管理,记录平时的饮食和锻炼情况 完整前后端源码,部署后可正常运行! 环境说明 开发语言:Java后端 框架:ssm,mybatis JDK版本:JDK1.8+ 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:eclipse/idea Maven包:Maven3.3+ 部署容器:tomcat7.5+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值