正确使用 XSL:应用模板标记中"select="属性的 xPATH



我是XSLT的新手,我很难适应其XSL:apply-templates element。它的元素。我想从我的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"标签进行模板匹配。> 那么,为什么我们不能为入口标签做同样的事情哪个是标题标签的父母?

<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>

在XSL中的上述片段中:如果我使用SELECT,请使用标签 属性,屏幕上没有显示内容。但是如果我删除它 一切都很好。我的问题是为什么?

的原因是您处于/根节点的上下文(这就是模板匹配的内容),而您的<xsl:apply-templates/>正在选择"条目" - 这是" Child :: entry"的缩写。但是,entry不是/的孩子,因此您的表达式一无所获。

如果删除选择,则将模板应用于当前节点的子节点(示例中的book)。然后,内置的模板规则将模板应用于book的子女,这就是您的模板匹配entry的方式。

您只需将第一个模板的启动标签更改为:

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

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

在匹配根节点(xsl:template match="/")的模板中,您使用的是xsl:apply-templates select="entry"/>,它等效于/entry,恰好选择什么都不选择。

如果要将网络应用于entry元素,则可以更改第一个模板以匹配文档元素(如 @Michael.hor257k建议),或者您可以调整root中的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>

最新更新