如何使用XSLT样式表仅列出XML中的唯一对?
这是我的XML结构:如何使用XSLT样式表仅列出XML中的唯一对?,xml,xslt,xpath,Xml,Xslt,Xpath,这是我的XML结构: <dblp> <inproceedings key="aaa" mdate="bbb"> <author>author1</author> <author>author2</author> <author>author3</author> <title>Title of pubblications</title> <pages>12345&l
<dblp>
<inproceedings key="aaa" mdate="bbb">
<author>author1</author>
<author>author2</author>
<author>author3</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</inproceedings>
<article key="aaa" mdate="bbb">
<author>author1</author>
<author>author4</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</article>
<article key="aaa" mdate="bbb">
<author>author1</author>
<author>author2</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</article>
<inproceedings key="aaa" mdate="bbb">
<author>author2</author>
<author>author1</author>
<author>author5</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</inproceedings>
</dblp>
输出应如下所示:
author1--auhtor2
author1--auhtor3
author2--auhtor3
author1--auhtor4
---
---
author2--auhtor5
author1--auhtor5
XSLT 2.0解决方案:
<xsl:for-each-group select="/*/*/author" group-by=".">
<xsl:sort select="current-grouping-key()"/>
<xsl:variable name="firstKey" select="current-grouping-key()"></xsl:variable>
<xsl:for-each-group select="/*/*/author[compare(., current-grouping-key()) = 1][some $x in (current-group()) satisfies $x/parent::* intersect ./parent::*]" group-by=".">
<xsl:value-of select="concat($firstKey, '--',current-grouping-key(),'; ')"></xsl:value-of>
</xsl:for-each-group>
</xsl:for-each-group>
XSLT 2.0解决方案:
<xsl:for-each-group select="/*/*/author" group-by=".">
<xsl:sort select="current-grouping-key()"/>
<xsl:variable name="firstKey" select="current-grouping-key()"></xsl:variable>
<xsl:for-each-group select="/*/*/author[compare(., current-grouping-key()) = 1][some $x in (current-group()) satisfies $x/parent::* intersect ./parent::*]" group-by=".">
<xsl:value-of select="concat($firstKey, '--',current-grouping-key(),'; ')"></xsl:value-of>
</xsl:for-each-group>
</xsl:for-each-group>
您可以为每个组使用xslt元素xsl:for,也可以为此使用函数distinct-values() 在下面的模板中,我将您的序列生成器放在一个名为round1的变量中,以便对其进行处理以删除重复项。我更改了内部循环,创建了一个元素(collab),该元素具有属性对(与您的配对匹配)和一个名为cannonicalPair的有序版本。到cannonicalPair是用来消除重复做不同顺序的作者。请注意,有时协作的顺序在现实世界中很重要 round1变量后面是一系列删除重复项的循环。前两个示例表明,您可以在数据集中输出协作的任意顺序。如果协作顺序不同,则最后一种方法不会将协作视为重复
<xsl:template match="/">
<xsl:variable name="papers" select="dblp/*"/>
<xsl:variable name="round1">
<xsl:for-each select="$papers">
<xsl:for-each select="author[position() != last()]">
<xsl:variable name="a1" select="."/>
<xsl:for-each select="following-sibling::author">
<xsl:element name="collab">
<xsl:attribute name="pair" select="concat(translate(translate(translate($a1,' ','_'),'.',''),"'",' '), '--', translate(translate(translate(.,' ','_'),'.',''),"'",' '), '; ')"/>
<xsl:attribute name="cannonicalPair">
<xsl:choose>
<xsl:when test="$a1 lt ." >
<xsl:sequence select="concat(translate(translate(translate($a1,' ','_'),'.',''),"'",' '), '--', translate(translate(translate(.,' ','_'),'.',''),"'",' '), '; ')" />
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="concat(translate(translate(translate(.,' ','_'),'.',''),"'",' '), '--', translate(translate(translate($a1,' ','_'),'.',''),"'",' '), '; ')" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:text>
</xsl:text>
<xsl:for-each-group select="$round1/collab" group-by="@cannonicalPair">
<xsl:value-of select="current-group()[1]/@pair" />
</xsl:for-each-group>
<xsl:text>---- listing seperator ----
</xsl:text>
<xsl:for-each-group select="$round1/collab" group-by="@cannonicalPair">
<xsl:value-of select="current-group()[last()]/@pair" />
</xsl:for-each-group>
<xsl:text>---- listing seperator ----
</xsl:text>
<xsl:for-each select="distinct-values($round1/collab/@cannonicalPair)">
<xsl:value-of select="." />
</xsl:for-each>
<xsl:text>---- listing seperator ----
</xsl:text>
<xsl:for-each select="distinct-values($round1/collab/@pair)">
<xsl:value-of select="." />
</xsl:for-each>
;
----列表分隔符--
;
----列表分隔符--
;
----列表分隔符--
;
可以为每个组使用xslt元素xsl:for,也可以为此使用函数distinct-values() 在下面的模板中,我将您的序列生成器放在一个名为round1的变量中,以便对其进行处理以删除重复项。我更改了内部循环,创建了一个元素(collab),该元素具有属性对(与您的配对匹配)和一个名为cannonicalPair的有序版本。到cannonicalPair是用来消除重复做不同顺序的作者。请注意,有时协作的顺序在现实世界中很重要 round1变量后面是一系列删除重复项的循环。前两个示例表明,您可以在数据集中输出协作的任意顺序。如果协作顺序不同,则最后一种方法不会将协作视为重复
<xsl:template match="/">
<xsl:variable name="papers" select="dblp/*"/>
<xsl:variable name="round1">
<xsl:for-each select="$papers">
<xsl:for-each select="author[position() != last()]">
<xsl:variable name="a1" select="."/>
<xsl:for-each select="following-sibling::author">
<xsl:element name="collab">
<xsl:attribute name="pair" select="concat(translate(translate(translate($a1,' ','_'),'.',''),"'",' '), '--', translate(translate(translate(.,' ','_'),'.',''),"'",' '), '; ')"/>
<xsl:attribute name="cannonicalPair">
<xsl:choose>
<xsl:when test="$a1 lt ." >
<xsl:sequence select="concat(translate(translate(translate($a1,' ','_'),'.',''),"'",' '), '--', translate(translate(translate(.,' ','_'),'.',''),"'",' '), '; ')" />
</xsl:when>
<xsl:otherwise>
<xsl:sequence select="concat(translate(translate(translate(.,' ','_'),'.',''),"'",' '), '--', translate(translate(translate($a1,' ','_'),'.',''),"'",' '), '; ')" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:text>
</xsl:text>
<xsl:for-each-group select="$round1/collab" group-by="@cannonicalPair">
<xsl:value-of select="current-group()[1]/@pair" />
</xsl:for-each-group>
<xsl:text>---- listing seperator ----
</xsl:text>
<xsl:for-each-group select="$round1/collab" group-by="@cannonicalPair">
<xsl:value-of select="current-group()[last()]/@pair" />
</xsl:for-each-group>
<xsl:text>---- listing seperator ----
</xsl:text>
<xsl:for-each select="distinct-values($round1/collab/@cannonicalPair)">
<xsl:value-of select="." />
</xsl:for-each>
<xsl:text>---- listing seperator ----
</xsl:text>
<xsl:for-each select="distinct-values($round1/collab/@pair)">
<xsl:value-of select="." />
</xsl:for-each>
;
----列表分隔符--
;
----列表分隔符--
;
----列表分隔符--
;
此XSLT 2.0转换:完整、简短且格式良好(27行): 说明:
<dblp>
<inproceedings key="aaa" mdate="bbb">
<author>author1</author>
<author>author2</author>
<author>author3</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</inproceedings>
<article key="aaa" mdate="bbb">
<author>author1</author>
<author>author4</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</article>
<article key="aaa" mdate="bbb">
<author>author1</author>
<author>author2</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</article>
<inproceedings key="aaa" mdate="bbb">
<author>author2</author>
<author>author1</author>
<author>author5</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</inproceedings>
</dblp>
author1 author2
author1 author3
author1 author4
author1 author5
author2 author3
author2 author5
$vAuthors
是一系列a
元素,其字符串值集是XML文档中作者
元素的不同值集。a
元素按此顺序排序'kauthorbysiling'
通过其任何同级元素(的字符串值)标识任何“author”元素$vAuthors
中的每个$a1
,我们得到$vAuthors
中的任何$a2
,其字符串值大于$a1
的字符串值,并且使得字符串值等于$a2
的author
元素是字符串值等于$a1
。为此,我们只需(使用通用相等运算符)检查$a2
的字符串值是否在键('kauthorbysiling',$a1,$doc)
的字符串值集中此XSLT 2.0转换:完整、简短且格式良好(27行): 说明:
<dblp>
<inproceedings key="aaa" mdate="bbb">
<author>author1</author>
<author>author2</author>
<author>author3</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</inproceedings>
<article key="aaa" mdate="bbb">
<author>author1</author>
<author>author4</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</article>
<article key="aaa" mdate="bbb">
<author>author1</author>
<author>author2</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</article>
<inproceedings key="aaa" mdate="bbb">
<author>author2</author>
<author>author1</author>
<author>author5</author>
<title>Title of pubblications</title>
<pages>12345</pages>
<year>12345</year>
<crossref>sometext</crossref>
<booktitle>sometext</booktitle>
<url>sometext</url>
<ee>sometext</ee>
</inproceedings>
</dblp>
author1 author2
author1 author3
author1 author4
author1 author5
author2 author3
author2 author5
$vAuthors
是一系列a
元素,其字符串值集是XML文档中作者
元素的不同值集。a
元素按此顺序排序'kauthorbysiling'
通过其任何同级元素(的字符串值)标识任何“author”元素$vAuthors
中的每个$a1
,我们得到$vAuthors
中的任何$a2
,其字符串值大于$a1
的字符串值,并且使得字符串值等于$a2
的author
元素是字符串值等于$a1
。为此,我们只需(使用通用相等运算符)检查$a2
的字符串值是否在键('kauthorbysiling',$a1,$doc)
的字符串值集中完美的谢谢大家。我不是XSLT2.0方面的专家,但我认为这大大改进了lifePerfect!谢谢大家。我不是XSLT2.0方面的专家,但我认为这大大改进了lifePerfect!谢谢大家。我不是XSLT2.0方面的专家,但我看到这大大改善了生活。太棒了!谢谢大家。我不是XSLT2.0方面的专家,但我认为这大大改善了生活。