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 4246982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:02:49+00:00 2026-05-21T04:02:49+00:00

I have an XML document structured as follows <items> <item> <name>item1</name> <attributes>a,b,c,d</attributes> </item> <item>

  • 0

I have an XML document structured as follows

<items>
 <item>
  <name>item1</name>
  <attributes>a,b,c,d</attributes>
 </item>
 <item>
  <name>item2</name>
  <attributes>c,d,e</attributes>
 </item>
</items>

For each unique attribute value (delimited by commas) I need to list all item names associated with that value like so:

a : item1
b : item1
c : item1, item2
d : item1, item2
e : item2

My initial plan was to use a template to parse the attributes into Attribute nodes, surrounding each with appropriate tags, and then separating out the unique values with an XPATH expression like

Attribute[not(.=following::Attribute)]

but since the result of the template isn’t a node-set that ever goes through an XML parser, I can’t traverse it. I also tried exslt’s node-set() function only to realize it does not allow me to traverse the individual Attribute nodes either.

At this point I’m at a loss for a simple way to do this and would really appreciate any help or ideas on how to proceed. Thanks!

  • 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-21T04:02:50+00:00Added an answer on May 21, 2026 at 4:02 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:ext="http://exslt.org/common">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:key name="kAtrByVal" match="attr" use="."/>
    
     <xsl:template match="/">
      <xsl:variable name="vrtfPass1">
       <groups>
        <xsl:apply-templates/>
       </groups>
      </xsl:variable>
    
      <xsl:variable name="vPass1"
           select="ext:node-set($vrtfPass1)"/>
    
      <xsl:apply-templates select="$vPass1/*"/>
     </xsl:template>
    
     <xsl:template match="item">
      <group name="{name}">
       <xsl:apply-templates select="attributes"/>
      </group>
     </xsl:template>
    
     <xsl:template match="attributes" name="tokenize">
      <xsl:param name="pText" select="."/>
    
      <xsl:if test="string-length($pText)">
       <xsl:variable name="vText" select=
            "concat($pText,',')"/>
       <attr>
        <xsl:value-of select="substring-before($vText,',')"/>
       </attr>
       <xsl:call-template name="tokenize">
        <xsl:with-param name="pText" select=
        "substring-after($pText,',')"/>
       </xsl:call-template>
      </xsl:if>
     </xsl:template>
    
     <xsl:template match=
      "attr[generate-id()
           =
            generate-id(key('kAtrByVal',.)[1])
           ]
      ">
      <xsl:value-of select="concat('&#xA;',.,': ')"/>
    
      <xsl:for-each select="key('kAtrByVal',.)">
       <xsl:value-of select="../@name"/>
       <xsl:if test="not(position()=last())">
        <xsl:text>, </xsl:text>
       </xsl:if>
      </xsl:for-each>
     </xsl:template>
    
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <items>
        <item>
            <name>item1</name>
            <attributes>a,b,c,d</attributes>
        </item>
        <item>
            <name>item2</name>
            <attributes>c,d,e</attributes>
        </item>
    </items>
    

    produces the wanted, correct result:

    a: item1
    b: item1
    c: item1, item2
    d: item1, item2
    e: item2
    

    Explanation:

    1. Pass1: tokenization and end result:

    <groups>
      <group name="item1">
        <attr>a</attr>
        <attr>b</attr>
        <attr>c</attr>
        <attr>d</attr>
      </group>
      <group name="item2">
        <attr>c</attr>
        <attr>d</attr>
        <attr>e</attr>
      </group>
    </groups>
    

    .2. Pass2 takes the result of Pass1 (converted to a nodeset using the extension function ext:node-set()) as input, performs Muenchian grouping and produces the final, wanted result.

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

Sidebar

Related Questions

I have an XML document looking similar to this: <items> <item cat=1 owner=14>bla</item> <item
i have an xml document structured like this: <NewDataSet> <videos> <video> <name>name of video</name>
I have an XML document: var xml:XML = new XML(<rootNode> <head> <meta name=template content=Default
I have an XML file which is structured as follows: <row> <store>place1</store> <location>location1</location> </row>
I have an xml sitemap structured like a document tree, such that it looks
I have an XML document with un-namespaced elements, and I want to use XSLT
I have an XML document something like ::: <?xml version=1.0 encoding=utf-8?> <?mso-application progid=Excel.Sheet?> <Workbook
I have an XML document that I'm trying to style via CSS. A relevant
I have an xml document object that I need to convert into a string.
Say I have a xml document that looks like this <foo> <bar id=9 />

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.