Group XML by category
-
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>
-
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>
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.
-
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.
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>
-
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>
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.