[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Group and compare data
Hi Thomas,
Here's an XSLT 1.0 solution: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" encoding="utf-8"/> <xsl:template match="/"> <xsl:apply-templates select="//Forhandler[navn]"/> </xsl:template> <xsl:template match="Forhandler"> <xsl:text>
</xsl:text> <xsl:value-of select="navn"/> <xsl:text>
</xsl:text> <xsl:apply-templates select="*[starts-with(name(),'aabningstider')][1]" mode="recurse"/> </xsl:template> <xsl:template match="*" mode="recurse"> <xsl:param name="start-day" select="substring-after(name(),'aabningstider')"/> <xsl:variable name="this-day" select="substring-after(name(),'aabningstider')"/> <xsl:variable name="next" select="following-sibling::*[1][starts-with(name(),'aabningstider')]"/> <xsl:choose> <xsl:when test="$next and normalize-space(.) = normalize-space($next/.)"> <xsl:apply-templates select="$next" mode="recurse"> <xsl:with-param name="start-day" select="$start-day"/> </xsl:apply-templates> </xsl:when> <xsl:otherwise> <xsl:text> </xsl:text> <xsl:if test="$start-day != $this-day"> <xsl:value-of select="$start-day"/> <xsl:text>-</xsl:text> </xsl:if> <xsl:value-of select="$this-day"/> <xsl:text> </xsl:text> <xsl:value-of select="normalize-space(.)"/> <xsl:text>
</xsl:text> <xsl:apply-templates select="$next" mode="recurse"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> It's not *that* tedious is it? :) Output (after fixing one "17: 30" in the input): Dealer name Man-Fre 7:30 - 17:30 Loer 10:00 - 14:00 Soen Efter aftale Another dealer Man-Tors 07.30 - 17.30 Fre 07.30 - 16.00 Loer-Soen Lukket Note: if you control the structure of your input file, consider using a single element name and putting the day names inside an attribute like this: <aabningstider day="Man">7:30 - 17:30</aabningstider> <aabningstider day="Tirs">7:30 - 17:30</aabningstider> ... It makes selecting them quite a bit easier: "*[starts-with(name(),'aabningstider')][1]" would become "aabningstider[1]" "substring-after(name(),'aabningstider')" becomes "@day" Best regards, Anton Verakso wrote: I have som XML that looks like this:
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|