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 August 22nd, 2012, 04:28 PM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default xsl:apply-templates replacing a descendent element

Hi all,

Apologies for the awkwardly worded title. I'm converting a portion of a document to a plain text list. I have the bulk of the transformation working; however, I'm getting hung up on a <emph> descendent element. I would like to replace occurrences of <emph> with &quot; -- any advice, suggestions, or guidance would be greatly appreciated. Thanks in advance!

Input xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ead>
    <archdesc>
        <dsc>
            <head>Container List</head>
            <c01 id="ref1251" level="file">
                <did>
                    <unittitle>#464: Dutch. <emph render="doublequote">I know Mary [Frances <emph
                                render="doublequote">Dutchess</emph> (Watrous) Roth] packed it some
                            where!</emph>,</unittitle>
                    <container id="cid4822615" type="Box" label="Mixed Materials">2</container>
                    <container parent="cid4822615" type="Folder">110</container>
                    <unitdate>undated</unitdate>
                    <physdesc id="ref1252" label="General Physical Description note"
                        >(Negative)</physdesc>
                </did>
            </c01>
            <c01 id="ref1331" level="file">
                <did>
                    <unittitle>#476: Mountain home near Cosby,</unittitle>
                    <container id="cid4822586" type="Box" label="Mixed Materials">2</container>
                    <container parent="cid4822586" type="Folder">139</container>
                    <unitdate>undated</unitdate>
                    <physdesc id="ref1332" label="General Physical Description note">(6 of
                        6)</physdesc>
                    <physdesc id="ref1333" label="General Physical Description note"
                        >(Print)</physdesc>
                </did>
            </c01>
        </dsc>
    </archdesc>
</ead>
Mostly-working XSLT:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    
    <xsl:output method="text"/>
    
    <xsl:template match="/">
        <xsl:apply-templates select="ead/archdesc/dsc"/>
    </xsl:template>
    
    <xsl:template match="dsc">
        <xsl:apply-templates select="c01/did"/>
    </xsl:template>
    
    <xsl:template match="did">
        <xsl:apply-templates select="unittitle"/>
        <xsl:text>	</xsl:text>
        <xsl:text>Box: </xsl:text><xsl:apply-templates select="container[1]"/>
        <xsl:text>	</xsl:text>
        <xsl:text>Folder: </xsl:text><xsl:apply-templates select="container[2]"/>
        <xsl:text>	</xsl:text>
        <xsl:apply-templates select="unitdate"/>
        <xsl:text>	</xsl:text>
        <xsl:apply-templates select="physdesc"/>
        <xsl:text>
</xsl:text>
    </xsl:template>
</xsl:stylesheet>
 
Old August 23rd, 2012, 04:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You haven't explained what you have tried, or what the problem appear to be.

Something like this though might be what you are after:

Code:
<xsl:template match="emph">"<xsl:apply-templates/>"</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 23rd, 2012, 10:08 AM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi Sam,

Thank you for the response. My apologies -- I didn't do a very good job of expressing my problem, or my attempts at a solution. The problem I was encountering was that the
Code:
<emph>
should render " around words in the
Code:
unittitle
element. Because I wasn't sure how to approach this correctly, the quotation marks were being left out of the resulting text document. [1]

I had initially tried to create a variable and use it to replace occurrences of <emph>, but that didn't work (that's probably an imperative solution?).

Here's an example of one of my attempts:
Code:
<xsl:template match="unittitle">
        <xsl:if test="contains(., 'emph')">
            <xsl:variable name="emphQuote" select="'&quot;'"/>
            <xsl:apply-templates />
        </xsl:if>
    </xsl:template>
Your example works perfectly. Thank you for the advice. If you have a moment, would you be willing to address another question? As you can hopefully see in the example at [1] or [2], the formatting of the output follows the whitespace/indenting of the original XML. I'm applying [CODE]<xsl:stripspace elements="*"/>, but it seems that the whitespace is considered significant by the processor (Saxon HE 9.4).

I've tried the following, but this strips too much; e.g. the quotation marks.
Code:
<xsl:template match="unittitle"><xsl:value-of select="normalize-space()"/></xsl:template>
.

Thanks!

[1]
Code:
#464: Dutch. I know Mary [Frances Dutchess (Watrous) Roth] packed it some
                            where!,	Box: 2	Folder: 110	undated	(Negative)
[2]
Code:
#464: Dutch. "I know Mary [Frances "Dutchess" (Watrous) Roth] packed it some
                            where!",	Box: 2	Folder: 110	undated	(Negative)
 
Old August 23rd, 2012, 10:31 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I don't see what the problem is. My proposed solution appears to produce exactly the output you are asking for.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
CanOfBees (August 23rd, 2012)
 
Old August 23rd, 2012, 10:40 AM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi Sam,

Yes! Your solution fit my initial problem perfectly -- thank you very much for the assistance!

I suppose I have an additional, whitespace problem in the text output, where the tabbing in the XML is preserved in the output.

Cheers!
 
Old August 24th, 2012, 01:44 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The following will remove all extraneous whitespace from all the text you output.
Code:
<xsl:template match="text()"><xsl:value-of select="normalize-space(.)"/></xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 27th, 2012, 11:09 AM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Sam,
thanks again for your generous assistance. I had tried using the normalize-space() function, but had applied it incorrectly.

Thanks again!





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:apply-templates usage bsridharg XSLT 6 April 16th, 2010 03:20 PM
xsl:param and xsl:apply-templates' "select" newbieboobers XSLT 1 March 25th, 2008 07:23 PM
xsl:apply-templates select problem harapraveen XSLT 2 October 22nd, 2007 08:05 AM
differnce between xsl:apply-templates and xsl:call chandu.mca007 XSLT 2 June 12th, 2007 04:12 AM
xsl:apply-templates on Variable containing xml nexus5 XSLT 9 November 4th, 2004 02:55 PM





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