How to express an Inner Join in XSLT?
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 9
- Joined: Thu Oct 29, 2009 1:51 pm
How to express an Inner Join in XSLT?
My source XML is as follows:
I want to calculate 'The total distance covered by all Porsche cars'.
My query should look something like:
sum(/Data/Journey[SOMECONDITION]/@distance)
(where SOMECONDITION joins the journeys to the associated car and checks that @type = 'Porsche')
---
I'm just not quite sure of how to express this in XSLT. Any help would be appreciated.
Code: Select all
<Data>
<Car registration="1" type="Ford" />
<Car registration="2" type="Volkswagen" />
<Car registration="3" type="Volkswagen" />
<Car registration="4" type="Porsche" />
<Car registration="5" type="Porsche" />
<Journey car="1" distance="10" />
<Journey car="1" distance="15" />
<Journey car="3" distance="5" />
<Journey car="2" distance="6" />
</Data>
My query should look something like:
sum(/Data/Journey[SOMECONDITION]/@distance)
(where SOMECONDITION joins the journeys to the associated car and checks that @type = 'Porsche')
---
I'm just not quite sure of how to express this in XSLT. Any help would be appreciated.
-
- Posts: 9
- Joined: Thu Oct 29, 2009 1:51 pm
Re: How to express an Inner Join in XSLT?
Note: I realise the example source code I provided will produce a result of zero for 'Distance covered by all Porsche cars', but hopefully you get the idea.
The results would be:
Ford: 25
Volkswagen: 11
Porsche: 0
The results would be:
Ford: 25
Volkswagen: 11
Porsche: 0
-
- Posts: 9
- Joined: Thu Oct 29, 2009 1:51 pm
Re: How to express an Inner Join in XSLT?
Ok, I managed to solve it using 2 lines:
Can this be expressed in one line?
Code: Select all
<xsl:variable name="volkswagenCars" select="/Data/Car[@type = 'Volkswagen']" />
<xsl:variable name="totalVolkswagenDistance" select="sum(/Data/Journey[@car = $volkswagenCars/@registration]/@distance)" />
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Re: How to express an Inner Join in XSLT?
Post by sorin_ristache »
Hello,
I don't think you can express it in one line if you want to process all the Car elements. For example:
Regards,
Sorin
I don't think you can express it in one line if you want to process all the Car elements. For example:
Code: Select all
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="Data">
<distances>
<xsl:for-each-group select="Car" group-by="@type">
<car type="{current-grouping-key()}" distance="{sum(for $i in current-group() return
following-sibling::Journey[@car = $i/@registration]/@distance)}"/>
</xsl:for-each-group>
</distances>
</xsl:template>
</xsl:stylesheet>
Regards,
Sorin
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Re: How to express an Inner Join in XSLT?
Post by sorin_ristache »
Actually the condition can be written as a single line (if you ignore the xsl:for-each-group element):
Regards,
Sorin
Code: Select all
<xsl:for-each-group select="Car" group-by="@type">
<car type="{current-grouping-key()}" distance="{sum(following-sibling::Journey[@car = current-group()/@registration]/@distance)}"/>
</xsl:for-each-group>
Regards,
Sorin
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ Artificial Intelligence (AI Positron Assistant add-on)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service