使用xsl转换xml字符串

时间:2011-07-19 18:03:55

标签: php html xml string xslt

我有一个看起来像这样的字符串"<root><1>1</1><2>2</2></root>"我怎样才能使用<xsl:for-each select="root/1">

1 个答案:

答案 0 :(得分:0)

所以,根据你的评论,让我建议这个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>

假设您只想匹配“Ford”类型的汽车元素,然后将输出显示为HTML。试试这个:

<?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>

由此产生的HTML(诚然不完整)是:

<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>

怎么样?

(在Win 7上用氧气12测试)

相关问题