Preserving text position but transforming sub elements.

Matthew Rees-George

Usenet Archive
Joined
Jul 30, 2003
Messages
2
Reaction score
0
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:eek:utput 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:eek:therwise><xsl:copy/></xsl:eek:therwise> </xsl:choose> </xsl:for-each> </xsl:template>

</xsl:stylesheet>

There must be a more obvious solution? Anyone?
 

Arnold M. Slotnik

Usenet Archive
Joined
Jan 29, 2001
Messages
54
Reaction score
0
(Matthew Rees-George) wrote in
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:eek:utput 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:eek:therwise><xsl:copy/></xsl:eek:therwise>
</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:eek:utput 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>
 

Christopher Boomer

Usenet Archive
Joined
Jul 25, 2003
Messages
8
Reaction score
0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:eek:utput 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.
 

Arnold M. Slotnik

Usenet Archive
Joined
Jan 29, 2001
Messages
54
Reaction score
0
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.
 

Matthew Rees-George

Usenet Archive
Joined
Jul 30, 2003
Messages
2
Reaction score
0
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.
 

Steven Dilley

Usenet Archive
Joined
Feb 24, 2003
Messages
46
Reaction score
0
Christopher Boomer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top