XSL Transform (Request)

Status
Not open for further replies.

topham

Ars Legatus Legionis
13,756
Subscriptor
I have this:
Code:
<root>
   <item>
      <category>XYZ</category>
      <name>John Doe</name>
    </item>
    <item>
       <category>XYZ</category>
      <name>Jane Doe</name>
     </item>
     <item>
        <category>ZZZ</category>
     </item>
</root>

And want to transform it to:

Code:
<root>
    <category name="XYZ">
      <item>
         <name>John Doe</name>
      </item>
      <item>
         <name>Jane Doe</name>
      </item>
    </category>
    <category name="ZZZ">
      ...
    </category>
</root>

Answers, or a tutorial with that transformation in mind would be good. I find XSL mind-bending; and don't do it enough.

edit: fixed error in xml
 

topham

Ars Legatus Legionis
13,756
Subscriptor
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
        <xsl:output method="xml" indent="yes"/>
        <xsl:variable name="unique-categories" select="//category[not(. = preceding::category/.)]"/>
        <xsl:template match="//root">               
            <xsl:for-each select="$unique-categories">
                <xsl:variable name="cat" select="."/>
                <category name="{$cat}">
                    <xsl:for-each select="//root/item/category[.=$cat]">
                        <xsl:copy-of select="parent::*[not(local-name()='category')]"/>
                    </xsl:for-each>
                </category>
            </xsl:for-each>
        </xsl:template>    
</xsl:stylesheet>

Code:
<?xml version="1.0" encoding="UTF-8"?>
<category name="XYZ">
   <item>
        <category>XYZ</category>
        <name>John Doe</name>
    </item>
   <item>
        <category>XYZ</category>
        <name>Jane Doe</name>
    </item>
</category>
<category name="ZZZ">
   <item>
        <category>ZZZ</category>
    </item>
</category>

Close, but not quite.
 

Metasyntactic

Ars Legatus Legionis
31,808
Not sure how to do XSL, but here's C# :)

Code:
    XElement source = ...;

    var transformed = 
        new XElement("root",
            from item in source.Elements("item")
            group item by item.Element("category").Value into grouping
            select new XElement("category",
                new XAttribute("name", grouping.Key),
                from g in grouping
                let name = g.Element("name")
                where name != null
                select new XElement("item", name)));
 
XSLT 1.0 is "mindbending" in that it is missing quite a bit. Try XSLT 2.0.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="root">
    <root>
      <xsl:for-each-group select="item" group-by="category">
        <category name="{current-grouping-key()}">
          <xsl:for-each select="current-group()">
            <xsl:if test="name">
              <item>
                <name><xsl:value-of select="name"/></name>
              </item>
            </xsl:if>
          </xsl:for-each>
        </category>
      </xsl:for-each-group>
    </root>
  </xsl:template>
</xsl:stylesheet>

This gives me:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <category name="XYZ">
      <item>
         <name>John Doe</name>
      </item>
      <item>
         <name>Jane Doe</name>
      </item>
   </category>
   <category name="ZZZ"/>
</root>

on your sample data.
 

Metasyntactic

Ars Legatus Legionis
31,808
topham":zybr5je7 said:
I want to drop Meta on his head. Seriously, C#?
What about VB? It's even nicer than C#:

Code:
        Dim source As XElement =
            <root>
                <item>
                    <category>XYZ</category>
                    <name>John Doe</name>
                </item>
                <item>
                    <category>XYZ</category>
                    <name>Jane Doe</name>
                </item>
                <item>
                    <category>ZZZ</category>
                </item>
            </root>

        Dim transformed As XElement =
            <root>
                <%= From item In source.<item>
                    Group By Category = item.<category>.Value Into Group
                    Select <category name=<%= Category %>>
                               <%= From g In Group
                                   Let name = g.<name>.FirstOrDefault()
                                   Where name IsNot Nothing
                                   Select <item><%= name %></item> %>
                           </category> %>
            </root>
 

Metasyntactic

Ars Legatus Legionis
31,808
topham":v89iq19y said:
I preferred the C# one. intermixing XML tags and Code is a bastardization like PHP.
Not a fan of XSLT either for the same reason, although the 2.0 solution above wasn't really any worse than the VB one.
You're weird :)

I'm having trouble seeing why 'source.Elements("item")' is nicer than 'source.<item>' or why:

Code:
  new XElement("category",
    new XAttribute("name", grouping.Key)

is nicer than:

Code:
  <category name=<%= Category %>
Instead of having all the pointless clutter of the calls to the methods, i can just express the final shape i want.

To me this is very LINQish. Just as i can declaratively put forth my query, i can also declaratively put forth the final XML i want in the syntax that most closely matches it. The <%= %> is just a convenient way to push/pop in between the different languages. To me it's a very functional approach that i'd love to be seen made extensible to support arbitrary DSLs.
 
Status
Not open for further replies.