Xslt 根据给定的顺序对信息进行排序

Xslt 根据给定的顺序对信息进行排序,xslt,xslt-2.0,Xslt,Xslt 2.0,请建议如何根据给定的排序顺序(Seq.xml)对信息进行排序 这里从文件夹中获取信息,其中文件名应具有“Sort##.xml”格式。输出应基于外部文件“D:\sort\Seq.xml”中给定的sequence的DOI编号进行排序 输入XML: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/

请建议如何根据给定的排序顺序(Seq.xml)对信息进行排序

这里从文件夹中获取信息,其中文件名应具有“Sort##.xml”格式。输出应基于外部文件“D:\sort\Seq.xml”中给定的sequence的DOI编号进行排序

输入XML:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="varCollection">

<xsl:copy-of select="
    collection('file:///D:/Sort/?select=Sort*.xml; recurse=yes')
    [matches(document-uri(.),'Sort/Sort[0-9][0-9].xml')]"/>
</xsl:variable><!-- to fetch info from folder 'Sort*.xml' s -->

<xsl:variable name="docSeq" select="document('D:/Sort/Seq.xml')"/><!--In this file, sequnce info is there -->

<!--xsl:key name="kSeq" match="$docSeq/root/toc/seq/@seq" use="art-id"/--><!-- I tried with key, but unable to get the required sequence -->

<xsl:template match="root">
    <xsl:for-each select="$varCollection/article">
        <!--xsl:sort select="key('kSeq', fm/doi)"/-->
        <art>
            <title><xsl:value-of select="fm/title"/></title>
            <Name><xsl:value-of select="fm/aug/au"/></Name>
            <DOI><xsl:value-of select="fm/doi"/></DOI>
        </art><xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
D:\Sort\Sort01.xml

<article>
<fm>
    <title>The solar system</title>
    <aug><au>Rudramuni TP</au></aug>
    <doi>10.11/MPS.0.10.11</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Galaxy</title>
    <aug><au>Kishan TR</au></aug>
    <doi>10.11/MPS.0.10.2</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Pluto</title>
    <aug><au>Kowshik MD</au></aug>
    <doi>10.11/MPS.0.10.10</doi>
</fm>
<body><text>The text</text></body>
</article>
<root>
<toc>
    <seq seq="1"><art-id>10.11/MPS.0.10.2</art-id></seq>
    <seq seq="2"><art-id>10.11/MPS.0.10.11</art-id></seq>
    <seq seq="3"><art-id>10.11/MPS.0.10.10</art-id></seq>
</toc>
</root>

太阳系
鲁德拉穆尼酒店
10.11/MPS.0.10.11
正文
D:\Sort\Sort02.xml

<article>
<fm>
    <title>The solar system</title>
    <aug><au>Rudramuni TP</au></aug>
    <doi>10.11/MPS.0.10.11</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Galaxy</title>
    <aug><au>Kishan TR</au></aug>
    <doi>10.11/MPS.0.10.2</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Pluto</title>
    <aug><au>Kowshik MD</au></aug>
    <doi>10.11/MPS.0.10.10</doi>
</fm>
<body><text>The text</text></body>
</article>
<root>
<toc>
    <seq seq="1"><art-id>10.11/MPS.0.10.2</art-id></seq>
    <seq seq="2"><art-id>10.11/MPS.0.10.11</art-id></seq>
    <seq seq="3"><art-id>10.11/MPS.0.10.10</art-id></seq>
</toc>
</root>

银河系
基尚
10.11/MPS.0.10.2
正文
D:\Sort\Sort03.xml

<article>
<fm>
    <title>The solar system</title>
    <aug><au>Rudramuni TP</au></aug>
    <doi>10.11/MPS.0.10.11</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Galaxy</title>
    <aug><au>Kishan TR</au></aug>
    <doi>10.11/MPS.0.10.2</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Pluto</title>
    <aug><au>Kowshik MD</au></aug>
    <doi>10.11/MPS.0.10.10</doi>
</fm>
<body><text>The text</text></body>
</article>
<root>
<toc>
    <seq seq="1"><art-id>10.11/MPS.0.10.2</art-id></seq>
    <seq seq="2"><art-id>10.11/MPS.0.10.11</art-id></seq>
    <seq seq="3"><art-id>10.11/MPS.0.10.10</art-id></seq>
</toc>
</root>

冥王星
九龙马里兰州
10.11/MPS.0.10.10
正文
D:\Sort\Seq.xml中的序列信息

<article>
<fm>
    <title>The solar system</title>
    <aug><au>Rudramuni TP</au></aug>
    <doi>10.11/MPS.0.10.11</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Galaxy</title>
    <aug><au>Kishan TR</au></aug>
    <doi>10.11/MPS.0.10.2</doi>
</fm>
<body><text>The text</text></body>
</article>
<article>
<fm>
    <title>The Pluto</title>
    <aug><au>Kowshik MD</au></aug>
    <doi>10.11/MPS.0.10.10</doi>
</fm>
<body><text>The text</text></body>
</article>
<root>
<toc>
    <seq seq="1"><art-id>10.11/MPS.0.10.2</art-id></seq>
    <seq seq="2"><art-id>10.11/MPS.0.10.11</art-id></seq>
    <seq seq="3"><art-id>10.11/MPS.0.10.10</art-id></seq>
</toc>
</root>

10.11/MPS.0.10.2
10.11/MPS.0.10.11
10.11/MPS.0.10.10
XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="varCollection">

<xsl:copy-of select="
    collection('file:///D:/Sort/?select=Sort*.xml; recurse=yes')
    [matches(document-uri(.),'Sort/Sort[0-9][0-9].xml')]"/>
</xsl:variable><!-- to fetch info from folder 'Sort*.xml' s -->

