使用XSL从xml目录中仅提取bob dylan cd

时间:2013-11-29 15:22:36

标签: xml xslt xslt-1.0

您好,我是XML& amp;的新手我想要做的是能够采取CD示例,其中目录中的各种艺术家有许多CD。然后我希望能够提取所有bob dylan cds并将它们显示在一个表中,而忽略目录中其余的cds。

到目前为止,我已经能够提取所有的CD并在表格中显示艺术家,专辑,公司,国家,年等,但到目前为止还无法弄清楚如何解决这个问题。我已将XSL包含在&也是我试图从中提取信息的xml。我希望我提供了足够的信息!谢谢你的帮助......

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
<cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
</cd>
<cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
</cd>
<cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
</cd>
 </catalog>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>


<xsl:variable name="XSLTEST" select="catalog/cd"/>  
<tr>
    count:<xsl:value-of select= "count($XSLTEST)"/>  
</tr>

<body>

 <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
      <th>country</th>
      <th>Company</th>
      <th>price</th>
      <th>year</th>
    </tr>



 <xsl:for-each select="$XSLTEST">       

   <!--<xsl:sort select="Bee Gees"/>-->

   <xsl:variable name="XSLTEST101" select="."/>                         
   <xsl:variable name="this_title" select="$XSLTEST101/title"/>
   <xsl:variable name="this_artist" select="$XSLTEST101/artist"/>       
   <xsl:variable name="this_country" select="$XSLTEST101/country"/>
   <xsl:variable name="this_company" select="$XSLTEST101/company"/>    
   <xsl:variable name="this_price" select="$XSLTEST101/price"/>         
   <xsl:variable name="this_year" select="$XSLTEST101/year"/>

  <tr>
    <td><xsl:value-of select="$this_title"/></td>
    <td><xsl:value-of select="$this_artist"/></td>      
    <td><xsl:value-of select="$this_country"/></td>
    <td><xsl:value-of select="$this_company"/></td>
    <td><xsl:value-of select="$this_price"/></td>
    <td><xsl:value-of select="$this_year"/></td>
  </tr>
</xsl:for-each select>


</table>

 </body>
 </html>

</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

只有当Bob Dylan这样参与时才选择cds:

<xsl:variable name="dylan-cds" select="catalog/cd[artist='Bob Dylan']"/>  

注意:

  • 我简化了样式表并删除了许多显然不必要的变量。相反,我直接选择了需要的值。
  • 最后,我假设你想要制作HTML。即使XSLT处理没有产生任何错误,独立的<tr>元素也不被视为最佳实践。

以下是整个样式表

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">

  <html>

  <xsl:variable name="dylan-cds" select="catalog/cd[artist='Bob Dylan']"/>  
  <tr>
  count:<xsl:value-of select= "count($dylan-cds)"/>  
  </tr>

  <body>
     <h2>My CD Collection</h2>
     <table border="1">
        <tr bgcolor="#9acd32">
           <th>Title</th>
           <th>Artist</th>
           <th>country</th>
           <th>Company</th>
           <th>price</th>
           <th>year</th>
        </tr>

        <xsl:for-each select="$dylan-cds">       
           <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="artist"/></td>      
            <td><xsl:value-of select="country"/></td>
            <td><xsl:value-of select="company"/></td>
            <td><xsl:value-of select="price"/></td>
            <td><xsl:value-of select="year"/></td>
           </tr>
        </xsl:for-each>
     </table>
  </body>
  </html>

</xsl:template>
</xsl:stylesheet>

你得到的输出

<?xml version="1.0" encoding="UTF-8"?>
<html>
 <tr>
  count:1</tr>
 <body>
  <h2>My CD Collection</h2>
  <table border="1">
     <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
        <th>country</th>
        <th>Company</th>
        <th>price</th>
        <th>year</th>
     </tr>
     <tr>
        <td>Empire Burlesque</td>
        <td>Bob Dylan</td>
        <td>USA</td>
        <td>Columbia</td>
        <td>10.90</td>
        <td>1985</td>
     </tr>
  </table>
 </body>
</html>