簡體   English   中英

正確使用xpath作為xsl:apply-templates標記中的“ select =”屬性

[英]proper use of xpath for “select=” attribute in xsl:apply-templates tag

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

在這里,我必須從every entry "select" the "title" tag然后必須使"select" the "title" tag與模板匹配。類似於以下內容。Previoussnippent選擇title標記,以下snippet對其進行匹配並創建一個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>

在上面的xsl:apply-templates標記中,如果我使用select屬性,則屏幕上不會顯示任何內容。但是如果我刪除它,一切都很好。我的問題是為什么?

原因是您位於/根節點的上下文中(這是您的模板所匹配的),並且您的<xsl:apply-templates/>選擇了“ entry”-這是“ child :: entry”的縮寫。 ”。 但是, entry不是/的子級,因此您的表達式不選擇任何內容。

如果刪除選擇,則模板將應用於當前節點的子節點(示例中的book )。 然后, 內置模板規則將模板應用於book的子代, book就是最終應用模板匹配entry方式。

您只需將第一個模板的開始標簽更改為:

<xsl:template match='/book'>

根節點/與文檔元素/* (在您的情況下/book )不同。

在與根節點匹配的模板( xsl:template match="/" )中,您正在使用xsl:apply-templates select="entry"/> ,它等效於/entry並且什么也沒選擇。

如果要對entry元素應用模板,則可以更改第一個模板以匹配文檔元素(如@ michael.hor257k 建議 ),或者可以將根節點模板中的應用模板的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM