使用XSLT删除重复

时间:2017-06-06 14:14:47

标签: xml xslt duplicates xslt-1.0

让我们假装我们有一个格式如下的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="preview2.xsl"?>
<NTC_PUBLICATION xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
  <SECTION_LIST>
    <SECTION_LIST_ITEM>
      <NM_MISC_SECTION>
        <SECTION_CONTENT_LIST>
          <SECTION_CONTENT_LIST_ITEM>
            <NM_PNC_PRD>
              <SECTION_CONTENT_LIST>
                <SECTION_CONTENT_LIST_ITEM>
                  <NM_PNC_NEPUB>
                    <INSTRUCTION_LIST>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCNEP>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>64</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCNEP>
                      </INSTRUCTION_LIST_ITEM>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCNEP>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>66</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCNEP>
                      </INSTRUCTION_LIST_ITEM>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCNEP>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>5111</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCNEP>
                      </INSTRUCTION_LIST_ITEM>
                    </INSTRUCTION_LIST>
                  </NM_PNC_NEPUB>
                </SECTION_CONTENT_LIST_ITEM>
                <SECTION_CONTENT_LIST_ITEM>
                  <NM_PNC_CPW>
                    <INSTRUCTION_LIST>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCPW>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>5111</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCPW>
                      </INSTRUCTION_LIST_ITEM>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCPW>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>5212</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCPW>
                      </INSTRUCTION_LIST_ITEM>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCPW>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>66</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCPW>
                      </INSTRUCTION_LIST_ITEM>
                    </INSTRUCTION_LIST>
                  </NM_PNC_CPW>
                </SECTION_CONTENT_LIST_ITEM>
              </SECTION_CONTENT_LIST>
            </NM_PNC_PRD>
          </SECTION_CONTENT_LIST_ITEM>
          <SECTION_CONTENT_LIST_ITEM>
            <NM_PNC_PRD_TPUB>
              <SECTION_CONTENT_LIST>
                <SECTION_CONTENT_LIST_ITEM>
                  <NM_PNC_NETPUB>
                    <INSTRUCTION_LIST>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCNETBP>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>64</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCNETBP>
                      </INSTRUCTION_LIST_ITEM>
                      <INSTRUCTION_LIST_ITEM>
                        <NM_MISC_INSTRUCT_PNCNETBP>
                          <CHART_LIST>
                            <CHART>
                              <CHART_NUM>5423</CHART_NUM>
                            </CHART>
                          </CHART_LIST>
                        </NM_MISC_INSTRUCT_PNCNETBP>
                      </INSTRUCTION_LIST_ITEM>
                    </INSTRUCTION_LIST>
                  </NM_PNC_NETPUB>
                </SECTION_CONTENT_LIST_ITEM>
              </SECTION_CONTENT_LIST>
            </NM_PNC_PRD_TPUB>
          </SECTION_CONTENT_LIST_ITEM>
        </SECTION_CONTENT_LIST>
      </NM_MISC_SECTION>
    </SECTION_LIST_ITEM>
  </SECTION_LIST>
</NTC_PUBLICATION>

我想从此模板生成的列表中删除重复项,该模板使用&#34;祖先&#34;:

    <xsl:template
        match="CHART_NUM[ancestor::NM_MISC_INSTRUCT_PNCNEP or ancestor::NM_MISC_INSTRUCT_PNCPW or ancestor::NM_MISC_INSTRUCT_PNCNETBP]"
        mode="list">
            <tr>
                <td style="text-align:left;">
                    <xsl:value-of select="concat('NZ ',.)"/>
                </td>
            </tr>
    </xsl:template>

模板的应用如下:

<xsl:apply-templates select="//CHART_NUM[ancestor::NM_MISC_INSTRUCT_PNCNEP or ancestor::NM_MISC_INSTRUCT_PNCPW or ancestor::NM_MISC_INSTRUCT_PNCNETBP]" mode="list"><xsl:sort data-type="number" order="ascending"></xsl:apply-templates>

目前输出是这样的,我想删除重复项:

NZ 64
NZ 64
NZ 66
NZ 66
NZ 5111
NZ 5111
NZ 5212
NZ 5423

我尝试了这个,但它无法正常工作:

    <xsl:template
        match="CHART_NUM[ancestor::NM_MISC_INSTRUCT_PNCNEP or ancestor::NM_MISC_INSTRUCT_PNCPW or ancestor::NM_MISC_INSTRUCT_PNCNETBP]"
        mode="list">
        <xsl:if test="not (preceding-sibling::CHART_NUM[ancestor::NM_MISC_INSTRUCT_PNCNEP or ancestor::NM_MISC_INSTRUCT_PNCPW or ancestor::NM_MISC_INSTRUCT_PNCNETBP]/text() = current()/text())" >
            <xsl:copy>
                <tr>
                    <td style="text-align:left;">
                        <xsl:value-of select="concat('NZ ',.)"/>
                    </td>
                </tr>
            </xsl:copy>
        </xsl:if>
    </xsl:template>

0 个答案:

没有答案