Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 18th, 2006, 06:57 AM
Registered User
 
Join Date: Jul 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Elements (EMPTY) and Attributes #Required Ian ORourke XML 10 July 14th, 2016 10:07 PM
xslt output: empty rectangle character pcase XSLT 1 December 3rd, 2007 07:39 PM
Saxon - Prevent closing of empty elements? tripecac XSLT 3 April 27th, 2007 08:22 AM
remove empty elements but not ones with attributes dupdup XSLT 1 March 3rd, 2007 07:02 PM
How to create end tag on empty elements rjonk XSLT 1 July 21st, 2006 01:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.