gpt4 book ai didi

xml - Xsl :apply-templates select issues, 需要指定我拉的是哪一年

转载 作者:行者123 更新时间:2023-12-04 07:56:00 25 4
gpt4 key购买 nike

我试图将电影拉到小于某一年和大于某一年。我的输出有问题。
这是我的 XSLT 代码

<?xml version="1.0" encoding="utf-8"?>
<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"/>
<!-- defines the format of the style sheet (html) -->
<xsl:template match="/">
<!-- the match attribute is used to connect the template with the xml element match = "/" means match the whole document. -->
<html>
<head>
<title>Movie Listing</title>
<h1>My Favorite Movies From Before 2005</h1>
<style type="text/css">
table, tr, td, th{background-color:"powderblue"; border: 3px solid black; padding: 5px; border-spacing: 3px;) <!-- style for table -->
</style>
</head>

<body>
<table>
<tr>
<td>
<b>Movie ID</b>
</td>
<td>
<b>Title</b>
</td>
<td>
<b>Director</b>
</td>
<td>
<b>Year</b>
</td>
<td>
<b>Genres</b>
</td>

<xsl:apply-templates select ="movies/movie[year&lt;2005]"></xsl:apply-templates>


<tr>
<td colspan ="3">
<b>Number of Movies</b>
</td>
<td colspan ="2">
<xsl:value-of select="count(movies/movie[year&lt;2005])"/>
</td>
</tr>
</tr>
</table>

<ul>
<b>Principal Directors:</b>
<xsl:for-each select="movies/movie[year&lt;2005]">
<xsl:sort select="pdirector" order="ascending"/>
<li>
<xsl:value-of select="pdirector"/>
</li>
</xsl:for-each>
</ul>






<h1>My Favorite Movies From 2005 Onward</h1>
<table>
<tr>
<td>
<b>Movie ID</b>
</td>
<td>
<b>Title</b>
</td>
<td>
<b>Director</b>
</td>
<td>
<b>Year</b>
</td>
<td>
<b>Genres</b>
</td>

<xsl:apply-templates select ="movies/movie[year>=2005]"></xsl:apply-templates>

<tr>
<td colspan ="3">
<b>Number of Movies</b>
</td>
<td colspan ="2">
<xsl:value-of select="count(movies/movie[year>=2005])"/>
</td>
</tr>
</tr>
</table>
<ul>
<b>Principal Directors:</b>
<xsl:for-each select="movies/movie[year>=2005]">
<xsl:sort select="pdirector" order="ascending"/>
<li>
<xsl:value-of select="pdirector"/>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>



<xsl:template match="movies"> <!-- template for data -->
<xsl:for-each select ="movie"> <!-- looping through movie -->
<xsl:sort select="title" order="ascending"/> <!-- sorting through movie -->
<tr>
<td>
<xsl:value-of select ="@id"/>
<!-- grabbing id-->
<br></br>
</td>
<td>
<!-- grabbing title -->
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</xsl:element>
<br></br>
</td>
<td>
<xsl:value-of select ="pdirector"/>
<!-- grabbing pdirector -->
<br></br>
</td>
<td>
<xsl:value-of select ="year"/>
<!-- grabing year -->
<br></br>
</td>
<td>
<xsl:for-each select ="genres">
<xsl:value-of select="genre"/>,
<xsl:value-of select="genre[2]"/>,
<xsl:value-of select="genre[3]"/>
<br></br>
</xsl:for-each>
</td>
</tr>


</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我应用实际模板的语句似乎给我带来了问题,因为当我将它们留空时,我只是将所有内容正确地拉出来。
<xsl:apply-templates></xsl:apply-templates>
当我继续设置一个条件来提取电影时,我的输出出错了
代码:
<xsl:apply-templates select="movies/movie[year&lt;2005]"></xsl:apply-templates>

and

<xsl:apply-templates select="movies/movie[year>=2005]"></xsl:apply-templates>

最佳答案

快速(部分)解决方案是替换 <xsl:apply-templates ...> 中的 XPath 表达式。从

<xsl:apply-templates select="movies/movie[year&lt;2005]"></xsl:apply-templates>
<xsl:apply-templates select="movies[movie/year&lt;2005]"></xsl:apply-templates>
(也将此更改应用于第二个表达式,使其成为 movies[movie/year>=2005] )
随着这一变化, movies模板将匹配,您将获得(更多)有用的输出。输出仍然不完美,但我猜你会更接近你想要的。
之前的问题是您在没有匹配模板的情况下选择了“电影”元素。现在您选择与您的模板匹配的“电影 s ”元素。

编辑::
XSLT-1.0 样式表的完全改进版本可能如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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"/>
<!-- defines the format of the style sheet (html) -->

<xsl:template match="/movies">
<!-- the match attribute is used to connect the template with the xml element match = "/" means match the whole document. -->
<html>
<head>
<title>Movie Listing</title>
<style type="text/css">
table, tr, td, th{background-color:"powderblue"; border: 3px solid black; padding: 5px; border-spacing: 3px;) <!-- style for table -->
</style>
</head>
<body>
<h1>My Favorite Movies before 2005</h1>
<xsl:call-template name="favorites">
<xsl:with-param name="listMovies" select="movie[year&lt;2015]" />
</xsl:call-template>
<h1>My Favorite Movies From 2005 Onward</h1>
<xsl:call-template name="favorites">
<xsl:with-param name="listMovies" select="movie[year>=2015]" />
</xsl:call-template>
</body>
</html>
</xsl:template>

<xsl:template name="favorites">
<xsl:param name="listMovies" />
<table>
<tr>
<td>
<b>Movie ID</b>
</td>
<td>
<b>Title</b>
</td>
<td>
<b>Director</b>
</td>
<td>
<b>Year</b>
</td>
<td>
<b>Genres</b>
</td>
</tr>
<xsl:apply-templates select ="$listMovies">
<xsl:sort select="title" order="ascending" />
</xsl:apply-templates>
<tr>
<td colspan ="3">
<b>Number of Movies</b>
</td>
<td colspan ="2">
<xsl:value-of select="count($listMovies)"/>
</td>
</tr>
</table>
<ul>
<b>Principal Directors:</b>
<xsl:for-each select="$listMovies">
<xsl:sort select="pdirector" order="ascending"/>
<li>
<xsl:value-of select="pdirector"/>
</li>
</xsl:for-each>
</ul>
</xsl:template>

<xsl:template match="movie"> <!-- template for data -->
<tr>
<td>
<xsl:value-of select ="@id"/>
<!-- grabbing id-->
<br></br>
</td>
<td>
<!-- grabbing title -->
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</xsl:element>
<br></br>
</td>
<td>
<xsl:value-of select ="pdirector"/>
<!-- grabbing pdirector -->
<br></br>
</td>
<td>
<xsl:value-of select ="year"/>
<!-- grabing year -->
<br></br>
</td>
<td>
<xsl:for-each select ="genres">
<xsl:value-of select="genre"/>,
<xsl:value-of select="genre[2]"/>,
<xsl:value-of select="genre[3]"/>
<br></br>
</xsl:for-each>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>

关于xml - Xsl :apply-templates select issues, 需要指定我拉的是哪一年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66701008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com