2013-10-12 107 views
1

我是新來的XML,我似乎無法理解這個簡單的問題...我正在檢索一個表與節點ID,使模型...代碼工作正常......但表頭和身體,它也檢索節點「john doe」..我猜想XPath表達式有問題。Xpath xslt檢索節點?

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="lab1.xsl"?> 
<labs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="lab.xsd"> 
    <lab name="FOA2A"> 
     <responsible> 
     <name>John</name> 
     <surname>Doe</surname> 
     </responsible> 
     <computers> 
     <computer> 
      <id>C001</id> 
      <cpu> 
       <make>Intel</make> 
       <model>Core2duo</model> 
       <clockspeed>2.4</clockspeed> 
      </cpu> 
      <monitor> 
       <type>LCD</type> 
       <size>17"</size> 
      </monitor> 
     </computer> 
     <computer> 
      <id>C002</id> 
      <cpu> 
       <make>AMD</make> 
       <model>Quad</model> 
       <clockspeed>3.0</clockspeed> 
      </cpu> 
      <monitor> 
       <type>Plasma</type> 
       <size>23"</size> 
      </monitor> 
     </computer> 
     <computer> 
      <id>C003</id> 
      <cpu> 
       <make>AMD</make> 
       <model>DoubleQuad</model> 
       <clockspeed>4.0</clockspeed> 
      </cpu> 
      <monitor> 
       <type>LED</type> 
       <size>32"</size> 
      </monitor> 
     </computer> 
     </computers> 
    </lab> 
</labs> 

XSLT:

<?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>TEST</head> 
     <body> 
      <table border="1"> 
       <thead> 
        <tr> 
        <td>Computer ID</td> 
        <td>CPU</td> 
        <td>Monitor</td> 
        </tr> 
       </thead> 
       <tbody> 
        <xsl:apply-templates /> 
       </tbody> 
      </table> 
     </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="labs/lab/computers"> 
     <xsl:for-each select="computer"> 
     <tr> 
      <td> 
       <xsl:value-of select="id/text()" /> 
      </td> 
      <td> 
       <xsl:value-of select="cpu/make/text()" /> 
      </td> 
      <td> 
       <xsl:value-of select="cpu/model/text()" /> 
      </td> 
     </tr> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

回答

1

只需推動第二所有的模板應用模板,改變有點像這樣:

<?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>TEST</head> 
     <body> 
      <table border="1"> 
       <thead> 
        <tr> 
        <td>Computer ID</td> 
        <td>CPU</td> 
        <td>Monitor</td> 
        </tr> 
       </thead> 
       <tbody> 
        <xsl:for-each select="labs/lab/computers/computer"> 
        <tr> 
         <td> 
          <xsl:value-of select="id/text()" /> 
         </td> 
         <td> 
          <xsl:value-of select="cpu/make/text()" /> 
         </td> 
         <td> 
          <xsl:value-of select="cpu/model/text()" /> 
         </td> 
        </tr> 
        </xsl:for-each> 
       </tbody> 
      </table> 
     </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

或者另一個解決方案加上select申請模板:

<xsl:apply-templates select="labs/lab/computers"/> 
+0

謝謝,現在工作:) – user2228135