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 May 10th, 2005, 08:30 AM
yan yan is offline
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl help

I have a java program which goes through a directory of xml files and extracts certain nodes using a xsl stylesheet.

Everything works except the xsl is pulling some extra text that I don't want and I don't know how to get rid of it.

<testimonial>
<author>Bob</author>
<location>Ottawa</location>
<pageHeading>Bob in Ottawa</pageHeading>
<quote>Work is fun</quote>About CFPSA5/9/2005CFCF\tsi
<link>test.asp</link>TestimonialsenglishCFCF\tsi5/9/2005_selftrue
</testimonial>

The above is what I'm getting from applying the xsl to a single xml file but what I want is:


<testimonial>
<author>Bob</author>
<location>Ottawa</location>
<pageHeading>Bob in Ottawa</pageHeading>
<quote>Work is fun</quote>
<link>test.asp</link>
</testimonial>


The extra text is from other nodes of the xml but I'm not extracting them using the xsl and I can't figure out how to get rid of it.

The xsl is below


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>

    <xsl:strip-space elements="/"/>

    <xsl:template match="/">
        <testimonial>
            <xsl:apply-templates />
        </testimonial>
    </xsl:template>

    <xsl:template match="/page/english/author">
        <author> <xsl:value-of select="."/> </author>
    </xsl:template>

    <xsl:template match="/page/english/location">
        <location> <xsl:value-of select="."/> </location>
    </xsl:template>
    <xsl:template match="/page/english/quote">
        <quote> <xsl:value-of select="."/> </quote>
    </xsl:template>
    <xsl:template match="/page/english/page_heading">
        <pageHeading> <xsl:value-of select="."/> </pageHeading>
    </xsl:template>
    <xsl:template match="/page/metadata/dcrName">
        <link> <xsl:value-of select="."/> </link>
    </xsl:template>



     <xsl:template match="text()">
     <xsl:value-of select="normalize-space()"/>
     </xsl:template>

</xsl:stylesheet>


the xml source files look like this:


<?xml version="1.0" encoding="UTF-8"?>
<page>
    <english>
        <choose>
            <heading/>
        </choose>
        <choose>
            <paragraph/>
        </choose>
        <author>abc</author>
        <location>def</location>
        <page_heading/>
        <quote>aaaaaaa</quote>
    </english>
    <french>
        <choose>
            <heading/>
        </choose>
        <choose>
            <paragraph/>
        </choose>
        <author/>
        <location/>
        <page_heading/>
        <quote/>
    </french>
    <metadata>
        <area>About CFPSA</area>
        <creation_date>5/9/2005</creation_date>
        <creator>CFCF\tsi</creator>
        <dcrName>daf</dcrName>
        <dcrType>Testimonials</dcrType>
        <description/>
        <expiration_date/>
        <keywords/>
        <language>english</language>
        <last_modified_by>CFCF\tsi</last_modified_by>
        <last_modified_date>5/9/2005</last_modified_date>
        <parent/>
        <target>_self</target>
        <touched>true</touched>
    </metadata>
</page>




I could use something like this:

http://www.w3schools.com/xsl/xsl_value_of.asp

but that would generate a
<?xml version="1.0" encoding="ISO-8859-1"?>
for each xml in the directory which I don't want.
 
Old May 10th, 2005, 09:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Remember that if there's an element that doesn't match any template rule in your stylesheet, the default rule is

<xsl:template match="*">
  <xsl:apply-templates/>
</xsl:template>

which generally causes the text of that element to be copied to the result (with no element tags). If this isn't what you want, add the rule:

<xsl:template match="*"/>

which causes unknown elements to be excluded from the result.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:param and xsl:apply-templates' "select" newbieboobers XSLT 1 March 25th, 2008 07:23 PM
Pass link values as xsl:parameter to php5 then xsl pauljr8 XSLT 1 July 2nd, 2007 10:32 PM
differnce between xsl:apply-templates and xsl:call chandu.mca007 XSLT 2 June 12th, 2007 04:12 AM
xsl advise needed - Replacing UID through XSL Billyl XSLT 6 February 28th, 2006 05:46 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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