473,834 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:styleshe et 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:valu e-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 2037
ma******@ihug.c o.nz (Matthew Rees-George) wrote in
news:77******** *************** ***@posting.goo gle.com:
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:styleshe et 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:valu e-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:styleshe et 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:styleshe et 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">'fo und 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.goo gle.com...
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:styleshe et 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:valu e-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:styleshe et 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
"Christophe r Boomer" <c-***********@tis cali.co.uk (don't dash!)>
wrote in news:3f******** @mk-nntp-2.news.uk.tisca li.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
"Christophe r Boomer" <c-***********@tis cali.co.uk (don't dash!)> wrote in
message news:3f******** @mk-nntp-2.news.uk.tisca li.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
2596
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 to assign a unique id for this object based upon the its occurence in the xml document. (note: the unique id should not be assigned depeneding upon its position with respect to the parent element but with respect to its number of occurence in the...
5
2183
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 it from a traditional programming point of view. I have did quite a bit of searching but have found no answers to my particular problem... please read below XML for the problem I am having.
4
8581
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 found an excellent site which actually has a great cross-platform script for doing just this;
5
1777
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 for adding the 'structured' elements which populate the higher regions of the tree (i.e. near the root). What I have to do next is write some code for working with the 'less structured' elements towards the 'leaf ends' of the tree. These are
16
5158
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. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
1
2032
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
1821
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, of which the contents are submitted to a search page. I know I could just send the form element data to a search page, but in this app, the user is creating a product code and need to see the complete code before it is
4
7739
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 the faster var color_speed = 50; //speed at which colors change var smoothing = 8; //the larger the number the less smooth but faster for sliding. var font_smoothing = 1; //lower the number the smoother the font changes.
1
4232
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 aligned to the top, along with the slideshow and link buttons, you have to scroll down to see the text - how can I make IE6 display correctly? http://geekarama.co.uk/new_home.html here is the code for new_home.html and following that the CSS...
0
9796
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9643
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10790
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4425
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.