Select Distinct Element into a node XSLT

馋奶兔 提交于 2019-12-24 19:34:03

问题


This is my XML code:

<SECTOR_LIST>
    <LIGHT_SECTOR>
        <SECTOR1>22</SECTOR1>
        <SECTOR2>92</SECTOR2>
    </LIGHT_SECTOR>
    <LIGHT_SECTOR>
        <SECTOR1>22</SECTOR1>
        <SECTOR2>92</SECTOR2>
    </LIGHT_SECTOR>
    <LIGHT_SECTOR>
        <SECTOR1>92</SECTOR1>
        <SECTOR2>137</SECTOR2>
    </LIGHT_SECTOR>
    <LIGHT_SECTOR>
        <SECTOR1>92</SECTOR1>
        <SECTOR2>137</SECTOR2>
    </LIGHT_SECTOR>
</SECTOR_LIST>

I create this XSLT 1.0:

<xsl:for-each select="SECTOR_LIST"> 
    <xsl:for-each select="LIGHT_SECTOR">
        <xsl:text>VIS </xsl:text>
            <xsl:value-of select="SECTOR1"/>
            <xsl:text>-</xsl:text>
            <xsl:value-of select="SECTOR2"/>
            <br/>
    </xsl:for-each>
</xsl:for-each>

In output I had this: VIS 22-92 VIS 22-92 VIS 92-137 VIS 92-137

I would only: VIS 22-92 VIS 92-137


回答1:


Please try this code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="sector" match="//LIGHT_SECTOR" use="."/>
<xsl:variable name="Sectors" select="//LIGHT_SECTOR"/>
<xsl:template match="SECTOR_LIST">
    <xsl:for-each select="$Sectors[count(. | key('sector', .)[1]) = 1]">
        <xsl:text>VIS </xsl:text>
        <xsl:value-of select="SECTOR1"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="SECTOR2"/>
        <br/>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


来源:https://stackoverflow.com/questions/21180816/select-distinct-element-into-a-node-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!