XSLT: Empty elements not copied
Hi,
I would like to transform the following XML file with "positional grouping" to another XML file to generate a chapter structure. This works pretty good, if the elements are not EMPTY. (If one Element is empty the green Elements are ignored)
This is an example source XML file:
<?xml version="1.0" encoding="utf-8"?>
<Document>
<Metainformation>
<pDocumentTitle>Title of Document</pDocumentTitle>
<pDocumentType>Handbook</pDocumentType>
<aDocumentVersion>1.0</aDocumentVersion>
</Metainformation>
<Content>
<pHeading1>My first Heading</pHeading1>
<pText>This is a Text Element, which ist not empty.</pText>
<pText name="emptyTextA"/>
<pText name="emptyTextB"/>
<pText name="emptyTextC"/>
<pText>This is another Text Element, which is not empty</pText>
<pHeading1>GröÃte Ãberschrift (eins)</pHeading1>
<pImage href="file:///H:/Temp/veo/icons/emoticons/smile.gif">This ist a smiliey</pImage>
<pImage href="file:///H:/Temp/veo/icons/emoticons/sad.gif">This is another icon</pImage>
<pImage href="file:///H:/Temp/veo/icons/emoticons/tongue.gif"/>
<pImage href="file:///H:/Temp/veo/icons/emoticons/biggrin.gif"/>
<pImage href="file:///H:/Temp/veo/icons/emoticons/wink.gif"/>
</Content>
</Document>
And this is the XSLT stylesheet, which transforms the flat XML structure to generate chapters:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:element name="Document">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Metainformation">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Content">
<xsl:element name="Content">
<xsl:for-each select="./pHeading1">
<xsl:element name="chapter">
<xsl:copy-of select="."/>
<xsl:call-template name="generateChapter">
<xsl:with-param name="kNode" select="following-sibling::*"/>
<xsl:with-param name="kPos" select="1"/>
</xsl:call-template>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name="generateChapter">
<xsl:param name="kNode"/>
<xsl:param name="kPos"/>
<xsl:if test="name($kNode[$kPos])!='pHeading1'">
<xsl:copy-of select="$kNode[$kPos]"/>
<xsl:if test="$kNode[$kPos]!=$kNode[last()]">
<xsl:call-template name="generateChapter">
<xsl:with-param name="kNode" select="$kNode"/>
<xsl:with-param name="kPos" select="$kPos+1"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
The following XML file is the result. If one Element is empty (red), the following sibling Elemnts are ignored by the transformation:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<Metainformation>
<pDocumentTitle>Title of Document</pDocumentTitle>
<pDocumentType>Handbook</pDocumentType>
<aDocumentVersion>1.0</aDocumentVersion>
</Metainformation>
<Content>
<chapter>
<pHeading1>My first Heading</pHeading1>
<pText>This is a Text Element, which ist not empty.</pText>
<pText name="emptyTextA"/>
</chapter>
<chapter>
<pHeading1>GröÃte Ãberschrift (eins)</pHeading1>
<pImage href="file:///H:/Temp/veo/icons/emoticons/smile.gif">This ist a smiliey</pImage>
<pImage href="file:///H:/Temp/veo/icons/emoticons/sad.gif">This is another icon</pImage>
<pImage href="file:///H:/Temp/veo/icons/emoticons/tongue.gif"/>
</chapter>
</Content>
</Document>
Does somebody know this problem? How can I copy the whole structure inclusively the EMPTY Elements?
Many thanks in advance.
Borg
|