473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Preserving text position but transforming sub elements.

I want this example xml:-
<xxx>text before <yyy att="value"/> text in the middle <zzz>ignored
but transformed</zzz> text after</xxx>

To be transformed to this:-
text before value text in the middle 'found a zzz' text after

I.e. the text positions must be preserved, but the element(s) must be
transformed.

I could not find similar problems or solutions on the web or
newsgroups, but I have this solution, which I really, really do not
like:-

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

<xsl:output method="text" indent="yes" encoding="utf-8"/>

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

<xsl:template match="xxx">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="name(.)='yyy'"><xsl:value-of
select="@att"/></xsl:when>
<xsl:when test="name(.)='zzz'">'found a zzz'</xsl:when>
<xsl:otherwise><xsl:copy/></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

There must be a more obvious solution? Anyone?
Jul 20 '05 #1
6 2022
ma******@ihug.co.nz (Matthew Rees-George) wrote in
news:77**************************@posting.google.c om:
I could not find similar problems or solutions on the web or
newsgroups, but I have this solution, which I really, really do
not like:-

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

<xsl:output method="text" indent="yes" encoding="utf-8"/>

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

<xsl:template match="xxx">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="name(.)='yyy'"><xsl:value-of
select="@att"/></xsl:when>
<xsl:when test="name(.)='zzz'">'found a
zzz'</xsl:when>
<xsl:otherwise><xsl:copy/></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

There must be a more obvious solution? Anyone?


Repeat after me: "Simpler is better".

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

<xsl:output method="text" indent="no" encoding="utf-8"/>

<!--First, match the xxx element and process its content -->

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

<!--Now let's deal with yyy -->

<xsl:template match="yyy">
<xsl:value-of select="@att"/>
</xsl:template>

<!--Finally, take care of the zzz element -->

<xsl:template match="zzz">
<xsl:text> 'found a zzz' </xsl:text>
</xsl:template>

</xsl:stylesheet>

--
a. m. slotnik
Jul 20 '05 #2
Use:

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

<xsl:output method="text"/>

<xsl:template match="@*">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="zzz">'found a zzz'</xsl:template>

</xsl:stylesheet>

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"Matthew Rees-George" <ma******@ihug.co.nz> wrote in message
news:77**************************@posting.google.c om...
I want this example xml:-
<xxx>text before <yyy att="value"/> text in the middle <zzz>ignored
but transformed</zzz> text after</xxx>

To be transformed to this:-
text before value text in the middle 'found a zzz' text after

I.e. the text positions must be preserved, but the element(s) must be
transformed.

I could not find similar problems or solutions on the web or
newsgroups, but I have this solution, which I really, really do not
like:-

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

<xsl:output method="text" indent="yes" encoding="utf-8"/>

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

<xsl:template match="xxx">
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="name(.)='yyy'"><xsl:value-of
select="@att"/></xsl:when>
<xsl:when test="name(.)='zzz'">'found a zzz'</xsl:when>
<xsl:otherwise><xsl:copy/></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

There must be a more obvious solution? Anyone?

Jul 20 '05 #3
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="utf-8"/>

<!--First, match the xxx element and process its content -->
<xsl:template match="xxx">
<xsl:apply-templates/>
</xsl:template>

<!--Now let's deal with yyy -->
<xsl:template match="yyy">
<xsl:value-of select="@att"/>
</xsl:template>

<!--Finally, take care of the zzz element -->
<xsl:template match="zzz">
<xsl:text> 'found a zzz' </xsl:text>
</xsl:template>

</xsl:stylesheet>

When you say it like that, it almost makes sense. But do you really think
that people are going to enter their expletives in zzz-tags? ;-)

I think after two months that XSLT is starting to sink into my brain. I've
had trouble convincing myself that templates were really function
definitions and that apply-templates was really a "run-source" command. I
guess you don't expect the program flow to be controlled by your input
parameter.

Strange world, strange syntax. Thanks both for the revelation.

One question: is it important to deal with xxx first? I suspect not, and
I'm used to defining functions before I attempt to call them, so I would
probably graduate to doing that last.

Jul 20 '05 #4
"Christopher Boomer" <c-***********@tiscali.co.uk (don't dash!)>
wrote in news:3f********@mk-nntp-2.news.uk.tiscali.com:
One question: is it important to deal with xxx first? I suspect
not, and I'm used to defining functions before I attempt to call
them, so I would probably graduate to doing that last.


No, the order of the templates in the stylesheet in this case don't
matter.

--
a. m. slotnik
Jul 20 '05 #5
Thanks, I knew it was blatently obvious ~ I just couldn't see the wood
for the trees (common problem in XSLT?). I forgot that you could
apply-templates without a select ~ and I have always selected. Learnt
my lesson.
Jul 20 '05 #6
"Christopher Boomer" <c-***********@tiscali.co.uk (don't dash!)> wrote in
message news:3f********@mk-nntp-2.news.uk.tiscali.com...

I've had trouble convincing myself that templates were really function
definitions and that apply-templates was really a "run-source" command.
I guess you don't expect the program flow to be controlled by your input
parameter.


It is best to throw out all concepts of "program flow", "functions", etc.
This is more like CSS: a set of patterns and corresponding outcomes.
(Or like awk or prolog).
The "input parameter" is what matches the pattern and activates the rule.

It is like saying "Put all nails in the blue drawer and all screws in the
green drawer". There is no assumption that the mixture of nails/screws
will be in any order, or that the rules will be handled sequentially.

Of course, selectively applying particular templates to a particular
sub-tree can make it look like a program, and the 'if' and 'for-each'
tags are very program-like. However, you can do most simple xsl
without either selecting templates or using batch-processing nodes.

--
Steve

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eshrath Ali Khan | last post by:
Hi, I have a requirement where I am transforming a XML into a html using an XSL. I need to create a object for the each and every occurrence of an element named "tgroup" in the xml. Also I need...
5
by: Jody Greening | last post by:
Transforming with XSLT, Grouping elements until difference found. I am seeking some help with the following problem, I am fairly new at XSLT transformations, and my problem may lie in looking at...
4
by: JE | last post by:
Hi! I am snooping around all sorts of websites and faqs, in search of a way to pick up any selected (mouse-highlighted) text in a HTML page, and store it in a JavaScript variable. I have...
5
by: Richard Lewis | last post by:
Hello Pythoners, I'm currently writing some Python to manipulate a semi-structured XML document. I'm using DOM (minidom) and I've got working code for transforming the document to HTML files and...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
1
by: =?ISO-8859-1?Q?Une_B=E9vue?= | last post by:
my xml fragment : <select id='aId'> <option>option 1</option> <option>option 2</option> <option>---?---</option> <option>option n + 1</option> <option>option n + 2</option>
12
by: David | last post by:
Hi, I am trying to achieve the following: 1/ To have a standard form in an asp web page which has various check boxes and radio buttons. As a user selects a form item it updates a text box,...
4
iam_clint
by: iam_clint | last post by:
Heres some sample code I wrote up for you guys to use on making text effects <script> //TEXT EFFECTS CODE WRITTEN BY iam_clint! window.onload = doEffects; var speed = 15; //lower the number...
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.