使用xslt过滤xml无法正常工作

时间:2016-04-08 18:53:06

标签: xml xslt

我有一个xml文件

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="main.xsl"?>
<document>
    <parents>
        <parent>
            <name>Jordan</name>
            <age>25</age>
            <childs>
                <child eyes="black">
                    <name>Ema</name>
                </child>
                <child eyes="blue">
                    <name>Kaloyan</name>
                </child>                
            </childs>
        </parent>
    </parents>
</document>

我想用xsl文件main.xsl过滤:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
    <xsl:include href="child.xsl" />
    <xsl:template match="/">
        <html>
            <head>
                <title>Head first</title>
            </head>
            <body>
                <table border="1">
                    <xsl:for-each select="document/parents/parent">
                        <tr>
                            <td>
                                <xsl:text disable-output-escaping="yes">Name</xsl:text>
                            </td>
                            <td>
                                <xsl:value-of select="name"></xsl:value-of>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <xsl:text disable-output-escaping="yes">Age</xsl:text>
                            </td>
                            <td>
                                <xsl:value-of select="age"></xsl:value-of>
                            </td>
                        </tr>
                        <xsl:for-each select="childs/child">
                            <xsl:variable name="obj" select="."></xsl:variable>
                            <xsl:apply-templates select="//child[@eyes = 'black']" mode="child">
                                <xsl:with-param name="obj" select="$obj"></xsl:with-param>
                            </xsl:apply-templates>
                        </xsl:for-each>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

指向child.xsl文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template  name="child">
        <xsl:param name="obj"></xsl:param>
        <h3><xsl:text disable-output-escaping="yes">Child name</xsl:text> </h3>
        <h4><xsl:value-of select="$obj/name"></xsl:value-of></h4>
    </xsl:template>
</xsl:stylesheet>

所需的结果是只看到一个名为Ema的子名,但我看到Ema两次:

<html>

<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  <title>Head first</title>
</head>

<body>
  <table border="1">
    <tbody>
      <tr>
        <td>Name</td>
        <td>Jordan</td>
      </tr>
      <tr>
        <td>Age</td>
        <td>25</td>
      </tr>
      Ema Ema
    </tbody>
  </table>
</body>

</html>

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

原因是您使用"use strict"; var data = [{"match": ["w", "w", "l", "w", "w", "w", "t", "w"]}, {"match": ["w", "k", "w", "A", "w", "w", "w", "w"]}, {"match": ["w", "w", "w", "w", "w", "w", "w", "w"]}, {"match": ["w", "w", "w", "w", "w", "w", "w", "w"]}, {"match": ["w", "w", "w", "w", "w", "w", "w", "w"]}, {"match": ["m", "w", "v", "v", "t", "m", "l", "m"]}, {"match": ["w", "w", "w", "l", "w", "w", "l", "l"]}, {"match": ["w", "w", "z", "w", "w", "m", "l", "w"]}, {"match": ["w", "w", "w", "w", "w", "w", "w", "w"]}, {"match": ["w", "w", "m", "w", "l", "w", "w", "w"]} ]; var root = { "name": "evt_seq", children: [] }; // Get initial pattern var groupedXs = _.groupBy(data, function (d) { return d.match[0]; }); _.forEach(_.keys(groupedXs), function (d) { let _x = groupedXs[d]; let _groupedXs = _.groupBy(_x, function (f) { return f.match[1]; }); let _children = _.map(_.keys(_groupedXs), function (f) { return {'name': f, 'size': _groupedXs[f].length} }); root.children.push({"name": d, children: _children}); }); console.log(JSON.stringify(root)); 迭代childs/child并且有两个<xsl:for-each>元素。因此,<child>执行两次,您将获得两个<xsl:apply-templates select="//child[@eyes = 'black']" mode="child">