Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 734277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:21:14+00:00 2026-05-14T07:21:14+00:00

How do I prevent duplicate entries into a list, and then ideally, sort that

  • 0

How do I prevent duplicate entries into a list, and then ideally, sort that list? What I’m doing, is when information at one level is missing, taking the information from a level below it, to building the missing list, in the level above. Currently, I have XML similar to this:

<c03 id="ref6488" level="file">
    <did>
        <unittitle>Clinic Building</unittitle>
        <unitdate era="ce" calendar="gregorian">1947</unitdate>
    </did>
    <c04 id="ref34582" level="file">
        <did>
            <container label="Box" type="Box">156</container>
            <container label="Folder" type="Folder">3</container>
        </did>
    </c04>
    <c04 id="ref6540" level="file">
        <did>
            <container label="Box" type="Box">156</container>
            <unittitle>Contact prints</unittitle>
        </did>
    </c04>
    <c04 id="ref6606" level="file">
        <did>
            <container label="Box" type="Box">154</container>
            <unittitle>Negatives</unittitle>
        </did>
    </c04>
</c03>

I then apply the following XSL:

<xsl:template match="c03/did">
    <xsl:choose>
        <xsl:when test="not(container)">
            <did>
                <!-- If no c03 container item is found, look in the c04 level for one -->
                <xsl:if test="../c04/did/container">

                    <!-- If a c04 container item is found, use the info to build a c03 version -->
                    <!-- Skip c03 container item, if still no c04 items found -->
                    <container label="Box" type="Box">

                        <!-- Build container list -->
                        <!-- Test for more than one item, and if so, list them, -->
                        <!-- separated by commas and a space -->
                        <xsl:for-each select="../c04/did">
                            <xsl:if test="position() &gt; 1">, </xsl:if>
                            <xsl:value-of select="container"/>
                        </xsl:for-each>
                    </container>                    
            </did>
        </xsl:when>

        <!-- If there is a c03 container item(s), list it normally -->
        <xsl:otherwise>
            <xsl:copy-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

But I’m getting the “container” result of

<container label="Box" type="Box">156, 156, 154</container>

when what I want is

<container label="Box" type="Box">154, 156</container>

Below is the full result that I’m trying to get:

<c03 id="ref6488" level="file">
    <did>
        <container label="Box" type="Box">154, 156</container>
        <unittitle>Clinic Building</unittitle>
        <unitdate era="ce" calendar="gregorian">1947</unitdate>
    </did>
    <c04 id="ref34582" level="file">
        <did>
            <container label="Box" type="Box">156</container>
            <container label="Folder" type="Folder">3</container>
        </did>
    </c04>
    <c04 id="ref6540" level="file">
        <did>
            <container label="Box" type="Box">156</container>
            <unittitle>Contact prints</unittitle>
        </did>
    </c04>
    <c04 id="ref6606" level="file">
        <did>
            <container label="Box" type="Box">154</container>
            <unittitle>Negatives</unittitle>
        </did>
    </c04>
</c03>

Thanks in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:21:15+00:00Added an answer on May 14, 2026 at 7:21 am

    Try the following code:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:output indent="yes"></xsl:output>
    
    <xsl:template match="node() | @*">
      <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
      </xsl:copy>
    </xsl:template>
    
      <xsl:template match="c03/did">
        <xsl:choose>
          <xsl:when test="not(container)">
            <did>
              <!-- If no c03 container item is found, look in the c04 level for one -->
              <xsl:if test="../c04/did/container">
                <xsl:variable name="foo" select="../c04/did/container[@type='Box']/text()"/>
                <!-- If a c04 container item is found, use the info to build a c03 version -->
                <!-- Skip c03 container item, if still no c04 items found -->
                <container label="Box" type="Box">
    
                  <!-- Build container list -->
                  <!-- Test for more than one item, and if so, list them, -->
                  <!-- separated by commas and a space -->
                  <xsl:for-each select="distinct-values($foo)">
                    <xsl:sort />
                    <xsl:if test="position() &gt; 1">, </xsl:if>
                    <xsl:value-of select="." />
                  </xsl:for-each>
                </container>
                <xsl:apply-templates select="*" />
              </xsl:if>
            </did>
          </xsl:when>
    
          <!-- If there is a c03 container item(s), list it normally -->
          <xsl:otherwise>
            <xsl:copy-of select="."/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
    </xsl:stylesheet>
    

    It looks pretty much as the output you want:

    <?xml version="1.0" encoding="UTF-8"?>
    <c03 id="ref6488" level="file">
      <did>
          <container label="Box" type="Box">154, 156</container>
          <unittitle>Clinic Building</unittitle>
          <unitdate era="ce" calendar="gregorian">1947</unitdate>
       </did>
      <c04 id="ref34582" level="file">
          <did>
             <container label="Box" type="Box">156</container>
             <container label="Folder" type="Folder">3</container>
          </did>
      </c04>
      <c04 id="ref6540" level="file">
          <did>
             <container label="Box" type="Box">156</container>
             <unittitle>Contact prints</unittitle>
          </did>
      </c04>
      <c04 id="ref6606" level="file">
          <did>
             <container label="Box" type="Box">154</container>
             <unittitle>Negatives</unittitle>
          </did>
      </c04>
    </c03>
    

    The trick is to use <xsl:sort> and distinct-values() together. See the (IMHO) great book from Michael Key “XSLT 2.0 and XPATH 2.0”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How i Prevent Duplicate Entries in database at the time of form submit and
Is there an easy or at least elegant way to prevent duplicate entries in
I want to prevent XSS attacks in my web application. I found that HTML
Is it possible to prevent an asp.net Hyperlink control from linking, i.e. so that
I need to prevent Session Fixation , a particular type of session hijacking, in
How do I prevent vim from replacing spaces with tabs when autoindent is on?
Does TCP/IP prevent multiple copies of the same packet from reaching the destination? Or
How do I prevent my users from accessing directly pages meant for ajax calls
How can I prevent a user from resizing GridViewColumns withing a ListView control?
Is it possible to prevent stack allocation of an object and only allow it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.