我对 XSLT 很陌生。我被困在我的项目中。我需要获取下面的 XML 并在下面生成所需的 HTML。
我需要获取“附加”小节下的子节点,并将它们两两分成 HTML 行,每行中都有一个左右元素。
我可以设法获得正确的左右元素。然后我可以将所有子节点分组到一个大行中,或者将每个子节点分组到一个单独的行中。但是,我无法在具有左右元素的一行中生成两个 HTML 组。
我需要将附加故事的数量限制为 show="" 属性。这将始终返回一个偶数。我有限制工作,我可以计算行数。但不能将它们分成两排。
我知道您需要使用复制节点,但我无法使其工作。
我有以下 XML:
<?xml version="1.0" ?>
<fullpage>
<section name="tops">
<subsect name="featured" count="1">
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align></align>
<size>2</size>
</article>
</subsect>
<subsect name="additional" count="7" show="6">
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>left</align>
</article>
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>right</align>
</article>
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>left</align>
</article>
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>right</align>
</article>
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>left</align>
</article>
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>right</align>
</article>
<article>
<url>http://www.myurl.com</url>
<title>Title Element</title>
<description>Description</description>
<align>left</align>
</article>
</subsect>
</section>
<section name="section2">
<article>
...
</article>
<article>
...
</article>
<article>
...
</article>
<article>
...
</article>
</section>
</fullpage>
到目前为止,这是 XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:variable name="topadd" select="fullpage/section[@name='tops']/subsect[@name='additional']/@show" />
<xsl:variable name="topaddrows" select="fullpage/section[@name='tops']/subsect[@name='additional']/@show div 2" />
<!-- top news -->
<div class="bucket">
<xsl:for-each select="fullpage/section[@name='tops']/subsect[@name='featured']">
<xsl:choose>
<xsl:when test="article/size = '2'">
<div class="col2Feature">
<ul>
<li> Two <xsl:value-of select="article/title"/> </li>
</ul>
</div>
</xsl:when>
<xsl:when test="article/size = '4'">
<div class="col4Feature">
<ul>
<li> Four <xsl:value-of select="article/title"/> </li>
</ul>
</div>
</xsl:when>
<xsl:when test="article/size = '6'">
<div class="col6Feature">
<ul>
<li> Six <xsl:value-of select="article/title"/> </li>
</ul>
</div>
</xsl:when>
<xsl:when test="article/size = '8'">
<div class="col8Feature">
<ul>
<li> Eight <xsl:value-of select="article/title"/> </li>
</ul>
</div>
</xsl:when>
</xsl:choose>
<!-- /feature -->
</xsl:for-each>
</div>
<!-- /top news -->
<xsl:value-of select="$topaddrows"/>
<xsl:for-each select="fullpage/section[@name='tops']/subsect[@name='additional']/article">
<xsl:if test="not(position() > $topadd)">
<xsl:choose>
<xsl:when test="align = 'left'">
<div class="bucketL">
<ul>
<li> <xsl:value-of select="title"/> </li>
</ul>
</div>
</xsl:when>
<xsl:when test="align = 'right'">
<div class="bucketR">
<ul>
<li> <xsl:value-of select="title"/> </li>
</ul>
</div>
</xsl:when>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
所需的 HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
</head>
<body>
<div class="bucket">
<div class="col2Feature">
<ul>
<li> Two Title Element</li>
</ul>
</div>
</div>
<div class="col2">
<div class="bucketL">
<ul>
<li>Title Element</li>
</ul>
</div>
<div class="bucketR">
<ul>
<li>Title Element</li>
</ul>
</div>
</div>
<div class="col2">
<div class="bucketL">
<ul>
<li>Title Element</li>
</ul>
</div>
<div class="bucketR">
<ul>
<li>Title Element</li>
</ul>
</div>
</div>
<div class="col2">
<div class="bucketL">
<ul>
<li>Title Element</li>
</ul>
</div>
<div class="bucketR">
<ul>
<li>Title Element</li>
</ul>
</div>
</div>
</body>
</html>