知方可补不足~用xsl来修饰xml

时间:2020-11-27 08:42:18

概念相关

XSL是可扩展样式表语言的外语缩写,是一种用于以可读格式呈现 XML(标准通用标记语言的子集)数据的语言。

起始于 XSL

万维网联盟(W3C)开始发展 XSL 的原因是:存在着对于基于 XML 的样式表语言的需求。

CSS = HTML 样式表

HTML 使用预先定义的标签,每个标签的意义很容易被理解。
HTML 中的 <table> 标签定义表格 - 并且浏览器清楚如何显示它。
向 HTML 元素添加样式是很容易的。通过 CSS,很容易告知浏览器用特定的字体或颜色显示一个元素。

XSL = XML 样式表

XML 不使用预先定义的标签(我们可以使用任何喜欢的标签名),并且每个标签的意义并不都那么容易被理解。
<table> 标签意味着一个 HTML 表格,一件家具,或是别的什么东西 - 浏览器不清楚如何显示它。
XSL 可描述如何来显示 XML 文档!

XSL - 不仅仅是样式表语言

XSL 包括三部分:

XSLT - 一种用于转换 XML 文档的语言。
    XPath - 一种用于在 XML 文档中导航的语言。
    XSL-FO - 一种用于格式化 XML 文档的语言。

XSL-主要关键字

<xsl:template> 元素用于构建模板
<xsl:value-of> 元素用于提取某个选定节点的值,并把值添加到转换的输出流中
<xsl:for-each> 元素可用于选取指定的节点集中的每个 XML 元素
<xsl:sort>如需对结果进行排序,只要简单地在 XSL 文件中的 <xsl:for-each> 元素内部添加一个 <xsl:sort> 元素
<xsl:if> 如需放置针对 XML 文件内容的条件测试,请向 XSL 文档添加 <xsl:if> 元素。
<xsl:choose> 元素用于结合 <xsl:when> 和 <xsl:otherwise> 来表达多重条件测试。
<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点
<xsl:when expression> 相当前于一种判断,表示当expression成立时执行的代码
<xsl:otherwise expression> 它与<xsl:when>一起使用,表示当when条件不成立时,执行的代码

代码相关

xml文件内容

<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="templete.xsl" ?>
<root>
<food>
<name>鸡蛋</name>
<price>$5</price>
<info>很有营养的</info>
<unit>2</unit>
</food>
<food>
<name>煎饼</name>
<price>$4.5</price>
<info>早餐必备</info>
<unit>1</unit>
</food>
<food>
<name>皮蛋粥</name>
<price>$2</price>
<info>我的最爱</info>
<unit>1</unit>
</food>
</root>

xsl文件内容

<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<BODY STYLE="font-family:微软雅黑; font-size:12pt;background-color:#fff">
<xsl:for-each select="root/food">
<DIV STYLE="background-color:#eeaaaa; color:white; padding:4px">
<SPAN STYLE="font-weight:bold; color:white">
<xsl:value-of select="name"/>
</SPAN>
<em>
<xsl:value-of select="price"/>
</em>
</DIV>
<DIV>
<xsl:value-of select="info"/>
<SPAN STYLE="font-style:italic">
(<xsl:value-of select="count" />份)
</SPAN>
</DIV>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

程序截图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfkAAACkCAIAAAAbs8AuAAAYfElEQVR4nO2dP2zbaJqHWapJqcbNAUaaIE2KARYqBioGCFIErgYLTGFs4WYQ4MBr0kyaVbHNAq5SLAwsm0mxASFtkUKFg9g4ZhKMfKPJZc63TiCIOxmdHW2ISEqoPzRF8r2C/z6SHyXKsSOb/j14gI3Ij6SsBX7fq5cfNQIBAADIO0Lajp4sQwghzIfIegghzL/IegghzL/IegghzL/IegghzL/IegghzL/IegghzL/IegghzL+fmvWGTkREuhrZruppp01iq2knP7As3agt/zOCEMKLbqasN+LRrRsNzeEexoa+O8bd4o3XDea0Q83gZ/2jH4/7Y8dyiIgsw3q9HezSu5P4VNF9uvQPEUIIz7kL1vVuZBvaMPiHrNpugrPJLss9P8odrREeyGJoUz2e/j1Z7m2rtuU4vVfHg6nd2R7sHdrDtx+9vbvmmGgcvIQQQpjJjFnfmBrGtOE3Z5isN4JAb2hOkOyy3JNVm630eXU9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