我对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>
我最终删除了我所做的每一次尝试.....我是偏离轨道还是我走在正确的轨道上?
提前致谢。
答案 0 :(得分: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>
答案 1 :(得分: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>