Topic
  • No replies
SystemAdmin
SystemAdmin
76 Posts

Pinned topic XML2XML, Problem setting ENDTAG

‏2003-11-28T10:50:05Z |
I want to convert an existing XML Document to an other XML Document using
XSLT. The new Document
sould include new Group-Main-Tags with the following rule:
AAA -> Group1 (AAB and AAC) repeat:0..n
BBB -> Group2 (subtag of Group1, with BBA,BBC) repeat:0..n

The Source-Document looks like this:
====================================
<MAIN>
<MSG>1.0</MSG>
<ORG>2.0</ORG>
<AAA>3.0.0</AAA>
<AAB>3.0.1</AAB>
<AAC>3.0.2</AAC>
<BBB>4.0.0</BBB>
<BBA>4.0.1</BBA>
<BBB>4.1.0</BBB>
<BBA>4.1.1</BBA>
<AAA>3.1.0</AAA>
<AAB>3.1.1</AAB>
<AAA>3.2.0</AAA>
<BBB>4.1.0</BBB>
<BBA>4.1.1</BBA>
<BBC>4.1.2</BBC>
</MAIN>

The new Document sould look like this:
======================================

<?xml version="1.0" encoding="UTF-8"?>
<MAIN>
<MSG>1.0</MSG>
<ORG>2.0</ORG>
<Group1>
<AAA>3.0.0</AAA>
<AAB>3.0.1</AAB>
<AAC>3.0.2</AAC>
<Group2>
<BBB>4.0.0</BBB>
<BBA>4.0.1</BBA>
</Group2>
<Group2>
<BBB>4.1.0</BBB>
<BBA>4.1.1</BBA>
</Group2>
</Group1>
<Group1>
<AAA>3.1.0</AAA>
<AAB>3.1.1</AAB>
</Group1>
<Group1>
<AAA>3.2.0</AAA>
<Group2>
<BBB>4.1.0</BBB>
<BBA>4.1.1</BBA>
<BBC>4.1.2</BBC>
</Group2>
</Group1>
</MAIN>

My problem was to set the ENDTAGS. I tried to do it using XPath .... Here
the not working Stylesheet
witch generate a non well formed XML Dokument.

<!--*************************** THIS STYLESEET IS NOT WORKING CORRECT ******************
-->
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<MAIN>
<xsl:for-each select="MAIN/*">
<xsl:if test="name() = 'AAA'">
<xsl:call-template name="setEndTagBBBGroup2"/>
<xsl:call-template name="setEndTagAAAGroup1"/>
<xsl:call-template name="setBeginTagAAAGroup1"/>
</xsl:if>
<xsl:if test="name() = 'BBB'">
<xsl:call-template name="setEndTagBBBGroup2"/>
<xsl:call-template name="setBeginTagBBBGroup2"/>
</xsl:if>
<xsl:variable name="tag">
<xsl:value-of select="name()"/>
</xsl:variable>
<xsl:element name="{$tag}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:call-template name="setEndTagBBBGroup2"/>
<xsl:call-template name="setEndTagAAAGroup1"/>
</MAIN>
</xsl:template>

<xsl:template name="setEndTagAAAGroup1">
<!-- get prev node in node-set -->
<xsl:if test="preceding::AAA[1]">
<xsl:text disable-output-escaping="yes"></Group1></xsl:text>
</xsl:if>
</xsl:template>
<xsl:template name="setBeginTagAAAGroup1">
<xsl:text disable-output-escaping="yes"><Group1></xsl:text>
</xsl:template>
<xsl:template name="setEndTagBBBGroup2">
<!-- get prev node in node-set -->
<xsl:if test="preceding::BBB[1]">
<xsl:text disable-output-escaping="yes"></Group2></xsl:text>
</xsl:if>
</xsl:template>
<xsl:template name="setBeginTagBBBGroup2">
<xsl:text disable-output-escaping="yes"><Group2></xsl:text>
</xsl:template>
</xsl:stylesheet>

Has anybody a idea how to do that?
Thanks