• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Extracting XSL content to XML with XSLT problem ...

Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that I am trying to extract content from an XSL doc using XSLT to transform into XML
(see the example below). Problem is that I am getting the text from the previous sections of the XSL,
which is unwanted (notice the 'This is the chapter' text). Can someone help?
Original XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
<xsl:template match="/"> <!--root rule-->
<h1><xsl:value-of select="doc/title"/></h1>
<xsl:for-each select="doc/chapter">
This is the chapter:<br/>
<h2><xsl:value-of select="title"/></h2>
<xsl:apply-templates />

<foo:months locale="en-us">

Applied transformation:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
<xsl utput method="xml" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates />
<xsl:template match="foo:months[@locale='en-us']">
<xsl:element name="locale">
<xsl:attribute name="id"><xsl:value-of select="@locale"/></xsl:attribute>
<xsl:apply-templates select="month"/>

<xsl:template match="month">
<xsl:if test="parent::*[@locale='en-us']">
<xsl:element name="month">
<xsl:value-of select="."/>
Output XML:
<?xml version="1.0" encoding="utf-8"?>
This is the chapter:<locale id="en-us">
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, this is what happens: you match "/". So that is ok. Then you match foo:months and month. So those elements are taken care off. But you still have the xsl:template element and all the HTML elements inside in your original stylesheet. The default templates for elements, text, comments are used to transform them. Since the default template for elements is basically another xsl:apply-templates, you don't get any output. But the default template for text is to output the text to the output, hence the "This is the chapter:" string in your output.
The solution is to match the xsl:template element and then ignore it, like this:
<xsl:template match='xsl:template'/>
Add the previous line to your (second) XSLT document, then the string will go away.
Khun Yee Fung
Author of XSLT: Working with XML and HTML
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!