2

I have a video player which is fed by a php file which gathers data from MySql, and outputs it as xml as below:

Current PHP/XML Output

     <?xml version="1.0" ?> 
<CONTENT>
  <GALLERY name="John" vid="1" vidtitle="NL - 22nd Jan 2011 - FO" sport="Soccer" /> 
  <GALLERY name="John" vid="2" vidtitle="NL - 22nd Jan 2011 - DL" sport="Golf" /> 
  <GALLERY name="sportshound" vid="28" vidtitle="Tiger Woods" sport="Golf" /> 
  <GALLERY name="sportshound" vid="29" vidtitle="Tigerwoodstest" sport="Golf" /> 
  <GALLERY name="John" vid="36" vidtitle="5 iron behind April" sport="Golf" /> 
  <GALLERY name="John" vid="35" vidtitle="face on april" sport="Golf" /> 
  <GALLERY name="John" vid="34" vidtitle="wqetfgtgdijuserf" sport="Golf" /> 
  <GALLERY name="John" vid="37" vidtitle="April - 3 iron Behind" sport="Golf" /> 
  <GALLERY name="John" vid="38" vidtitle="April - 7 iron behind" sport="Golf" /> 
  <GALLERY name="John" vid="39" vidtitle="April - 3 wood behind" sport="Golf" /> 
  <GALLERY name="John" vid="40" vidtitle="24 April - 7 iron behind" sport="Golf" /> 
  <GALLERY name="John" vid="41" vidtitle="April 29 Iron behind swing left" sport="Golf" /> 
  <GALLERY name="John" vid="42" vidtitle="29 April iron behind shallowing" sport="Golf" /> 
  <GALLERY name="John" vid="43" vidtitle="1st May Driver Behind" sport="Golf" /> 
  <GALLERY name="John" vid="44" vidtitle="21st May - 6I behind - swing left" sport="Golf" /> 
  <GALLERY name="John" vid="45" vidtitle="Adam Scott - Masters '11 - iron behind" sport="Golf" /> 
  <GALLERY name="John" vid="46" vidtitle="19th June 2011 - Face on - impact" sport="Golf" /> 
  <GALLERY name="John" vid="47" vidtitle="19 June - Behind - 6i" sport="Golf" /> 
  <GALLERY name="John" vid="48" vidtitle="19 June 2011 - Face on - 8i (impact)" sport="Golf" /> 
  <GALLERY name="John" vid="49" vidtitle="19 June 2011 - Face On - 5i (impact)" sport="Golf" /> 
  </CONTENT>

I have been looking for weeks for a way to transform the structure using XSLT so that the output is structured by Gallery, then by sport. Any help on this would be greatly appreciated!

Proposed structure

<CONTENT>
  <GALLERY name="John">
    <CATEGORY sport="Soccer">
      <ITEM>
         <vid>1</vid>
      </ITEM>
    </CATEGORY>
    <CATEGORY sport="Golf">
      <ITEM>
        <vid>2</vid>
        <vid>36</vid>
         .....
      </ITEM>
   </CATEGORY>
  <GALLERY/>
  <GALLERY name="sportshound">
    <CATEGORY sport="Golf">
      <ITEM>
        <vid>28</vid>
        <vid>29</vid>
      </ITEM>
    </CATEGORY>
  <GALLERY/>
</CONTENT>

2 Answers 2

3

Here is a sample stylesheet:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output indent="yes"/>

  <xsl:key name="k1" match="GALLERY" use="@name"/>
  <xsl:key name="k2" match="GALLERY" use="concat(@name, '|', @sport)"/>

  <xsl:template match="CONTENT">
    <xsl:copy>
      <xsl:apply-templates select="GALLERY[generate-id() = generate-id(key('k1', @name)[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="GALLERY">
    <GALLERY name="{@name}">
      <xsl:apply-templates select="key('k1', @name)[generate-id() = generate-id(key('k2', concat(@name, '|', @sport))[1])]" mode="sport"/>
    </GALLERY>
  </xsl:template>

  <xsl:template match="GALLERY" mode="sport">
    <CATEGORY sport="{@sport}">
      <ITEM>
        <xsl:apply-templates select="key('k2', concat(@name, '|', @sport))/@vid"/>
      </ITEM>
    </CATEGORY>
  </xsl:template>

  <xsl:template match="GALLERY/@vid">
    <vid>
      <xsl:value-of select="."/>
    </vid>
  </xsl:template>

</xsl:stylesheet>

To read more about that XSLT 1.0 approach called Muenchian grouping visit http://www.jenitennison.com/xslt/grouping/muenchian.xml.

Sign up to request clarification or add additional context in comments.

Comments

1

Here's the XSLT 2.0 solution based on the new grouping feature.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="CONTENT">
        <CONTENT>
            <xsl:for-each-group select="GALLERY" 
                group-by="@name">
                <GALLERY name="{current-grouping-key()}">
                    <xsl:for-each-group select="current-group()" 
                        group-by="@sport">
                        <CATEGORY sport="{current-grouping-key()}">
                            <ITEM>
                                <xsl:apply-templates select="current-group()"/>
                            </ITEM>
                        </CATEGORY>
                    </xsl:for-each-group>
                </GALLERY>          
            </xsl:for-each-group>
        </CONTENT>
    </xsl:template>

    <xsl:template match="GALLERY">
        <vid><xsl:value-of select="@vid"/></vid>
    </xsl:template>

</xsl:stylesheet>

The produced output is:

<CONTENT>
   <GALLERY name="John">
      <CATEGORY sport="Soccer">
         <ITEM>
            <vid>1</vid>
         </ITEM>
      </CATEGORY>
      <CATEGORY sport="Golf">
         <ITEM>
            <vid>2</vid>
            <vid>36</vid>
            <vid>35</vid>
            <vid>34</vid>
            <vid>37</vid>
            <vid>38</vid>
            <vid>39</vid>
            <vid>40</vid>
            <vid>41</vid>
            <vid>42</vid>
            <vid>43</vid>
            <vid>44</vid>
            <vid>45</vid>
            <vid>46</vid>
            <vid>47</vid>
            <vid>48</vid>
            <vid>49</vid>
         </ITEM>
      </CATEGORY>
   </GALLERY>
   <GALLERY name="sportshound">
      <CATEGORY sport="Golf">
         <ITEM>
            <vid>28</vid>
            <vid>29</vid>
         </ITEM>
      </CATEGORY>
   </GALLERY>
</CONTENT>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.