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 9th, 2012, 04:49 AM
Authorized User
 
Join Date: Aug 2012
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
Default Can I use XSLT for this problem? ....

Hi,

I use XML and SGML on a daily basis for my work (Technical Author) but i'm only just at the very beginning stages of getting into XSLT.

I have a bunch of SGML documents (not XML, so this may be the first problem) which are not parsing against the DTD due to some elements being out of context.

In the text of our documents, when referencing to another section, figure or table, the element
Code:
<xref id"f001" idtype"figure"></xref>
can be used (id attribute obviously varies depending on what is being linked to).

The problem is that somehow, when these documents were originally authored, somebody has inserted text within the opening and closing Xref element tags - which we don't want. So what we end up with is something like this:

Code:
<para>Refer to <xref xrefid="f001" xidtype="figure"> Undo the 10 M3 countersunk screws that attach the required Electronic Assembly to its mounting brackets.
</xref></para>
What i need to be able to do, is find an automated way of extracting the text from within the Xref element and re-insert it after the closing Xref tag, so we end up with:

Code:
<para>Refer to <xref xrefid="f001" xidtype="figure"></xref>. Undo the 10 M3 countersunk screws that attach the required Electronic Assembly to its mounting brackets.</para>
Is this possible with XSLT or am i barking up the wrong proverbial tree?

Also, and along similar lines, in places somebody has managed to insert Xref elements within the opening and closing tags of other Xref elements ... which is against the rules of the DTD.

Again, is it possible to automate the correction of this do you think? I can provide a test file if it is possible to achieve and anybody wishes to see the source.

Many thanks
 
Old August 9th, 2012, 05:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, this is very easy to achieve with XSLT. It just needs one template rule

Code:
<xsl:template match="xref">
  <xref>
     <xsl:copy-of select="@*"/>
  </xref>
  <xsl:value-of select="."/>
</xsl:template>
and of course another rule that copies everything else unchanged:

Code:
<xsl:template match="*">
  <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Daedalus (August 9th, 2012)
 
Old August 9th, 2012, 05:23 AM
Authorized User
 
Join Date: Aug 2012
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks Mike, looks like a simple fix using XSLT.

So can i assume that the following stylesheet should do it?

Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" encoding="utf-8" indent="no"/>
						
<xsl:template match="xref">
  <xref>
     <xsl:copy-of select="@*"/>
  </xref>
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="*">
  <xsl:copy><xsl:copy-of select="@*"/>
<xsl:apply-templates/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
If this looks to be correct, can you please give me a pointer as to the best way to run the stylesheet to try it against my document. I have Visual Studio if that helps, and i understand it can be done in a browser as well. As I say, XSLT is all very new to me so thanks for your patients whilst dealing with my novice questions.

Thanks again in advance.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT problem with IE 7 asadallahi BOOK: Professional Ajax ISBN: 978-0-471-77778-6 3 April 9th, 2009 09:33 AM
XSLT problem her_today XSLT 1 October 23rd, 2006 11:09 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
XSLT problem pendyalap XSLT 4 March 24th, 2006 04:08 PM
problem with xslt superwebba BOOK: Professional Ajax ISBN: 978-0-471-77778-6 5 March 10th, 2006 01:31 AM





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