XSL中template的match属性匹配模式

本文详细介绍了XSL中各种匹配模式的应用,包括根节点、元素名、子节点、后代节点等10种常见匹配方式,并通过一个具体示例展示了如何使用这些匹配模式进行XML文档的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出处:http://www.cnblogs.com/ygcao/archive/2010/05/23/1742247.html

匹配模式

1.匹配根节点

<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
2.匹配元素名

<xsl:template match="films">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
3.匹配子节点

<xsl:template match="file/name">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
除了使用“/”,还可以使用“*”来匹配任意元素。在下面的程序中匹配了“film”元素的所有子节点的“name”元素。
<xsl:template match="film/*/name">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
4.匹配元素后代

<xsl:template match="film//name">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>

在程序中指定的“film//name”, 包括了“film/name”,“film/*/name”,“film/*/*/name”等各种情况。对于选择给定类型的所有元素而不管这些元素是不是直系子孙时,这种方法会特别的有效。

5.匹配属性

<xsl:template match="films/film">
<p>
<xsl:value-of select="@country"/>
</p>
</xsl:template>

匹配属性的机制和匹配元素的机制十分的类似,只是多了一个“@”符号而已。

就像使用“*”来选择一个元素的所有属性,例如,“film/@*”表示选择“film”元素的所有属性。

6.通过ID匹配

<xsl:template match="id('A101')">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
7.匹配文本节点
<xsl:template match="name">
<xsl:value-of select="text()"/>
</xsl:template>
XSL缺省规则规定,如果没有规则用于处理这一个文本节点,这个节点的文本将直接被输出,可以使用text()来推翻这一个缺省规则,让处理器对文本节点什么都不做。如下所示:
<xsl:template match="text()">
</xsl:template>

8.匹配注释

使用comment()来选择注释,例如:

<?xml version="1.0" encoding="gb2312"?>
<film>
    <!--目前只有VCD版, DVD版还未上市-->
    <name>拯救大兵瑞恩</name>
    <director>施皮尔伯格</director>
</film>
 
<!--xsl-->
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3g.org/1999/XSL/Transform">
<xsl:template match="film">
    <xsl:apply-templates select="comment()"/>
<xsl:value-of select="name"/>
<xsl:value-of select="director"/>
</xsl:template>
<xsl:template>
    <xsl:comment>
        <xsl:value-of select="."/>
    </xsl:comment>
</xsl:template>

9.匹配操作指令

使用processing-instruction()来匹配操作指令,缩写为“pi()”,有时想要将样式单附加到另一个文档上,这是“pi()”就可以派上用场。

<xsl:template match="/pi()">
<xsl:pi name="xml-stylesheet">
    <xsl:value-of select="."/>
</xsl:pi>
</xsl:template>

10.使用“或”操作符

在处理多个节点时,使用“或”操作符可以匹配多种可能的模式。

<xsl:template match="name|director">
<xsl:value-of select=".">
</xsl:template>

程序中同时选择了“name”和“director”2个元素,并对他们使用同样的规则。

实例:

xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="MyFirstXsl.xslt"?>
<root>
    <music date="2010">
        <title>传奇</title>
        <signer>王菲</signer>
        <舞台>
            <演员>路人甲</演员>
            <代表作>路人甲的代表作</代表作>
        </舞台>
        <舞台>
            <演员>路人乙</演员>
            <代表作>路人乙的代表作</代表作>
        </舞台>
        <lyric>我一直在你身旁,从未走远...</lyric>  
        <!--在2010年春节联欢晚会上演唱-->
    </music>
    <music date="2009">
        <title>梁祝</title>
        <signer>sweety</signer>
        <lyric>沉默了七世纪,没放弃逆转这宿命...</lyric>
    </music>
    <music date="2009">
        <title>幸福毛毛虫</title>
        <signer>sweety</signer>
        <lyric>你的出现改变我原来的想象...</lyric>
        <!--很受欢迎-->
    </music>
</root>
xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>
 
    <xsl:template match="/">
        <p align="center">
            <font size="7">流行歌曲</font>
        </p>
        <table width="95%" border="1">
            <tr>
                <td width="15%">
                    <div align="center">歌曲</div>
                </td>
                <td width="10%">
                    <div align="center">演唱者</div>
                </td>
                <td width="30%">
                    <div align="center">伴唱</div>
                </td>
                <td width="20%">
                    <div align="center">歌词概览</div>
                </td>
                <td width="10%">
                    <div align="center">时间</div>
                </td>
                <td width="10%">
                    <div align="center">备注</div>
                </td>
            </tr>
            <xsl:apply-templates/>
        </table>
    </xsl:template>
 
    <!--注释的匹配规则-->
    <xsl:template match="root/music/comment()">
        <xsl:value-of select="."/>
    </xsl:template>
     
    <!--演唱者的详细介绍-->
    <xsl:template match="舞台">
        <xsl:value-of select="演员"/>(
        <xsl:value-of select="代表作"/>)
        <br/>
    </xsl:template>
 
    <xsl:template match="root/music">
        <tr>
            <td>
                <div align="center">
                    <strong>
                        <font size="5">
                            <xsl:value-of select="title"/>
                        </font>
                    </strong>
                </div>
            </td>
 
            <td>
                <div align="center">
                    <strong>
                        <font size="5">
                            <xsl:value-of select="signer"/>
                        </font>
                    </strong>
                </div>
            </td>
 
            <td>
                <div align="center">
                    <strong>
                        <font size="5">
                            <xsl:apply-templates select="舞台"/>
                        </font>
                    </strong>
                </div>
            </td>
 
            <td>
                <div align="center">
                    <strong>
                        <font size="5">
                            <xsl:value-of select="lyric"/>
                        </font>
                    </strong>
                </div>
            </td>
 
            <td>
                <div align="center">
                    <strong>
                        <font size="5">
                            <xsl:value-of select="@date"/>
                        </font>
                    </strong>
                </div>
            </td>
             
            <!--匹配注释-->
            <td>
                <div align="center">
                    <strong>
                        <font size="5">
                            <xsl:apply-templates select="comment()"/>
                        </font>
                    </strong>
                </div>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>
