问题描述
我再次为创建适当的 XSLT (3.0) 以在我的文本中提到的人的不同列表而感到困惑。
XML 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<p>Lorem ipsum dolor sit <rs ref="#1">Romeo</rs>,consetetur sadipscing elitr,<rs ref="#2"
>Julia</rs> diam nonumy eirmod <rs ref="#2 #4">family</rs> invidunt ut labore et <other corresp="#3">Dolores</other></p>
我正在尝试获取@ref 或@corresp 的所有不同值的列表。注意@ref 或@corresp 中可以有两个值,因此需要对其进行标记。
结果可能是这样的:
<values>
<a>#1</a>
<a>#2</a>
<a>#3</a>
<a>#4</a>
</values>
在我的实际使用中,我将使用接收到的值在另一个文件中查找人员列表。
到目前为止,这是我所拥有的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:template match="p">
<xsl:for-each-group select="descendant::*[self::rs or self::other]" group-by="@ref">
<a>
<xsl:copy-of select="@ref"/>
</a>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
我未能整合 tokenize(@ref,' ') 以及搜索的值在@ref 或@corresp 中。我错过了什么?
解决方法
像这样(编辑):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:template match="p">
<xsl:variable name="numbers" as="xs:string*">
<xsl:apply-templates select="rs/@ref|other/@corresp"/>
</xsl:variable>
<values>
<xsl:for-each select="distinct-values($numbers)">
<xsl:sort select="."/>
<a>
<xsl:value-of select="."/>
</a>
</xsl:for-each>
</values>
</xsl:template>
<xsl:template match="@*">
<xsl:sequence select="tokenize(.,'\s+')"/>
</xsl:template>
</xsl:stylesheet>