xslt - 选择用户的汽车名称

时间:2012-09-07 06:35:45

标签: xml xslt xslt-1.0 cycle value-of

我的xml文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet version="1.0" type="text/xml" href="template.xsl" ?>
<source>
    <man name="John" surname="Piterson" >
        <cars>
            <car name="Lamborgini Gallardo" maxspeed="400km" />
            <car name="Honda" maxspeed="200km" />
        </cars>
        <appartments>
            <appartment city="New York" />
            <appartment city="Tokio" />
        </appartments>
    </man>
</source>

我在下面有xsl模板,它应该显示汽车列表

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="/" >
        <html>
            <body>
                <h1>Users info</h1>
                <div><strong>The cars of John</strong></div>
                <xsl:for-each select="/man/cars/car" >
                    <div>
                        <xsl:value-of select="car@name" />
                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我需要打印约翰的所有车名,<xsl:for-each - 在这种情况下不起作用,怎么做?

2 个答案:

答案 0 :(得分:1)

<xsl:for-each select="/source/man[@name='John'][@surname='Piterson']/cars/car" >
  <div>
    <xsl:value-of select="@name" />
  </div>
</xsl:for-each>

答案 1 :(得分:1)

<xsl:for-each select="//man[@name = 'John']" >
  <ul>
    <xsl:for-each select="./cars/car" >
      <li>
        <xsl:value-of select="@name"/>: <xsl:value-of select="@maxspeed"/>
      </li>
    </xsl:for-each>
  </ul>
</xsl:for-each>