Reputation: 11
I have the following source-xml-file and would like to transform it with xsl into another xml-file. How can i check if an Element exists and depending on this result create a new one or append a child to the existing element?
<Vortriebsorte>
<Vortriebsort>
<Name>Südröhre</Name>
<Bauphase>Kalotte</Bauphase>
<Vortrieb>Südröhre</Vortrieb>
</Vortriebsort>
<Vortriebsort>
<Name>Nordröhre</Name>
<Bauphase>Strosse</Bauphase>
<Vortrieb>Nordröhre</Vortrieb>
</Vortriebsort>
<Vortriebsort>
<Name>Südröhre</Name>
<Bauphase>Strosse / Sohle</Bauphase>
<Vortrieb>Südröhre</Vortrieb>
</Vortriebsort>
</Vortriebsorte>
the result-xml-file should look like the following snippet:
<data>
<group name="Abschlagsdaten">
<group name="Vortrieb: Südröhre">
<group name="Bauphase: Kalotte">
<!--- some more stuff -->
</group>
<group name="Bauphase: Strosse / Sohle">
<!--- some more stuff -->
</group>
</group>
<group name="Vortrieb: Nordröhre">
<group name="Bauphase: Strosse / Sohle">
<!--- some more stuff -->
</group>
</group>
</group>
</data>
the element "Bauphase" should be grouped by the element "Vortrieb" in the result-file. i was able to produce a valid output without grouping by "Vortrieb".
as i'm new in xsl-tranformation i would appreciate if someone can give me a hint if this is possible and if so how?
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="Vortriebsorte">
<data>
<xsl:for-each select="Vortriebsort">
<!-- if contains element append new element <group name="Bauphase: ..."> to the existing element -->
<xsl:if test="(contains(@name, \"Vortrieb: \"<xsl:value-of select=\"Vortrieb\" />))">
<group>
<xsl:attribute name="name">Abschlag: <xsl:value-of select="Bauphase" /> </xsl:attribute>
<xsl:apply-templates select="Tunnelbandgruppen/Tunnelbandgruppe"/>
</group>
</xsl:if>
<!-- if NOT contains element add <group name="Vortrieb: ..."> and apply templates for child-nodes -->
<xsl:if test="not(contains(@name, \"Vortrieb: \"<xsl:value-of select=\"Vortrieb\" />))">
<group>
<xsl:attribute name="name">Vortrieb: <xsl:value-of select="Vortrieb" /> </xsl:attribute>
<group>
<xsl:attribute name="name">Abschlag: <xsl:value-of select="Bauphase" /> </xsl:attribute>
<xsl:apply-templates select="Tunnelbandgruppen/Tunnelbandgruppe"/>
</group>
</group>
</xsl:if>-->
</xsl:for-each>
</data>
</tunneltracer-exchange-file>
</xsl:template>
</xsl:stylesheet>
kind regards, Markus
Upvotes: 1
Views: 1023
Reputation: 23637
You can use Muenchian grouping in XSLT 1.0 for grouping.
Here is a stylesheet using that technique which produces your expected result:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="vortrieb-group" match="Vortrieb" use="."/>
<xsl:key name="bauphase-group" match="Bauphase" use="."/>
<xsl:template match="Vortriebsorte">
<data>
<group name="Abschlagsdaten">
<xsl:apply-templates/>
</group>
</data>
</xsl:template>
<xsl:template match="Vortriebsort">
<xsl:apply-templates select="Vortrieb[generate-id(.)=generate-id(key('vortrieb-group', .))]"/>
</xsl:template>
<xsl:template match="Vortrieb">
<group name="Vortrieb: {.}">
<xsl:apply-templates select="//Bauphase[../Vortrieb=current()][generate-id(.)=generate-id(key('bauphase-group', .))]"/>
</group>
</xsl:template>
<xsl:template match="Bauphase">
<group name="Bauphase: {.}">
<xsl:value-of select="."/>
</group>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 6615
You could try using this template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<data>
<group name="Abschlagsdaten">
<xsl:apply-templates select="Vortriebsorte/Vortriebsort[not(./Name=preceding::*/Name)]"/>
</group>
</data>
</xsl:template>
<xsl:template match="Vortriebsort">
<xsl:variable name="n" select="./Name"/>
<group>
<xsl:attribute name="name">
<xsl:value-of select="'Vortrieb: '"/>
<xsl:value-of select="./Name"/>
</xsl:attribute>
<xsl:apply-templates select="//Bauphase[../Name = $n]"/>
</group>
</xsl:template>
<xsl:template match="Bauphase">
<group>
<xsl:attribute name="name">
<xsl:value-of select="'Bauphase: '"/>
<xsl:value-of select="."/>
</xsl:attribute>
</group>
</xsl:template>
</xsl:stylesheet>
It will find only distinct Vortriebsort
nodes by their Name
, and then search for all Bauphase nodes that have the same value as Name
.
It will produce this XML:
<data>
<group name="Abschlagsdaten">
<group name="Vortrieb: Südröhre">
<group name="Bauphase: Kalotte" />
<group name="Bauphase: Strosse / Sohle" />
</group>
<group name="Vortrieb: Nordröhre">
<group name="Bauphase: Strosse" />
</group>
</group>
</data>
Upvotes: 1