<xsl:variable name="docSeq" select="document('D:/Sort/Seq.xml')"/><!--In this file, sequnce info is there -->

<!--xsl:key name="kSeq" match="$docSeq/root/toc/seq/@seq" use="art-id"/--><!-- I tried with key, but unable to get the required sequence -->

<xsl:template match="root">
    <xsl:for-each select="$varCollection/article">
        <!--xsl:sort select="key('kSeq', fm/doi)"/-->
        <art>
            <title><xsl:value-of select="fm/title"/></title>
            <Name><xsl:value-of select="fm/aug/au"/></Name>
            <DOI><xsl:value-of select="fm/doi"/></DOI>
        </art><xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>



所需顺序

<art><title>The Galaxy</title><Name>Kishan TR</Name><DOI>10.11/MPS.0.10.2</DOI></art>
<art><title>The solar system</title><Name>Rudramuni TP</Name><DOI>10.11/MPS.0.10.11</DOI></art>
<art><title>The Pluto</title><Name>Kowshik MD</Name><DOI>10.11/MPS.0.10.10</DOI></art>
GalaxyKishan TR10.11/MPS.0.10.2
太阳能系统Rudramuni TP10.11/MPS.0.10.11
普鲁托科夫斯基MD10.11/MPS.0.10.10
我想你想要

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="varCollection" select="
    collection('file:///D:/Sort/?select=Sort*.xml; recurse=yes')
    [matches(document-uri(.),'Sort/Sort[0-9][0-9].xml')]"/>


<xsl:variable name="docSeq" select="document('file:///D:/Sort/Seq.xml')"/><!--In this file, sequnce info is there -->

<xsl:key name="kSeq" match="root/toc/seq" use="art-id"/>

<xsl:template match="root">
    <xsl:for-each select="$varCollection/article">
        <xsl:sort select="key('kSeq', fm/doi, $docSeq)/xs:integer(@seq)"/>
        <art>
            <title><xsl:value-of select="fm/title"/></title>
            <Name><xsl:value-of select="fm/aug/au"/></Name>
            <DOI><xsl:value-of select="fm/doi"/></DOI>
        </art>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

我想你想要

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="varCollection" select="
    collection('file:///D:/Sort/?select=Sort*.xml; recurse=yes')
    [matches(document-uri(.),'Sort/Sort[0-9][0-9].xml')]"/>


<xsl:variable name="docSeq" select="document('file:///D:/Sort/Seq.xml')"/><!--In this file, sequnce info is there -->

<xsl:key name="kSeq" match="root/toc/seq" use="art-id"/>

<xsl:template match="root">
    <xsl:for-each select="$varCollection/article">
        <xsl:sort select="key('kSeq', fm/doi, $docSeq)/xs:integer(@seq)"/>
        <art>
            <title><xsl:value-of select="fm/title"/></title>
            <Name><xsl:value-of select="fm/aug/au"/></Name>
            <DOI><xsl:value-of select="fm/doi"/></DOI>
        </art>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

您也可以从相反方向执行此操作:

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>

<xsl:variable name="varCollection">
    <xsl:sequence select="collection('file:///D:/Sort/?select=Sort*.xml; recurse=yes')
    [matches(document-uri(.),'Sort/Sort[0-9][0-9].xml')]"/>
</xsl:variable>

<xsl:variable name="docSeq" select="document('D:/Sort/Seq.xml')"/>

<xsl:key name="article" match="article" use="fm/doi" />

<xsl:template match="root">
    <xsl:for-each select="$docSeq/root/toc/seq">
        <xsl:sort select="@seq" data-type="number" order="ascending"/>
        <xsl:apply-templates select="key('article', art-id, $varCollection)"/>
     </xsl:for-each>    
</xsl:template>

<xsl:template match="article">
    <art>
        <title><xsl:value-of select="fm/title"/></title>
        <Name><xsl:value-of select="fm/aug/au"/></Name>
        <DOI><xsl:value-of select="fm/doi"/></DOI>
    </art>
</xsl:template>

</xsl:stylesheet>


请注意,输出缺少根元素。

您也可以从相反方向执行此操作:

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>

<xsl:variable name="varCollection">
    <xsl:sequence select="collection('file:///D:/Sort/?select=Sort*.xml; recurse=yes')
    [matches(document-uri(.),'Sort/Sort[0-9][0-9].xml')]"/>
</xsl:variable>

<xsl:variable name="docSeq" select="document('D:/Sort/Seq.xml')"/>

<xsl:key name="article" match="article" use="fm/doi" />

<xsl:template match="root">
    <xsl:for-each select="$docSeq/root/toc/seq">
        <xsl:sort select="@seq" data-type="number" order="ascending"/>
        <xsl:apply-templates select="key('article', art-id, $varCollection)"/>
     </xsl:for-each>    
</xsl:template>

<xsl:template match="article">
    <art>
        <title><xsl:value-of select="fm/title"/></title>
        <Name><xsl:value-of select="fm/aug/au"/></Name>
        <DOI><xsl:value-of select="fm/doi"/></DOI>
    </art>
</xsl:template>

</xsl:stylesheet>


请注意,输出缺少根元素。

完美建议先生,加上一个用于极好的“键”函数。完美建议先生,加上一个用于极好的“键”函数。感谢您的建议,但这里的转换不是来自Seq.xml,再加上一对一的方法。@RudramuniTP这是一个微不足道的调整:请参见编辑后的答案。非常感谢您的建议,您向我建议了两种不同的方法来达到结果。再次感谢。感谢您的建议,但这里的转换不是来自Seq.xml,再加上一个换一个方法。@RudramuniTP这是一个很小的调整:请参阅编辑后的答案。非常感谢您的建议,您向我建议了两种不同的方法以获得结果。再次感谢。