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 December 16th, 2009, 05:02 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Text generation using XSLT

Hi,

I have an XML:

<chapter id="ch01" label="1" prefix="Chapter">
<title>Understanding Assessment</title>
<para>Assessment is a major focal point in education today.</para>
<figure label="1.2" prefix="Figure" float="1" id="ch01fig2" type="fig2">
<title>General Requirements for Assessments</title></figure></chapter>

And I want to generate the text Chapter 1 and Figure 1.2 using prefix and label attribute, like below:

<chapter aid_pstyle="CHAP_NUM" id="ch01" label="1" prefix="Chapter">Chapter 1<title>Understanding Assessment</title>
<para>Assessment is a major focal point in education today.</para>
<figure aid_pstyle="FIG_NUM" label="1.2" prefix="Figure" float="1" id="ch01fig2" type="fig2">Figure 1.2<title>General Requirements for Assessments</title></figure>
</chapter>


My XSLT is:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="chapter">
<xsl:copy>
<xsl:attribute name="aid_pstyle">CHAP_NUM</xsl:attribute>
<xsl:copy-of select="@*"/>
<xsl:value-of select="@prefix"/><xsl:text> </xsl:text><xsl:value-of select="@label"/><xsl:text></xsl:text>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="figure">
<xsl:copy>
<xsl:attribute name="aid_pstyle">FIG_NUM</xsl:attribute>
<xsl:copy-of select="@*"/>
<xsl:value-of select="@prefix"/><xsl:text> </xsl:text><xsl:value-of select="@label"/><xsl:text></xsl:text>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Using my XSLT, I can able to generate the required text. But I want to add the xml tag <inst>..</inst> around the text.

Is there a way in XSLT to generate the <inst> tag in comman way. That is, whatever the text generated by XSLT should also have <inst> .. </inst> around it.

Thanks,
Krish
 
Old December 16th, 2009, 05:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Not quite sure what you mean. Does the following not do what you want?

<inst><xsl:value-of select="@prefix"/><xsl:text> </xsl:text><xsl:value-of select="@label"/></inst>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 16th, 2009, 05:38 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, that is what I want. But I need this in generalized way.
Because, In XSLT, I can generate text using any level of tag (for ex: chapter, figure, table, sidebar). By using your suggestion, I need to include <inst> in all place where I generate the text.

The reason I am asking is:

I am generating XML file from another XML file with all these text inserted into it. Then this XML file is poured into Pagination software (Indesign). At some point I want to extract XML back from Indesign. At that time I need to remove all text that are generated by XSLT.
If I include <inst> around XSLT generated text, I can easily identify the text and remove it.

Thanks,
Gopal
 
Old December 16th, 2009, 06:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You could only do this is the stuff you are generating was generic - i.e if you could wrap the text you are generating in a template and call that perhaps.

e.g. In the following example you can call the 'outputText' template from multiple places:

Code:
<xsl:template match="chapter">
  <xsl:copy>
    <xsl:attribute name="aid_pstyle">CHAP_NUM</xsl:attribute>
    <xsl:copy-of select="@*"/>
    <xsl:call-template name="outputText"/>
    <xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
		</xsl:copy>	
	</xsl:template>

<xsl:template name="outputText">
<inst><xsl:value-of select="@prefix"/><xsl:text> </xsl:text><xsl:value-of select="@label"/><xsl:text></xsl:text></inst>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 16th, 2009, 06:25 AM
Registered User
 
Join Date: Dec 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks samjudson.

I will try this method.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT for XML to Text navik_pathak XSLT 2 July 9th, 2009 07:17 PM
How to update text after generation Rana AbuMari Reporting Services 1 March 17th, 2008 11:13 PM
xslt adding text blitzer XSLT 3 May 19th, 2007 10:10 PM
can we identify the text in the XSLT? rajatake XSLT 7 March 22nd, 2007 08:28 AM
xslt - need help with the text dev.user06 XSLT 6 July 22nd, 2006 08:22 PM





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