Jump to content

xslt restructuring


gregmystikal

Recommended Posts

Hi,Pretty new to the world of xml/xslt so this problem may be v. simple :) then again it may not be...Ok, my problem is that i would like to re-arrange some simple xml using xslt. An example of my xml is:<example><exampleID>xxx-123-zzz</exampleID><name>aName</name><details>some details go here</details></example><example><exampleID>yxz-654-zyx</exampleID><name>anotherName</name><details>some details go here 2</details></example><example><exampleID>xxx-123-zzz</exampleID><name>aDifferentName</name><details>some details go here 3</details></example>I would like to transform/restructure this with xslt so that for each unique exampleID there is a new element e.g.:<exampleID value='xxx-123-zzz' ><aName>some details go here</aName><aDifferentName>some details go here 3</aDifferentName></exampleID><exampleID value='yxz-654-zyx'><anotherName>some details go here 2</anotherName></exampleID>As you can see the output should have an element (i hope this is the correct word!) for each unique example ID from the xml. Within each of these should be an element called the name from the xml, with the text value of the details element. I hope this makes sense. Really not good at trying to explain my problem! I know that there is a 'for each' function in xslt, but im having trouble looping this to get the above format. I've looped it and used the function:preceding-sibling::PatientResponse[1]/exampleID"to check whether the previous sibling was the same or not, but this isnt doing what i need it to in terms of putting my data in the correct format!Apologies if my example is not very clear, i am new to xml/xslt. Any help would be very much appreciated :) Cheers,Greg

Link to comment
Share on other sites

Instead of trying to get all exampleID elements (the elements you WILL have), you may loop over all example elements (the elements you CURRENTLY have) instead, and then output stuff in the way you want to, like:

<xsl:for-each select="/*/example">	<exampleID value="{exampleID}">		<xsl:element name="{name}"><xsl:value-of select="details" /></xsl:element>	</exampleID></xsl:for-each>

Link to comment
Share on other sites

That is a grouping problem you can solve as follows with XSLT 2.0:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">    <xsl:output method="xml" indent="yes"/>  <xsl:strip-space elements="*"/>    <xsl:template match="root">	<xsl:copy>	  <xsl:for-each-group select="example" group-by="exampleID">		<exampleID value="{current-grouping-key()}">		  <xsl:apply-templates select="current-group()"/>		</exampleID>	  </xsl:for-each-group>	</xsl:copy>  </xsl:template>    <xsl:template match="example">	<xsl:element name="{name}">	  <xsl:value-of select="details"/>	</xsl:element>  </xsl:template></xsl:stylesheet>

XSLT 2.0 can be used with XML editors like Oxygen, Stylus Studio, XML Spy and with XSLT 2.0 processors like Saxon 9 and AltovaXML Tools.With XSLT 1.0 you could use Muenchian grouping, let us know whether XSLT 2.0 is an option for you or whether you need help with an XSLT 1.0 solution.

Link to comment
Share on other sites

Hi,Thanks great, thanks very much for you answers. Using XSLT 2.0 shouldnt be a problem so im going to try this out.Thanks Again :)

That is a grouping problem you can solve as follows with XSLT 2.0:
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">    <xsl:output method="xml" indent="yes"/>  <xsl:strip-space elements="*"/>    <xsl:template match="root">	<xsl:copy>	  <xsl:for-each-group select="example" group-by="exampleID">		<exampleID value="{current-grouping-key()}">		  <xsl:apply-templates select="current-group()"/>		</exampleID>	  </xsl:for-each-group>	</xsl:copy>  </xsl:template>    <xsl:template match="example">	<xsl:element name="{name}">	  <xsl:value-of select="details"/>	</xsl:element>  </xsl:template></xsl:stylesheet>

XSLT 2.0 can be used with XML editors like Oxygen, Stylus Studio, XML Spy and with XSLT 2.0 processors like Saxon 9 and AltovaXML Tools.With XSLT 1.0 you could use Muenchian grouping, let us know whether XSLT 2.0 is an option for you or whether you need help with an XSLT 1.0 solution.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...