I am trying to style some xml:
<campers>
<camper>
<firstname>James</firstname>
<lastname>West</lastname>
<gender>Boy</gender>
</camper>
<camper>
<firstname>Adam</firstname>
<lastname>West</lastname>
<gender>Boy</gender>
</camper>
<camper>
<firstname>Ann</firstname>
<lastname>Landers</lastname>
<gender>Girl</gender>
</camper>
<camper>
<firstname>Billy</firstname>
<lastname>Batson</lastname>
<gender>Boy</gender>
</camper>
<camper>
<firstname>Diana</firstname>
<lastname>Prince</lastname>
<gender>Girl</gender>
</camper>
</campers>
I want to have them sorted by gender then name, but I also want to have a header by gender. I cannot seem to get this to work. My output should look like this:
Campers
Boys
Batson, Billy
West, Adam West, JamesGirls
Landers, Ann
Prince, DianaUsing the following xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="1.0"/>
<xsl:template match="/">
<html>
<head><title>Camper list</title></head>
<body style="font-family:Arial,Helvetica,sans-serif;font-size:8pt;">
<h2>Campers</h2>
<xsl:apply-templates select="//camper">
<xsl:sort select="gender" />
<xsl:sort select="lastname" />
<xsl:sort select="firstname" />
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:开发者_如何学Pythontemplate match="camper">
<xsl:if test="not(gender = preceding::gender)">
<h3><xsl:value-of select="gender"/>s</h3>
</xsl:if>
<xsl:value-of select="lastname"/><xsl:text>, </xsl:text><xsl:value-of select="firstname"/><br />
</xsl:template>
</xsl:stylesheet>
Unfortunately, my output looks like this:
Campers
Batson, Billy
West, AdamBoys
West, James
Girls
Landers, Ann
Prince, DianaI am sure I am doing something wrong with the sort, or the sort is not being used as I expect it to be. Any suggestions would help.
Thanks,
David
Addendum: Based on lwburk's solution, here is what I came up with:
<xsl:key name="campers-by-gender" match="camper" use="gender" />
<xsl:template match="/">
<html>
<head><title>Camper xsl test</title></head>
<body style="font-family:Arial,Helvetica,sans-serif;font-size:8pt;">
<h2>Campers</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="campers">
<xsl:for-each select="camper[count(. | key('campers-by-gender', gender)[1]) = 1]">
<h3><xsl:value-of select="key('campers-by-gender', gender)/gender"/>s</h3>
<xsl:for-each select="key('campers-by-gender', gender)">
<xsl:sort select="lastname" />
<xsl:sort select="firstname" />
<xsl:value-of select="lastname"/><xsl:text>, </xsl:text><xsl:value-of select="firstname"/><br />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Thanks for all the help and good ideas! D.
You need to group first and then sort:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="1.0"/>
<xsl:key name="gender" match="camper" use="gender" />
<xsl:template match="/">
<html>
<head><title>Camper list</title></head>
<body style="font-family:Arial,Helvetica,sans-serif;font-size:8pt;">
<h2>Campers</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template
match="camper[generate-id()=generate-id(key('gender', gender)[1])]">
<h3><xsl:value-of select="gender" />s</h3>
<xsl:apply-templates select="key('gender', gender)" mode="out">
<xsl:sort select="lastname" />
<xsl:sort select="firstname" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="camper" mode="out">
<xsl:value-of select="lastname" />
<xsl:text>, </xsl:text>
<xsl:value-of select="firstname" />
<br />
</xsl:template>
<xsl:template match="camper" />
</xsl:stylesheet>
Which produces the following output on your source document:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Camper list</title>
</head>
<body style="font-family: Arial,Helvetica,sans-serif; font-size: 8pt;">
<h2>Campers</h2>
<h3>Boys</h3>
Batson, Billy<br />
West, Adam<br />
West, James<br />
<h3>Girls</h3>
Landers, Ann<br />
Prince, Diana<br />
im sure there are several more simple ways to do this!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="1.0"/>
<xsl:template match="/">
<html>
<head>
<title>Camper list</title>
</head>
<body style="font-family:Arial,Helvetica,sans-serif;font-size:8pt;">
<h2>Campers</h2>
<xsl:apply-templates select="//camper[gender='Boy']">
<xsl:sort select="lastname" />
<xsl:sort select="firstname" />
</xsl:apply-templates>
<xsl:apply-templates select="//camper[gender='Girl']">
<xsl:sort select="lastname" />
<xsl:sort select="firstname" />
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="camper">
<xsl:if test="position() < 2">
<h3>
<xsl:value-of select="gender"/>s
</h3>
</xsl:if>
<xsl:value-of select="lastname"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="firstname"/>
<br />
</xsl:template>
</xsl:stylesheet>
精彩评论