Question

Following big problem:

<root>
<div>
    <programm></programm>
    <systemes><p></p></systemes>
    <systemes><table>.1.</table></systemes>
    <systemes><table>.2.</table></systemes>
    <systemes><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table></systemes>
    <systemes><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table></systemes>
    <systemes><table>.2.</table></systemes>
    <systemes><p></p></systemes>
    <requirements></requirements>
</div>
</root>

I need the output to be this:

<root>
<div>
    <programm></programm>
    <systemes><p></p><table>.1.</table><table>.2.</table><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table><table>.2.</table><p></p></systemes>
    <requirements></requirements>
</div>
</root>

I hope someone can help me with this problem. I know the Muenchian Method but dont get it to work properly. Thank you very much!

This is what I tried so far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

<xsl:strip-space elements="*"/>

<xsl:key name="systemsKey" match="//systemes" use="name()"/>

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="systemes[generate-id()=generate-id(key('systemesKey', name())[1])]">
    <xsl:copy>
        <xsl:apply-templates select="@*|key('systemesKey', name())/node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="systemes[not(generate-id()=generate-id(key('systemesKey', name())[1]))]"/>

</xsl:stylesheet>
Was it helpful?

Solution

This produces exactly the output you described (except for the order of childs inside systemes elements).

<?xml version="1.0" encoding="utf-8"?>

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="root">
  <xsl:copy>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="requirements|programm|p">
  <xsl:copy>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="div">
  <xsl:copy>
     <xsl:element name="systemes">
        <xsl:for-each select="systemes">
           <xsl:apply-templates/>
        </xsl:for-each>
     </xsl:element>
     <xsl:apply-templates select="requirements|programm"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="table">
  <xsl:copy>
     <xsl:value-of select="."/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

You do not need to use grouping at all, Muenchian or not. I would not advise you use something as complex as keys if it is not necessary for the goal you want to achieve.

EDIT: I have used XSLT 2.0 but there is nothing in it which cannot be done in 1.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top