选择用户的汽车名称



我有下面的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>

我需要打印John的所有汽车名称,<xsl:for-each-在这种情况下不起作用,如何做到这一点

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

相关内容

  • 没有找到相关文章

最新更新