XSLT,XML,For-Each的属性和元素

时间:2010-11-01 15:41:28

标签: xml xslt

我找到了使用XSLT显示元素值的教程,但我正在努力学习属性。这是我的XML文档。

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="PrintListStyleSheet.xsl"?>
<PrinterDirectory>
<Country Name = "UK">
<Region Name="Birmingham">
<Building Name="Brindley Place" url="Brindley.htm"/>
<Building Name="Solihull TE" url="Solihull.htm"/>
<Building Name="Midland ATE" url="MidlandATE.html"/>
 <Building Name="Redditch" url="Redditch.html"/>
</Region>
<Region Name="Wolverhampton">
<Building Name="Wolverhampton TE" url="WolverhamptonTE.htm"/>
</Region>
<Region Name="Cardiff">
<Building Name="Park Gate" url="CardiffParkGate.htm"/>
<Building Name="Cardiff Bay Data Centre " url="CardiffParkGate.htm"/>
</Region>
   </Country>
 </PrinterDirectory>

我要做的是显示区域列表。即。

  • 伯明翰
    • Brindley Place
    • Solihull ATE
  • 伍尔弗

我该怎么做?

1 个答案:

答案 0 :(得分:3)

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="PrinterDirectory">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    <xsl:template match="Country|Region">
        <li>
            <p>
                <xsl:value-of select="@Name"/>
            </p>
            <ul>
                <xsl:apply-templates/>
            </ul>
        </li>
    </xsl:template>
    <xsl:template match="Building">
        <li>
            <xsl:value-of select="@Name"/>
        </li>
    </xsl:template>
</xsl:stylesheet>

输出:

<ul>
    <li>
        <p>UK</p>
        <ul>
            <li>
                <p>Birmingham</p>
                <ul>
                    <li>Brindley Place</li>
                    <li>Solihull TE</li>
                    <li>Midland ATE</li>
                    <li>Redditch</li>
                </ul></li>
            <li>
                <p>Wolverhampton</p>
                <ul>
                    <li>Wolverhampton TE</li>
                </ul></li>
            <li>
                <p>Cardiff</p>
                <ul>
                    <li>Park Gate</li>
                    <li>Cardiff Bay Data Centre </li>
                </ul></li>
        </ul></li>
</ul>

修改:使用xsl:value-of

提高效果