Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT中的每父节点分组_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Xml XSLT中的每父节点分组

Xml XSLT中的每父节点分组,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,这个问题的标题可能会令人困惑。如果有人对此有正确的术语,请编辑。 我有这样一个XML输入文件: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="show.xsl"?> <patients> <patient> <name>Joe</name> <age>

这个问题的标题可能会令人困惑。如果有人对此有正确的术语,请编辑。 我有这样一个XML输入文件:

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="show.xsl"?>
    <patients>
        <patient>
            <name>Joe</name>
            <age>45</age>
            <kids>
                <kid>
                    <name>Ramsay</name>
                    <gender>M</gender>
                </kid>
                <kid>
                    <name>Eva</name>
                    <gender>F</gender>
                </kid>
                <kid>
                    <name>Arthas</name>
                    <gender>M</gender>
                </kid>
            </kids>
        </patient>
        <patient>
            <name>Adam</name>
            <age>34</age>
            <kids>
                <kid>
                    <name>Jon</name>
                    <gender>M</gender>
                </kid>
                <kid>
                    <name>Jane</name>
                    <gender>F</gender>
                </kid>
                <kid>
                    <name>Sunita</name>
                    <gender>F</gender>
                </kid>
            </kids>
        </patient>
    </patients>
下面是我使用xsl:key编写的XSLT,但它给出了错误的输出,因为它无法对患者进行分组。


我正在寻找一种解决方案,最好是XSLT 1.0。

如果您的数据没有唯一的患者id,或者即使有,您也可以使用generate id函数生成一个。我不确定您是否有一个正常的名称,不能假定名称是唯一的,因此请尝试以下方法:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:output method="text" encoding="UTF-8" />
<xsl:strip-space elements="*"/>

<xsl:key name="kids-by-gender" match="kid" use="concat(generate-id(ancestor::patient), gender)"/>

<xsl:template match="patient">
    <xsl:variable name="pid" select="generate-id()" />
    <xsl:value-of select="concat('Name: ', name, '&#10;') "/>
    <xsl:value-of select="concat('Age: ', age, '&#10;') "/>
    <xsl:text>Kids:&#10;</xsl:text>
    <xsl:text>&#9;Boys:&#10;</xsl:text>
    <xsl:apply-templates select="key('kids-by-gender', concat($pid, 'M')) "/>
    <xsl:text>&#9;Girls:&#10;</xsl:text>
    <xsl:apply-templates select="key('kids-by-gender', concat($pid, 'F')) "/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="kid">
    <xsl:value-of select="concat('&#9;&#9;', name, '&#10;') "/>
</xsl:template>
</xsl:stylesheet>

注意,这需要更多的工作来适应没有性别的孩子,或者根本没有孩子。

你的数据是否包括病人的唯一ID?你可以把名字看作唯一的ID。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="header" match="//kid" use="gender"/>
<xsl:template match="//patients/patient">
        <xsl:value-of select="name"/>
        <xsl:text xml:space="preserve">
        </xsl:text>
        <xsl:value-of select="age"/>
        <xsl:text xml:space="preserve">
        </xsl:text>
        <xsl:text xml:space="preserve">Boys:
            </xsl:text>
        <xsl:for-each select="key('header', 'M')">
            <xsl:value-of select="name"/>
            <xsl:text xml:space="preserve">
            </xsl:text>
        </xsl:for-each>
        <xsl:text xml:space="preserve">
        </xsl:text>        
<xsl:text xml:space="preserve">Girls:
            </xsl:text>
        <xsl:for-each select="key('header', 'F')">
            <xsl:value-of select="name"/>
            <xsl:text xml:space="preserve">
            </xsl:text>
        </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:output method="text" encoding="UTF-8" />
<xsl:strip-space elements="*"/>

<xsl:key name="kids-by-gender" match="kid" use="concat(generate-id(ancestor::patient), gender)"/>

<xsl:template match="patient">
    <xsl:variable name="pid" select="generate-id()" />
    <xsl:value-of select="concat('Name: ', name, '&#10;') "/>
    <xsl:value-of select="concat('Age: ', age, '&#10;') "/>
    <xsl:text>Kids:&#10;</xsl:text>
    <xsl:text>&#9;Boys:&#10;</xsl:text>
    <xsl:apply-templates select="key('kids-by-gender', concat($pid, 'M')) "/>
    <xsl:text>&#9;Girls:&#10;</xsl:text>
    <xsl:apply-templates select="key('kids-by-gender', concat($pid, 'F')) "/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="kid">
    <xsl:value-of select="concat('&#9;&#9;', name, '&#10;') "/>
</xsl:template>
</xsl:stylesheet>