I have a string that looks like thi开发者_运维问答s "<root><1>1</1><2>2</2></root>"
How would I be able to use <xsl:for-each select="root/1">
?
So, based on your comment, let me suggest this XML:
<?xml version="1.0" encoding="UTF-8"?>
<carLot>
<car type="Ford">2011 Mustang</car>
<car type="Honda">2010 Civic</car>
<truck type="Ford">2007 F150</truck>
<car type="Ford">2010 Focus</car>
<car type="Toyota">2001 Camry</car>
</carLot>
Suppose you only want to match car elements that are of type "Ford," and then display the output as HTML. Try this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:element name="html">
<xsl:element name="body">
<xsl:element name="h1">My Favorite Cars</xsl:element>
<xsl:element name="p">
<xsl:text>These are the Ford cars I found on the car lot:</xsl:text>
</xsl:element>
<xsl:element name="ul">
<xsl:for-each select="carLot/car[@type='Ford']">
<xsl:element name="li">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
The resulting HTML (admittedly incomplete) is this:
<html>
<body>
<h1>My Favorite Cars</h1>
<p>These are the Ford cars I found on the car lot:</p>
<ul>
<li>2011 Mustang</li>
<li>2010 Focus</li>
</ul>
</body>
</html>
How's that?
(Tested with Oxygen 12 on Win 7)
精彩评论