我想匹配特定节点值的第一次出现,我很难过。
我已经尝试过<xsl:when test="root/content/contentType = root/content/contentType[.='generic'][1]">
,但它与每一次出现都匹配,就像<xsl:when test="root/content/contentType[.='generic'][1]">
我想以下面的输出 HTML 结尾。第一个项目有一个标题,但每个项目都有一个相同值的类。
这是我的 XML。
任何想法都非常感谢。
XML:
<root>
<content>
<contentType>ingredients</contentType>
<listItems>
<item>Item 1</item>
<item>Item 2</item>
</listItems>
</content>
<content>
<contentType>generic</contentType>
<listItems>
<item>Item 1</item>
<item>Item 2</item>
</listItems>
</content>
<content>
<contentType>generic</contentType>
<listItems>
<item>Item 1</item>
<item>Item 2</item>
</listItems>
</content>
<content>
<contentType>ingredients</contentType>
<listItems>
<item>Item 1</item>
<item>Item 2</item>
</listItems>
</content>
<content>
<contentType>directions</contentType>
<listItems>
<item>Item 1</item>
<item>Item 2</item>
</listItems>
</content>
</root>
期望的输出:
<div class="ingredients">
<h2>Ingredients</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
<div class="generic">
<h2>Generic</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
<div class="generic">
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
<div class="ingredients">
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
<div class="directions">
<h2>Directions</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
编辑:
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:for-each select="root/content">
<div>
<xsl:attribute name="class"><xsl:value-of select="./contentType"/></xsl:attribute>
<xsl:choose>
<xsl:when test="./contentType[.='ingredients'][1]">
<h2><xsl:value-of select="./contentType"/></h2>
</xsl:when>
<xsl:when test="./contentType[.='generic'][1]">
<h2><xsl:value-of select="./contentType"/></h2>
</xsl:when>
<xsl:when test="./contentType[.='directions'][1]">
<h2><xsl:value-of select="./contentType"/></h2>
</xsl:when>
</xsl:choose>
<ul>
<xsl:for-each select="listItems/item">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
</ul>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>