You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Matthew Cordes <mc...@maine.edu> on 2000/04/10 04:40:14 UTC

What stylesheet might I use to parse the xml..

Hello, I have some xml that I want to reorder, but leave in xml format 
via an xsl.  What I have right now isn't working, please help.


Starting xml:

<resultset>
    <result>
        <name>  Art </name>
        <age>   22  </age>
        <course>    mes-399 </course>
    </result>
    <result>
        <name>  Pat </name>
        <age>   22  </age>
        <course>    cos-399 </course>
    </result>
    <result>
        <name>  Stan </name>
        <age>   45  </age>
        <course>    mes-399 </course>
    </result>
    <result>
        <name>  Jimmy </name>
        <age>   23  </age>
        <course>    cos-399 </course>
    </result>
</resultset>


Here is the stylesheet I used,

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

<xsl:template match="/">
    <courses>
        <xsl:apply-templates select="/resultset/result"/>
    </courses>
</xsl:template>

<xsl:template match="result">
    <course>
        <xsl:attribute name="title">
            <xsl:value-of select="course"/>
        </xsl:attribute>
		<student>
	        <name> <xsl:value-of select="name"/> </name>
	        <age> <xsl:value-of select="age"/> </age>
		</student>
    </course>
</xsl:template>
</xsl:stylesheet>


together these output: 


<?xml version="1.0" encoding="ISO-8859-1"?>
<courses>
	<course title="mes-399">
		<student>
			<name> Art </name>
			<age>22</age>
		</student>	
	</course>
	<course title="cos-399">
		<student>
			<name> Pat </name>
			<age>22</age>
		</student>
	</course>
	<course title="mes-399">
		<student>
			<name> Stan </name>
			<age>45</age>
		</student>
	</course>
	<course title="cos-399">
		<student>
			<name> Jimmy </name>
			<age>23</age>
		</student>
	</course>
</courses>


when I really wanted this output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<courses>
	<course title="mes-399">
		<student>
			<name> Art </name>
			<age>22</age>
		</student>
		<student>
			<name> Stan </name>
			<age>45</age>
		</student>
	</course>
	<course title="cos-399">
		<student>
			<name> Pat </name>
			<age>22</age>
		</student>	
		<student>
			<name> Jimmy </name>
			<age>23</age>
		</student>	
	</course>
</courses>

Notice how the desired xml above is grouped by course, while the real
output is not.  Can anyone help me do it this way?

Perhaps also, some kind of xsl that would allow me to compress nodes with
the same name would also work (e.g., above, <course title="cos-399">
appears twice, but I really only want it once ).

Thanks for reading this,

-matt