Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Group XML by category

Group XML by category

Scheduled Pinned Locked Moved XML / XSL
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blindcapt
    wrote on last edited by
    #1

    I was hoping someone could give me a hand with this. I'd like to convert a standard XML file into one that groups the data by one of the data elements. Here is a sample of data I'd be using: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Inventory> <Item> <producttype>Book</producttype> <productname>A Connecticut Yankee in King Arthur's Court</productname> <artistfirstname>Mark</artistfirstname> <artistlastname>Twain</artistlastname> <description>A Connecticut Yankee travels back to King Arthur's court and adventure ensues. Fun, fun, fun.</description> <company>Pocket Books</company> <year>1889</year> <productid>978-1416534730</productid> <price>4.95</price> <image/> </Item> <Item> <producttype>Book</producttype> <productname>A Farewell To Arms</productname> <artistfirstname>Ernest</artistfirstname> <artistlastname>Hemingway</artistlastname> <description>The tragic tale of love and loss during WWI.</description> <company>Charles Scribner's Sons</company> <year>1929</year> <productid>978-0684837888</productid> <price>18.15</price> <image href="file://images/978-0684837888.jpg"/> </Item> <Item> <producttype>Book</producttype> <productname>A Portrait of the Artist as a Young Man</productname> <artistfirstname>James</artistfirstname> <artistlastname>Joyce</artistlastname> <description>A a fictionalized memoir of Joyce's alter ego, Stephen Daedelus, describing his coming of age in Dublin.</description> <company>Everyman's Library</company> <year>1916</year> <productid>978-0679405757</productid> <price>14.25</price> <image href="file://images/978-0679405757.jpg"/> </Item> <Item> <producttype>Audiobook</producttype> <productname>A Tale of Two Cities</productname> <artistfirstname>Charles</artistfirstname> <artistlastname>Dickens</artistlastname> <description>Classic telling of life in two cities, London and Paris, during the upheaval of the French Revolution.</description> <company>Blackstone Audiobooks</company> <year>

    L L 2 Replies Last reply
    0
    • B blindcapt

      I was hoping someone could give me a hand with this. I'd like to convert a standard XML file into one that groups the data by one of the data elements. Here is a sample of data I'd be using: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Inventory> <Item> <producttype>Book</producttype> <productname>A Connecticut Yankee in King Arthur's Court</productname> <artistfirstname>Mark</artistfirstname> <artistlastname>Twain</artistlastname> <description>A Connecticut Yankee travels back to King Arthur's court and adventure ensues. Fun, fun, fun.</description> <company>Pocket Books</company> <year>1889</year> <productid>978-1416534730</productid> <price>4.95</price> <image/> </Item> <Item> <producttype>Book</producttype> <productname>A Farewell To Arms</productname> <artistfirstname>Ernest</artistfirstname> <artistlastname>Hemingway</artistlastname> <description>The tragic tale of love and loss during WWI.</description> <company>Charles Scribner's Sons</company> <year>1929</year> <productid>978-0684837888</productid> <price>18.15</price> <image href="file://images/978-0684837888.jpg"/> </Item> <Item> <producttype>Book</producttype> <productname>A Portrait of the Artist as a Young Man</productname> <artistfirstname>James</artistfirstname> <artistlastname>Joyce</artistlastname> <description>A a fictionalized memoir of Joyce's alter ego, Stephen Daedelus, describing his coming of age in Dublin.</description> <company>Everyman's Library</company> <year>1916</year> <productid>978-0679405757</productid> <price>14.25</price> <image href="file://images/978-0679405757.jpg"/> </Item> <Item> <producttype>Audiobook</producttype> <productname>A Tale of Two Cities</productname> <artistfirstname>Charles</artistfirstname> <artistlastname>Dickens</artistlastname> <description>Classic telling of life in two cities, London and Paris, during the upheaval of the French Revolution.</description> <company>Blackstone Audiobooks</company> <year>

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      blindcapt wrote:

      Does anyone know how to write an XSLT to create this kind of grouping?

      What kind of grouping? I'm not going to reverse engineer your XML to decipher the problem statement for you. If you understand your problem clearly, you should have no trouble stating it in a post.

      B 1 Reply Last reply
      0
      • L led mike

        blindcapt wrote:

        Does anyone know how to write an XSLT to create this kind of grouping?

        What kind of grouping? I'm not going to reverse engineer your XML to decipher the problem statement for you. If you understand your problem clearly, you should have no trouble stating it in a post.

        B Offline
        B Offline
        blindcapt
        wrote on last edited by
        #3

        Each record contains these elements: producttype, productname, productid, productprice I want the elements to group under the producttype element, such as: Original: <products> <item>  producttype  productname  productid  productprice </item> <item> producttype productname productid productprice </item> </products> Desired: <products> <category> <producttype></producttype> <item> productname productid productprice </item> <item> productname productid productprice </item> </category> <category> <producttype></producttype> <item> productname productid productprice </item> <item> productname productid productprice </item> </category> </products>

        1 Reply Last reply
        0
        • B blindcapt

          I was hoping someone could give me a hand with this. I'd like to convert a standard XML file into one that groups the data by one of the data elements. Here is a sample of data I'd be using: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Inventory> <Item> <producttype>Book</producttype> <productname>A Connecticut Yankee in King Arthur's Court</productname> <artistfirstname>Mark</artistfirstname> <artistlastname>Twain</artistlastname> <description>A Connecticut Yankee travels back to King Arthur's court and adventure ensues. Fun, fun, fun.</description> <company>Pocket Books</company> <year>1889</year> <productid>978-1416534730</productid> <price>4.95</price> <image/> </Item> <Item> <producttype>Book</producttype> <productname>A Farewell To Arms</productname> <artistfirstname>Ernest</artistfirstname> <artistlastname>Hemingway</artistlastname> <description>The tragic tale of love and loss during WWI.</description> <company>Charles Scribner's Sons</company> <year>1929</year> <productid>978-0684837888</productid> <price>18.15</price> <image href="file://images/978-0684837888.jpg"/> </Item> <Item> <producttype>Book</producttype> <productname>A Portrait of the Artist as a Young Man</productname> <artistfirstname>James</artistfirstname> <artistlastname>Joyce</artistlastname> <description>A a fictionalized memoir of Joyce's alter ego, Stephen Daedelus, describing his coming of age in Dublin.</description> <company>Everyman's Library</company> <year>1916</year> <productid>978-0679405757</productid> <price>14.25</price> <image href="file://images/978-0679405757.jpg"/> </Item> <Item> <producttype>Audiobook</producttype> <productname>A Tale of Two Cities</productname> <artistfirstname>Charles</artistfirstname> <artistlastname>Dickens</artistlastname> <description>Classic telling of life in two cities, London and Paris, during the upheaval of the French Revolution.</description> <company>Blackstone Audiobooks</company> <year>

          L Offline
          L Offline
          Lee Humphries
          wrote on last edited by
          #4

          The technique for this is known as the Muenchian Method (or Muenchian grouping[^]) and it involves the use of xsl:key in a rather clever way.

          I just love Koalas - they go great with Bacon.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups