在xsl:apply-templates标记

时间:2015-05-13 00:50:47

标签: html xml xslt

我是xslt的新手,我很难习惯它的xsl:apply-templates元素。我有一个简单的xml文件,我想在它的元素上应用XSL样式。我想选择每一个我的XML文件中的入口元素,并在屏幕上显示它的标题子。我从我的XSL文件中提取该部分,这是我的困惑。

<xsl:template match='/'>
<html>
  <head>
     <title>my xsl file</title>
  </head>
  <body>
    <h2>my book collection</h2>
      <xsl:apply-templates select='entry'/>
  </body>
</html>
</xsl:template>

在xsl:apply-templates标签的上述片段中,如果我使用select属性,屏幕上不显示任何内容。但如果我删除它一切都很好。我的问题是为什么是?我不应该选择和匹配entry tag。如下所示

<xsl:template match='entry'>
   <p>
       <xsl:apply-templates select='title'/>

   </p>
</xsl:template>

这里我必须"select" the "title" tag形成every entry然后必须为“title”标签进行模板匹配。如下所示。前一个片段选择标题标签,下面的片段与之匹配并创建一个h2标签及其内容。 那么为什么我们不能为标题标签的父标记条目标记做同样的事情?

<xsl:template match='title'>

  <h2 style='color:red;'><xsl:value-of select="."/></h2>

</xsl:template>

完整代码: XML文件:

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='haha.xslt'?>
<book>
   <entry>
       <title>amar boi</title>
       <page>100</page>
   </entry>
   <entry>
       <title>adhunik biggan</title>
       <page>200</page>
   </entry>
   <entry>
       <title>machine design</title>
       <page>1000</page>
   </entry>
</book>

XSL文件:

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

<xsl:template match='/'>
<html>
  <head>
     <title>my xsl file</title>
  </head>
  <body>
    <h2>my book collection</h2>
      <xsl:apply-templates select='entry'/>
  </body>
</html>
</xsl:template>
<xsl:template match='entry'>
   <p>
       <xsl:apply-templates select='title'/>

   </p>
</xsl:template>

<xsl:template match='title'>

  <h2 style='color:red;'><xsl:value-of select="."/></h2>

</xsl:template>


</xsl:stylesheet>

2 个答案:

答案 0 :(得分:3)

  

如果我使用select,请在xsl:apply-templates标签的上述代码段中   属性,屏幕上没有显示任何内容。但是,如果我删除它   一切都很好。我的问题是为什么会这样?

原因是您处于/根节点的上下文中(这是您的模板匹配的),而您的<xsl:apply-templates/>正在选择“条目” - 这是“的缩写”孩子::条目”。但是,entry不是/的孩子,因此您的表达式不会选择任何内容。

如果删除选择,则模板将应用于作为当前节点的子节点的节点(示例中为book)。然后,built-in template rule会将模板应用于book的子项,这就是最终应用与entry匹配的模板的方式。

只需将第一个模板的开始标记更改为:

即可避免此问题
<xsl:template match='/book'>

答案 1 :(得分:3)

根节点/与文档元素/*(在您的情况下为/book)不同。

在与根节点(xsl:template match="/")匹配的模板中,您正在使用xsl:apply-templates select="entry"/>,这相当于/entry,并且碰巧没有选择任何内容。

如果您要将模板应用于entry元素,则可以更改第一个模板以匹配文档元素(@michael.hor257k recommends),或者您可以调整根节点模板中的apply-templates的XPath为:xsl:apply-templates select="book/entry",甚至是*/entry"

完整示例:

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

    <xsl:template match='/'>
        <html>
            <head>
                <title>my xsl file</title>
            </head>
            <body>
                <h2>my book collection</h2>
                <xsl:apply-templates select='book/entry'/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match='entry'>
        <p>
            <xsl:apply-templates select='title'/>      
        </p>
    </xsl:template>

    <xsl:template match="title">
        <h2 style="color:red;">
            <xsl:value-of select="."/>
        </h2>
    </xsl:template>

</xsl:stylesheet>