显示效果:




<?xml version=“1.0” encoding=“UTF-8”?> <xsl:stylesheet version=“3.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” xmlns:fo=“http://www.w3.org/1999/XSL/Format” xmlns:w=“http://schemas.openxmlformats.org/wordprocessingml/2006/main” exclude-result-prefixes=“w”> <!-- 根模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <fo:region-body margin="1in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates select="//w:body"/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <!-- 段落处理 --> <xsl:template match="w:p"> <fo:block xsl:use-attribute-sets="paragraph-style"> <xsl:apply-templates select=".//w:r"/> </fo:block> </xsl:template> <!-- 文本格式处理 --> <xsl:template match="w:r"> <fo:inline> <xsl:apply-templates select="w:rPr"/> <xsl:value-of select="w:t"/> </fo:inline> </xsl:template> <!-- 字体样式提取 --> <xsl:template match="w:rPr"> <xsl:if test="w:rFonts/@w:ascii"> <xsl:attribute name="font-family" select="w:rFonts/@w:ascii"/> </xsl:if> <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size" select="concat(w:sz/@w:val * 0.5, 'pt')"/> </xsl:if> <xsl:if test="w:color/@w:val"> <xsl:attribute name="color" select="concat('#', w:color/@w:val)"/> </xsl:if> </xsl:template> <!-- 加粗处理 --> <xsl:template match="w:b"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:template> <!-- 斜体处理 --> <xsl:template match="w:i"> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:template> <!-- 表格处理 --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%"> <xsl:apply-templates select="w:tr"/> </fo:table> </xsl:template> <xsl:template match="w:tr"> <fo:table-row> <xsl:apply-templates select="w:tc"/> </fo:table-row> </xsl:template> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt"> <fo:block> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 段落默认样式 --> <xsl:attribute-set name="paragraph-style"> <xsl:attribute name="space-after">12pt</xsl:attribute> <xsl:attribute name="text-align">left</xsl:attribute> </xsl:attribute-set> </xsl:stylesheet>完善修改这个xslt样式表,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
03-11
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" exclude-result-prefixes="w"> <!-- 根模板 --> <xsl:template match="/"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="A4"> <fo:region-body margin="1in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates select="//w:body"/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <!-- 段落处理 --> <xsl:template match="w:p"> <fo:block xsl:use-attribute-sets="paragraph-style"> <xsl:apply-templates select=".//w:r"/> </fo:block> </xsl:template> <!-- 文本格式处理 --> <xsl:template match="w:r"> <fo:inline> <xsl:apply-templates select="w:rPr"/> <xsl:value-of select="w:t"/> </fo:inline> </xsl:template> <!-- 字体样式提取 --> <xsl:template match="w:rPr"> <xsl:if test="w:rFonts/@w:ascii"> <xsl:attribute name="font-family" select="w:rFonts/@w:ascii"/> </xsl:if> <xsl:if test="w:sz/@w:val"> <xsl:attribute name="font-size" select="concat(w:sz/@w:val * 0.5, 'pt')"/> </xsl:if> <xsl:if test="w:color/@w:val"> <xsl:attribute name="color" select="concat('#', w:color/@w:val)"/> </xsl:if> </xsl:template> <!-- 加粗处理 --> <xsl:template match="w:b"> <xsl:attribute name="font-weight">bold</xsl:attribute> </xsl:template> <!-- 斜体处理 --> <xsl:template match="w:i"> <xsl:attribute name="font-style">italic</xsl:attribute> </xsl:template> <!-- 表格处理 --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%"> <xsl:apply-templates select="w:tr"/> </fo:table> </xsl:template> <xsl:template match="w:tr"> <fo:table-row> <xsl:apply-templates select="w:tc"/> </fo:table-row> </xsl:template> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt"> <fo:block> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template> <!-- 段落默认样式 --> <xsl:attribute-set name="paragraph-style"> <xsl:attribute name="space-after">12pt</xsl:attribute> <xsl:attribute name="text-align">left</xsl:attribute> </xsl:attribute-set> </xsl:stylesheet>完善修改这个xslt样式表,一定要注意这是word的xml转xls-fo的xlst样式表,且xslt版本为3.0,请直接给出修改好的全部结果
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值