一个关于xslt的模板匹配规则的问题

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

一个关于xslt的模板匹配规则的问题

问题出处: http://expert.csdn.net/Expert/topic/2549/2549396.xml?temp=.5212061

问题描述:

***************源文件:test.xml****************

<?xml version="1.0"?>

<books>

<book>

<title id="toutou">极限编程研究</title>

<price>70</price>

<author>Giancarrio Succi/Michele Marchesi/张辉(译)</author>

</book>

</books>

***************转换文件:test.xsl***************

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

<xsl:output method="html"/>

<xsl:template match="/books/book/price">

dd

</xsl:template>

</xsl:stylesheet>

输出:

极限编程研究 dd Giancarrio Succi/Michele Marchesi/张辉(译)

而不是我想要的dd

如果match=/books/book/*,就是dd dd dd

这是为什么,请说得清楚一点

解决方法以及祥解

首先让我们来熟悉一下xsl的工作流程.

Xsl的解析器开始工作的时候总是先查找文档的根(root)节点,也就是<xsl:template match=”/”>……<xsl: template>.几乎每个xsl文档都会用到这个模板.

xsl解析器首先找文档的根也就是 <xsl:template match="/">,如果你不写,它会用它内置的模板,这个模板的主要作用就是把文档中的所有元素的内容按你在xml文档的中的顺序输出.在输出过程中它还会查找你已经为节点提供的模板,如果找到它就会使用这个模板,就不使用它内置的了.

上面的话看着似乎有点迷糊,接下来,我们来看看”问题描述”中的xsl文档输出的流程,来让我们更好地理解xsl的工作流程.

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

<xsl:output method="html"/>

<xsl:template match="/books/book/price">

dd

</xsl:template>

</xsl:stylesheet>

对于上面的代码解析器的工作流程是这样的:

1. 寻找<xsl:template match=”/”>……</xsl:template>.这里没有提供,所以解析器就用它内置的.

2. 寻找<xsl:template match=”books”>……</xsl:template>.这里也没有提供.如果在books元素开始结束标记之间其他元素开始结束标记之外有文本的话,它就会输出.这里没有,所有什么也不输出.例如:

<books>死就死吧

<book>

<title id="toutou">极限编程研究</title>

<price>70</price>

<author>Giancarrio Succi/Michele Marchesi/张辉(译)</author>

</book>

</books>

如果没有为books节点提供模板,解析器在处理books节点的时候就会输出”死就死吧”

3. 寻找<xsl:template match=”books/book”>……</xsl:template>.同上.

4. 寻找<xsl:template match=”books/book/title”>……</xsl:template>.这里没有提供,但是在开始和结束标记之间有文本”极限编程研究”,所有解析器就把它踢出来于世人见面了.

5. 寻找<xsl:template match=”books/book/price”>……</xsl:template>.这里为这个节点提供了模板,解析器就使用了提供的模板,输出”dd”.

6. 寻找<xsl:template match=” books/book/author”>……</xsl:template>.这里没有提供模板,所有” Giancarrio Succi/Michele Marchesi/张辉(译)”就被踢了出来.

最后输出的结果就是:

极限编程研究 dd Giancarrio Succi/Michele Marchesi/张辉(译)

下面来看看问题提出者的第二个问题: 如果match=/books/book/*,就是dd dd dd.

把更改后的代码写出来:

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

<xsl:output method="html"/>

<xsl:template match="/books/book/*">

dd

</xsl:template>

</xsl:stylesheet>

<xsl:template match="/books/book/*">这段代码的意思是匹配books/book下的所有节点.通过xml文件可以看到book下有三个节点.全部的代码应该是:

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

<xsl:output method="html"/>

<xsl:template match="/books/book/title">

dd

</xsl:template>

<xsl:template match="/books/book/price">

dd

</xsl:template>

<xsl:template match="/books/book/autor">

dd

</xsl:template>

</xsl:stylesheet>

这样处理器在处理相应的节点的时候就会调用提供的模板.所以输出的结果就会是你看到的那样了.

问题提出者想要的结果是dd.我们应该怎么做?

下面是几个解决方法:

第一种,提供一个匹配文档根节点的模板,覆盖内置的模板.

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

<xsl:output method="html"/>

<xsl:template match=”/”>

<xsl:apply-template select=”/books/book/price”/>

<xsl:template

<xsl:template match="/books/book/price">

dd

</xsl:template>

</xsl:stylesheet>

第二种,不提供匹配文档根节点的模板,但提供一个匹配一级节点的模板.

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

<xsl:output method="html"/>

<xsl:template match=”/books”>

<xsl:apply-template select=”/books/book/price”/>

<xsl:template

<xsl:template match="/books/book/price">

dd

</xsl:template>

</xsl:stylesheet>

还有好多种变化,请大家自己思考.

题外话:

大家可以看到xsl是通过对xml文件内容的选取来显示内容的,它的工作方式让人有点手足无措.当然,它也可以显示与xml文件内容毫无关系的内容.确切地说,xsl是个让你实现挂羊头卖狗肉这一特殊技能的好工具.下面是一段xls代码,实现了挂羊头卖狗肉的功能.

<?xml version="1.0" encoding="gb2312"?>

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

<xsl:output method="html"/>

<xsl:template match="/">

<strong>书名:</strong><xsl:apply-templates select="/books/book/title"/>,

<strong>价格:</strong><xsl:apply-templates select="/books/book/price"/>,

<strong>作者:</strong><xsl:apply-templates select="/books/book/author"/>

</xsl:template>

<xsl:template match="/books/book/price">

给钱也不卖!

</xsl:template>

<xsl:template match="/books/book/author">

亡害危

</xsl:template>

<xsl:template match="/books/book/title">

极度变态烟酒!

</xsl:template>

</xsl:stylesheet>

这是我的第一篇文章.在csdn上像个流浪汉似的逛了两年多了,从这里学习到了很多东西.但从来没有发文章.我在论坛中看到了提问者的这个问题,这好是我当初学习xml所遇到难处,故写此文,希望能帮助初学者度过难关.以免象我那样掉入”粪坑”久难爬出.

另外,希望大家多多提出宝贵的意见和建议,本人无条件接受各位看官的辱骂和诋毁.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航