2

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

我需要做的是选择每个 final/cd_info 项目,然后只需选择名称 = Best Buy 的商店记录,然后选择每个销售和退货的本月值。

我正在尝试制作一个如下所示的表格:

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>

我最终删除了我所做的每一次尝试......我只是偏离轨道还是我在正确的轨道上?

提前致谢。

4

2 回答 2

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>
于 2013-04-25T01:19:12.090 回答
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>
于 2013-04-25T03:16:03.400 回答