【问题标题】:Xpath xslt retrieving nodes?Xpath xslt 检索节点?
【发布时间】:2013-10-12 12:38:31
【问题描述】:

我是 XML 新手,我似乎无法理解这个简单的问题...我正在检索一个包含节点 ID、品牌和型号的表...代码运行良好...但是在表头和表体之间,它还检索节点“john doe”。我猜 XPath 表达式有问题。

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>

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    只需将第二个所有模板移至 apply-templates 并进行如下更改:

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

    或其他解决方案将选择添加到应用模板:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2010-11-04
      相关资源
      最近更新 更多