2013-04-25 128 views
1

对于XSLT样式表,我很新。我已经和他们做了一些非常基础的工作,现在已经得到了一个测试我的能力的项目。我甚至不认为我以正确的方式提出这个问题。XSLT:根据属性选择节点和属性

下面是一组XML数据,代表我正在使用的数据。它的废话数据...

<?xml version="1.0" encoding="utf-8"?> 
<catalog_new> 
    <catalog_old> 
     <catalog_newold> 
      <final> 
       <cd_info title="Empire Burlesque" artist="Dylan" /> 
       <store name="BestBuy" /> 
        <sales thismonth="500"/> 
        <returns thismonth="10"/> 
       <store name="Target" /> 
        <sales thismonth="500"/> 
        <returns thismonth="10"/> 
      </final> 
      <final> 
       <cd_info title="Stand" artist="REM" /> 
       <store name="BestBuy" /> 
        <sales thismonth="1000"/> 
        <returns thismonth="20"/> 
       <store name="Target" /> 
        <sales thismonth="530"/> 
        <returns thismonth="50"/> 
      </final> 
     </catalog_newold> 
    </catalog_old> 
    </catalog_new> 

我需要做的是选择每个最终/ cd_info项目,然后只需选择店里记录下名称=百思买并为每个销售和回报这个月值。

我想产生一个表,看起来像:

CDName   Artist   Store   SalesThisMonth Returns 
Stand   R.E.M   Best Buy   500    10 

这是我到:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes" encoding = "utf-8" standalone = "yes"/> 

    <xsl:template match="/"> 

    <html> 
     <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     </head> 
     <body> 

     <table border="1" style="width: 100%;"> 
      <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      </tr> 

      <xsl:for-each select ="//catalog_new"> 
      <xsl:for-each select="catalog_old"> 
       <xsl:for-each select="catalog_newold"> 
       <xsl:for-each select="final"> 
        <xsl:for-each select="cd_info"> 
        <tr> 
         <td> 
         <xsl:value-of select="@title"/> 
         </td> 
         <td> 
         <xsl:value-of select="@artist"/> 
         </td> 
        </tr> 
        </xsl:for-each> 
       </xsl:for-each> 
       </xsl:for-each> 
      </xsl:for-each> 
      </xsl:for-each> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

我已经结束了删除每次尝试我做了.....我只是偏离轨道,还是我在正确的轨道上?

在此先感谢。

回答

2

这个怎么样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html" indent="yes" encoding="utf-8" standalone = "yes"/> 
    <xsl:variable name="storeMapNf"> 
    <map from="BestBuy" to="Best Buy" /> 
    </xsl:variable> 
    <xsl:variable name="storeMapping" select="msxsl:node-set($storeMapNf)/*" /> 

    <xsl:template match="/"> 

    <html> 
     <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     </head> 
     <body> 

     <table border="1" style="width: 100%;"> 
      <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      <th>SalesThisMonth</th> 
      <th>Returns</th> 
      </tr> 

      <xsl:apply-templates select="//final/store[@name = 'BestBuy']" /> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="final/store"> 
    <xsl:variable name="cdi" select="../cd_info" /> 
    <xsl:variable name="storeMap" 
        select="$storeMapping[@from = current()/@name]" /> 
    <tr> 
     <td> 
     <xsl:value-of select="$cdi/@title" /> 
     </td> 
     <td> 
     <xsl:value-of select="$cdi/@artist" /> 
     </td> 
     <td> 
     <xsl:value-of select="$storeMap/@to | 
           @store[not($storeMap)]"/> 
     </td> 
     <td> 
     <xsl:value-of select="following-sibling::sales/@thismonth"/> 
     </td> 
     <td> 
     <xsl:value-of select="following-sibling::returns/@thismonth"/> 
     </td> 
    </tr> 
    </xsl:template> 
</xsl:stylesheet> 

当你的样品输入运行,其结果是:

<html> 
    <head> 
    <META http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <meta charset="utf-8"> 
    <title></title> 
    </head> 
    <body> 
    <table border="1" style="width: 100%;"> 
     <tr> 
     <th>Albumn Name</th> 
     <th>Artist</th> 
     <th>Store</th> 
     <th>SalesThisMonth</th> 
     <th>Returns</th> 
     </tr> 
     <tr> 
     <td>Empire Burlesque</td> 
     <td>Dylan</td> 
     <td>Best Buy</td> 
     <td>500</td> 
     <td>10</td> 
     </tr> 
     <tr> 
     <td>Stand</td> 
     <td>REM</td> 
     <td>Best Buy</td> 
     <td>1000</td> 
     <td>20</td> 
     </tr> 
    </table> 
    </body> 
</html> 
+0

它真的接近我一直在寻找的 - 我想上面的代码和它只有返回单行(但它返回我期待的方式)除此之外,它的工作。为什么它不会给我第二个记录?我最不明白的部分是storeMap变量和select。谢谢你的时间... – 2013-04-25 03:28:43

+0

忽略之前的评论。我意识到我没有保存带有额外的行的文件。 DOH!感谢你的协助。 – 2013-04-25 03:35:44

0

这种转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <html> 
     <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     </head> 
     <body> 

     <table border="1" style="width: 100%;"> 
      <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      <th>SalesThisMonth</th> 
      <th>Returns</th> 
      </tr> 
      <xsl:apply-templates/> 
     </table> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="store[@name='BestBuy']"> 
    <tr> 
    <xsl:apply-templates select= 
    "@name| ../cd_info/@* | following-sibling::*[not(position()>2)]/@thismonth"/> 
    </tr> 
</xsl:template> 

<xsl:template match="@*"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 
</xsl:stylesheet> 

时所提供的应用XML文档

<catalog_new> 
    <catalog_old> 
     <catalog_newold> 
      <final> 
       <cd_info title="Empire Burlesque" artist="Dylan" /> 
       <store name="BestBuy" /> 
       <sales thismonth="500"/> 
       <returns thismonth="10"/> 
       <store name="Target" /> 
       <sales thismonth="500"/> 
       <returns thismonth="10"/> 
      </final> 
      <final> 
       <cd_info title="Stand" artist="REM" /> 
       <store name="BestBuy" /> 
       <sales thismonth="1000"/> 
       <returns thismonth="20"/> 
       <store name="Target" /> 
       <sales thismonth="530"/> 
       <returns thismonth="50"/> 
      </final> 
     </catalog_newold> 
    </catalog_old> 
</catalog_new> 

产生想要的,正确的结果:

<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

     <meta charset="utf-8"> 
     <title></title> 
    </head> 
    <body> 
     <table border="1" style="width: 100%;"> 
     <tr> 
      <th>Albumn Name</th> 
      <th>Artist</th> 
      <th>Store</th> 
      <th>SalesThisMonth</th> 
      <th>Returns</th> 
     </tr> 
     <tr> 
      <td>Empire Burlesque</td> 
      <td>Dylan</td> 
      <td>BestBuy</td> 
      <td>500</td> 
      <td>10</td> 
     </tr> 
     <tr> 
      <td>Stand</td> 
      <td>REM</td> 
      <td>BestBuy</td> 
      <td>1000</td> 
      <td>20</td> 
     </tr> 
     </table> 
    </body> 
</html>