Results 1 to 5 of 5

Thread: Removing duplications from XML using XSL styelsheets.

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    3

    Removing duplications from XML using XSL styelsheets.

    Hello im basically creating a form based system using drop down menu's where as I have an XML document, then am processing that using an XSL (HTML ORITENTATED) document, sending them both to a JSP and processing them to output xhtml.

    My problem is that in the form drop down menu from the XML I extract many peices of information which are the same. For instance say im making a list of the courses, but the 'Computer science' course is listed 3 times I want to only list that course in the menu once. I've got around this problem using a predicate as follows.

    Code:
    <xsl:apply-templates select="curriculum/modules/module/subject[not(.=preceding::subject/.)]" mode="q2d" />
    The problem in facing now is that im only extracting part of the XML from a document, for instance module codes like CSCI1401, CSCI1408, INFO2403. I only want to retreive the first 4 letters of these module codes, so to do this im using substring(.,1,4) however I can get rid of duplicates in the way that I dont have two CSCI1401's but I can't seem to get the drop down menu to just contain 1 instance of CSCI, INFO, etc.

    Any ideas on how you remove duplicates when you're trying to remove them based on a substring? It won't let me put the substring function directly into the tree structure because it requires a node ;s

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Removing duplications from XML using XSL styelsheets.

    Are you using any front-end language which you could possibly combine the xsl transformation with?

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    3

    Re: Removing duplications from XML using XSL styelsheets.

    No we're not aloud to, we have to use just XSL, XML and JSP to output XHTML. Anyway I have kinda got round the problem to some extent, using an apply template as follows.

    Code:
    <xsl:apply-templates select="curriculum/modules/module[not(substring(@code,1,4)=substring(following-sibling::module/@code,1,4))]" mode="q3fa"/>
    This works but still leaves 2 duplicates, i.e. 2 instances of CSCI and 2 of INFO in the drop down menu. I've found the reasoning for this is because theres level 1 and level 2 modules, and the CSCI's are listed in level 1, then start again in the level 2 module list, unfortunately I can't change the ordering of the XML either.

    The way I see it the only way around it is using a sort method, ive tried something like follows:

    Code:
    <xsl:apply-templates select="curriculum/modules/module[not(substring(@code,1,4)=substring(following-sibling::module/@code,1,4))]" mode="q3fa">
    										<xsl:sort select="@code"/>
    									</xsl:apply-templates>
    but it only orders the dropdown menu after the informations been extracted and so I want someway of ordering all the module/@code's then using that sorted list in my apply templates predicate. Anyone know how to do this? Trouble it don't seem to let you use variables straight into apply templates.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Removing duplications from XML using XSL styelsheets.

    Use a for-each to order them and an if to filter them after the fact.

    Something like
    Code:
    <xsl:for-each select="unfiltered expression">
      <xsl:sort by the value/>
      <xsl:if test="filter expression">
        <!-- real stuff here -->
      </xsl:if>
    </xsl:for-each>
    Or use a different axis instead of preceding-sibling, perhaps preceding. That might work.
    Last edited by CornedBee; Dec 10th, 2006 at 05:16 PM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    3

    Re: Removing duplications from XML using XSL styelsheets.

    I tried doing this:

    Code:
    <xsl:for-each select="curriculum/modules/module">
       <xsl:sort select="@code" order="ascending" />
       <xsl:if test="self::node()[not(substring(@code,1,4)=substring(following-sibling::module/@code,1,4))]">
          <xsl:apply-templates select="." mode="q3fa" />
       </xsl:if>
    </xsl:for-each>
    But it still gives me the same duplicates? doesn't seem to be ordering them how I want them too.. or ordering the XML before it applies the template?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width