我试图做的是在示例<person>
中获取一个特定的节点,并将其组合到所有可用人员节点的列表中。 这是我的示例来源:
<group groupName="The Little Rascals">
<peopleInGroup>
<people>
<person>
<name value="John Doe">
<birthdate value="01/01/1953">
</person>
<person>
<name value="John Doe 2">
<birthdate value="01/01/1953">
</person>
<childrenInGroup>
<person>
<name value="Jane">
<birthdate value="01/01/1973">
</person>
<person>
<name value="Suzie">
<birthdate value="01/01/1970">
</person>
</childrenInGroup>
</people>
</peopleInGroup>
</group>
在这种情况下,我想做的是获取所有<person>
元素的列表,而不管每个元素的级别和循环。 该列表如下所示:
<person>
<name value="John Doe">
<birthdate value="01/01/1953">
</person>
<person>
<name value="John Doe 2">
<birthdate value="01/01/1953">
</person>
<person>
<name value="Jane">
<birthdate value="01/01/1973">
</person>
<person>
<name value="Suzie">
<birthdate value="01/01/1970">
</person>
在这种情况下,我唯一想做的排序可能是按生日。 我在想类似于<person>
节点的深度副本,但我不知道它的实现会是什么样子。
创建列表后,我们的想法是按如下所示的 for-each 遍历列表:
<xsl:for-each select="$persons">
<xsl:value-of select="@name"/>
<xsl:value-of select="@birthday"/>
</xsl:for-each>
感谢您的帮助!
除了排序之外,您还可以简单地将 XPath 与//person
一起使用,以选择所有person
元素作为序列。
如果要对它们进行排序,可以使用xsl:perform-sort
:
<xsl:variable name="sorted-persons" as="element(person)*">
<xsl:perform-sort select="descendant::person">
<xsl:sort select="xs:date(replace(birthdate/@value, '([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$2-$1'))"/>
</xsl:perform-sort>
</xsl:variable>
然后,如果需要,可以在for-each中使用该排序序列,或者使用value-of
:直接输出数据:https://xsltfiddle.liberty-development.net/94hvTyZ
<xsl:template match="group">
<xsl:for-each select="peopleInGroup/people/person|peopleInGroup/people/childrenInGroup/person">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
您可以使用 xsl:for-each,然后复制 person。