每个循环的XSLT不起作用

时间:2014-06-03 14:49:33

标签: html xml xslt

我有一个像这样的xml文件

<generic-cv:generic-cv xmlns:generic-cv="http://www.cihr-irsc.gc.ca/generic-cv/1.0.0" lang="en" dateTimeGenerated="2014-05-30 11:40:50">
    <section id="f589cbc028c64fdaa783da01647e5e3c" label="Personal Information">
        <section id="2687e70e5d45487c93a8a02626543f64" label="Identification" recordId="4f7c2ebd789f407b939e05664f6aa7c0">
            <field id="ee8beaea41f049d8bcfadfbfa89ac09e" label="Title">
                <lov id="00000000000000000000000000000318">Mr.</lov>
            </field>
            <field id="5c6f17e8a67241e19667815a9e95d9d0" label="Family Name">
                <value type="String">ali</value>
            </field>
            <field id="98ad36fee26a4d6b8953ea764f4fed04" label="First Name">
                <value type="String">Hara</value>
            </field>

        </section>
        <section id="2687e70e5d45487c93a8a02626543f64" label="Identification" recordId="4f7c2ebd789f407b939e05664f6aa7c0">
            <field id="ee8beaea41f049d8bcfadfbfa89ac09e" label="Title">
                <lov id="00000000000000000000000000000318">Mr.</lov>
            </field>
            <field id="5c6f17e8a67241e19667815a9e95d9d0" label="Family Name">
                <value type="String">fara</value>
            </field>
            <field id="98ad36fee26a4d6b8953ea764f4fed04" label="First Name">
                <value type="String">hhh</value>
            </field>

        </section>
    </section>
</generic-cv:generic-cv>

和像这样的xslt文件

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Xml Convertor</title>
            </head>
            <body>
                <h2><b> Personal Information</b></h2>
                <xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" />
                <ul>
                    <li>Name: <xsl:value-of select=".//field[@id='98ad36fee26a4d6b8953ea764f4fed04']/value" />, <xsl:value-of select=".//field[@id='5c6f17e8a67241e19667815a9e95d9d0']/value" /></li>

                </ul>       
                </xsl:for-each>                    

            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

我希望循环每个具有特定ID号的部分并打印出列表中的名称。应该看起来像这样

Hara
hhh

到目前为止,我所尝试的并不奏效。有人能看看我做错了吗

2 个答案:

答案 0 :(得分:0)

这是正确的XSLT:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:template match="/">
    <html>
        <head>
            <title>Xml Convertor</title>
        </head>
        <body>
            <h2><b> Personal Information</b></h2>
          <ul>
            <xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" >

                <li>Name: <xsl:value-of   select=".//field[@id='98ad36fee26a4d6b8953ea764f4fed04']/value" />, <xsl:value-of select=".//field[@id='5c6f17e8a67241e19667815a9e95d9d0']/value" /></li>


            </xsl:for-each>                    
            </ul> 
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

我已使用XSLT Tryit Editor from w3schools进行检查,这会产生:

个人信息

姓名:Hara,ali

姓名:hhh,fara

答案 1 :(得分:-1)

在for-each循环中,您需要选择First NameFamily Name字段,而不是ID,如下所示:

<xsl:for-each select=".//section[@id='2687e70e5d45487c93a8a02626543f64']" >
    <li><xsl:value-of select="field[@label='First Name']/value" />,<xsl:value-of select="field[@label='Family Name']/value" /></li>
</xsl:for-each>