• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extracting XSL content to XML with XSLT problem ...

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
xmlns:foo="http://someplace.com/foo"
extension-element-prefixes="foo">
<xsl:template match="/"> <!--root rule-->
<html>
<head><title></title></head>
<body>
<h1><xsl:value-of select="doc/title"/></h1>
<hr/>
<xsl:for-each select="doc/chapter">
This is the chapter:<br/>
<h2><xsl:value-of select="title"/></h2>
<xsl:apply-templates />
</xsl:for-each>
</body>
</html>
</xsl:template>

<foo:months locale="en-us">
<month>January</month>
<month>February</month>
<month>March</month>
</foo:months>
</xsl:stylesheet>

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"
xmlns:foo="http://someplace.com/foo"
exclude-result-prefixes="xsl">
<xsl utput method="xml" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<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:element>
</xsl:template>

<xsl:template match="month">
<xsl:if test="parent::*[@locale='en-us']">
<xsl:element name="month">
<xsl:value-of select="."/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output XML:
<?xml version="1.0" encoding="utf-8"?>
This is the chapter:<locale id="en-us">
<month>January</month>
<month>February</month>
<month>March</month>
</locale>
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic