根据元素值从列表XSLT 1.0中删除重复项

时间:2015-06-12 01:33:23

标签: xslt-1.0

我需要帮助在xslt 1.0中编写一个函数,我可以传递一个列表,并且它会返回列表并删除重复项。它需要是一个模板,我可以轻松克隆或修改其他列表,因为有几个列表,我希望能够运行它。

以下是一个例子:

输入列表:

 <Diagnoses>
      <Code>444.4</Code>
      <Code>959.99</Code>
      <Code>524</Code>
      <Code>444.4</Code>
    </Diagnoses>

在删除重复代码值444.4后所需的输出:

 <Diagnoses>
    <Code>444.4</Code>
    <Code>959.99</Code>
    <Code>524</Code>
 </Diagnoses>

这是我到目前为止所做的,但它似乎并没有起作用:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:math="http://exslt.org/math" 
xmlns:exsl="http://exslt.org/common" 
xmlns:data="http://example.com/data" version="1.0" 
extension-element-prefixes="math exsl" 
exclude-result-prefixes="math exsl data">
   <xsl:output omit-xml-declaration="yes" />






<xsl:variable name="DiagnosesList" select="Diagnoses"/>

<Diagnoses>
   <xsl:call-template name="DedupeLists">
      <xsl:with-param name="Input" select = "exsl:node-set($DiagnosesList)" />
    </xsl:call-template>
</Diagnoses>


<xsl:template name="DedupeLists">
    <xsl:param name = "Input" />

    <xsl:for-each select="$Input/*">
     <xsl:if test="Code[not(preceding::Code)]">
          <xsl:copy-of select="."/>
        </xsl:if>
     </xsl:for-each>

</xsl:template>   

</xsl:stylesheet>

2 个答案:

答案 0 :(得分:2)

@lingamurthy,

只是

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
  <xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Code[. = preceding-sibling::Code]"/>
</xsl:stylesheet>

答案 1 :(得分:1)

这是一种方式:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Code[not(. = preceding-sibling::Code)]">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Code"/>

</xsl:stylesheet>
相关问题