473,396 Members | 1,893 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.

XSL copy template

Hi all.
I am trying to write a stylesheet that will structure the input HTML
file in the following way:
For each heading of level n it needs to enclose the heading and all
its content until the next heading of the same level within the level
tag.

Example

input file:

<body>
<h1>First heading</h1>
Some text for first heading.
<h2>Second heading</h2>
<p>Some text for second heading</p>
<h3>Small heading</h3>
<div>Small text</div>

<h1>Another big heading</h1>
<span>Some text for big heading</span>
<h2>Sub heading for second big heading</h2>
</body>

output file:
<body>
<level1>
<h1>First heading</h1>
Some text for first heading.
<level2>
<h2>Second heading</h2>
<p>Some text for second heading</p>
<level3>
<h3>Small heading</h3>
<div>Small text</div>
</level3>
</level2>
</level1>
<level1>
<h1>Another big heading</h1>
<span>Some text for big heading</span>
<level2>
<h2>Sub heading for second big heading</h2>
</level2>
</level1>
</body>

I am trying to build an xsl which will call apply-templates for inner
elements with another mode, but fail to do it cleanly - I get a lot of
text nodes output multiple times.

Is there a neat way to do such a thing?

Thank you very much for help and sorry if this is an inapropriate
question.

Anna
Jul 20 '05 #1
2 3442
In article <8e**************************@posting.google.com >,
Anna <an**@ubaccess.com> wrote:

[...]

% I am trying to build an xsl which will call apply-templates for inner
% elements with another mode, but fail to do it cleanly - I get a lot of
% text nodes output multiple times.

Most likely this is because you're using value-of to copy the text,
but the text is also being processed by the default template for text(),
which copies text to the result tree. Try adding

<xsl:template match='text()'/>

% Is there a neat way to do such a thing?

You could do something like

<xsl:template match='node()'>
<xsl:copy>
<xsl:apply-template select='node()|@*'/>
</xsl:copy>
</xsl:template>

<xsl:template match='h1'>
<level1>
<xsl:copy>
<xsl:apply-template select='node()|@*'/>
</xsl:copy>
</level1>
</xsl:tempalte>

<!-- etc. -->
You could use some of the text-processing functions in XPath along
with xsl:element to have one header template work for all headings.
--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #2
Hi Anna,

Trying to structure a flat XML into smething more structure based on certain
'start' elements can be tricky - and the XPaths get a bit compicated ;) but
try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="h1"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*[starts-with(local-name(),'h') and
number(substring(local-name(),2)) = number(substring(local-name(),2))]">
<xsl:variable name="level" select="number(substring(name(),2))"/>
<xsl:element name="level{$level}">
<xsl:copy-of select="."/>
<xsl:variable name="this-name" select="local-name()"/>
<xsl:variable name="this-id" select="generate-id()"/>
<xsl:variable name="next-name" select="concat('h',$level + 1)"/>
<xsl:apply-templates
select="following-sibling::node()[generate-id(preceding-sibling::*[starts-wi
th(local-name(),'h') and number(substring(local-name(),2)) =
number(substring(local-name(),2))][1]) = $this-id and
not(starts-with(local-name(),'h') and number(substring(local-name(),2)) =
number(substring(local-name(),2)))] |
following-sibling::*[local-name() =
$next-name][generate-id(preceding-sibling::*[local-name() = $this-name][1])
= $this-id]"/>
</xsl:element>
</xsl:template>

<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="no_level"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*" mode="no_level">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="no_level"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@* | text() | comment() | processing-instruction()"
mode="no_level">
<xsl:copy/>
</xsl:template>

</xsl:stylesheet>
HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
"Anna" <an**@ubaccess.com> wrote in message
news:8e**************************@posting.google.c om...
Hi all.
I am trying to write a stylesheet that will structure the input HTML
file in the following way:
For each heading of level n it needs to enclose the heading and all
its content until the next heading of the same level within the level
tag.

Example

input file:

<body>
<h1>First heading</h1>
Some text for first heading.
<h2>Second heading</h2>
<p>Some text for second heading</p>
<h3>Small heading</h3>
<div>Small text</div>

<h1>Another big heading</h1>
<span>Some text for big heading</span>
<h2>Sub heading for second big heading</h2>
</body>

output file:
<body>
<level1>
<h1>First heading</h1>
Some text for first heading.
<level2>
<h2>Second heading</h2>
<p>Some text for second heading</p>
<level3>
<h3>Small heading</h3>
<div>Small text</div>
</level3>
</level2>
</level1>
<level1>
<h1>Another big heading</h1>
<span>Some text for big heading</span>
<level2>
<h2>Sub heading for second big heading</h2>
</level2>
</level1>
</body>

I am trying to build an xsl which will call apply-templates for inner
elements with another mode, but fail to do it cleanly - I get a lot of
text nodes output multiple times.

Is there a neat way to do such a thing?

Thank you very much for help and sorry if this is an inapropriate
question.

Anna

Jul 20 '05 #3

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
5
by: Adam Barr | last post by:
I have a tag foo that I want to copy unchanged when it is a subtag of bar, so I have a template (x is the namespace for the document): <xsl:template match="x:bar/x:foo"> <xsl:copy>...
3
by: Matt Bitten | last post by:
Hi, all. I have the same old problem about templates and copy constructors. I know this has been addressed hundreds of times, but despite perusing many old postings, and The Standard as well, I'm...
3
by: Martin Vorbrodt | last post by:
In "C++ Templates, The Complete Guide" i read that template copy-con is never default copy constructor, and template assignment-op is never a copy assignment operator. Could someone please explain...
8
by: Knighterrant | last post by:
I want to copy elements from one namespace to anothor, how to create the xslt? for example, the source data is: <s:mail xmlns:s="urn:source-namespace"> <s:subject>xxxx</s:subject>...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
2
by: tschwartz | last post by:
I have an xml document in which elements are hierarchically related to eachother conceptually. Unfortunately, the hierarchical relationship is not modelled in the schema (i.e., the elements are...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.