473,385 Members | 1,944 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,385 software developers and data experts.

XSLT Child node of a specific node

Hi All,

I would like to use XSLT to replace all <unodes that are children of
a <bnode with a new <headingnode. Also, if the <bnode has no
other children than remove it as well.
For example:
This would be some <b><u>Text</u></b>. It could <balso maybe be like
<u>this</u>...</b>

to

This would be some <heading>Text</heading>. It could <balso maybe be
like <heading>this</heading>...</b>

I've been trying something like:

<xsl:template match="//b[u]">
...
</xsl:template>

with no success.

Could someone point me in the right direction?

Thanks!

Oct 20 '06 #1
3 5703
On 2006-10-20, gr************@gmail.com <gr************@gmail.comwrote:
Hi All,

I would like to use XSLT to replace all <unodes that are children of
a <bnode with a new <headingnode. Also, if the <bnode has no
other children than remove it as well.
For example:
This would be some <b><u>Text</u></b>. It could <balso maybe be like
<u>this</u>...</b>

to

This would be some <heading>Text</heading>. It could <balso maybe be
like <heading>this</heading>...</b>

I've been trying something like:

<xsl:template match="//b[u]">
...
</xsl:template>

with no success.

Could someone point me in the right direction?
To replace a <uthat is a child of a <bwith <heading>, you can use

<xsl:template match="u[parent::b]">
<heading>
<xsl:apply-templates select="node()"/>
</heading>
</xsl:template>

To remove a <bwhose only child is a <u>, you can add

<xsl:template match="//b[u and count(node()) = 1]">
<xsl:apply-templates select="node()"/>
</xsl:template>

You will have to adjust the select attributes of the apply-templates
if you want the attributes of the replaced elements to be attributes
of the output <headingelements.
These templates throw away the attributes of the replaced nodes.
Oct 20 '06 #2

A. Bolmarcich wrote:
On 2006-10-20, gr************@gmail.com <gr************@gmail.comwrote:
Hi All,

I would like to use XSLT to replace all <unodes that are children of
a <bnode with a new <headingnode. Also, if the <bnode has no
other children than remove it as well.
For example:
This would be some <b><u>Text</u></b>. It could <balso maybe be like
<u>this</u>...</b>

to

This would be some <heading>Text</heading>. It could <balso maybe be
like <heading>this</heading>...</b>

I've been trying something like:

<xsl:template match="//b[u]">
...
</xsl:template>

with no success.

Could someone point me in the right direction?

To replace a <uthat is a child of a <bwith <heading>, you can use

<xsl:template match="u[parent::b]">
<heading>
<xsl:apply-templates select="node()"/>
</heading>
</xsl:template>

To remove a <bwhose only child is a <u>, you can add

<xsl:template match="//b[u and count(node()) = 1]">
<xsl:apply-templates select="node()"/>
</xsl:template>

You will have to adjust the select attributes of the apply-templates
if you want the attributes of the replaced elements to be attributes
of the output <headingelements.
These templates throw away the attributes of the replaced nodes.
Thanks for the reply Bolmarcich,

This works except that it places the new heading node in the wrong
place...
This is closer to what it actually looks like:

<notes>
<timestamp>
...
<content>
Here would be some <b><u>content...</u></b>
</content>
</timestamp>
<timestamp>
...
</timestamp>
</notes>

using your method results in:

<notes>
<timestamp>
...
<content>
<heading>content...</heading>
Here would be some <b><u>content...</u></b>
</content>
</timestamp>
<timestamp>
...
</timestamp>
</notes>

I would like the heading tag to actually replace the b and u tags...

I wish I knew more about XSLT to know the proper approach to this. Any
help is appreciated.

Oct 20 '06 #3
On 2006-10-20, gr************@gmail.com <gr************@gmail.comwrote:
[snip]
This works except that it places the new heading node in the wrong
place...
This is closer to what it actually looks like:

<notes>
<timestamp>
...
<content>
Here would be some <b><u>content...</u></b>
</content>
</timestamp>
<timestamp>
...
</timestamp>
</notes>

using your method results in:

<notes>
<timestamp>
...
<content>
<heading>content...</heading>
Here would be some <b><u>content...</u></b>
</content>
</timestamp>
<timestamp>
...
</timestamp>
</notes>
That is not the result that I get using xalan or saxon as the xlst
processor. Here are the files that I use.

input XML:

<?xml version="1.0"?>
<notes>
<timestamp>
...
<content>
Here would be some <b><u>content...</u></b>
</content>
</timestamp>
<timestamp>
...
</timestamp>
</notes>
XSLT (with a copy template in addition to the templates I suggested):

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

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

<xsl:template match="u[parent::b]">
<heading>
<xsl:apply-templates select="node()"/>
</heading>
</xsl:template>

<xsl:template match="//b[u and count(node()) = 1]">
<xsl:apply-templates select="node()"/>
</xsl:template>

</xsl:stylesheet>
output:
<?xml version="1.0" encoding="UTF-8"?><notes>
<timestamp>
...
<content>
Here would be some <heading>content...</heading>
</content>
</timestamp>
<timestamp>
...
</timestamp>
Please provide a complete example that demonstrates the problem.

In one template I may have followed what you gave in your original
posting too closly by using 'match="//b[u and count(node()) = 1]'.
It would be better to use 'match="b[u and count(node()) = 1]'.
Oct 20 '06 #4

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

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
4
by: titanandrews | last post by:
Hi, I have ran into a situation that I think should be possible, but I am fairly new to XSLT so maybe not. Suppose I have the following document <ROOT> <FOO name="A"> <CHILD name="B"/>...
12
by: Keith Chadwick | last post by:
I have a fairly hefty XSLT file that for the sake of debugging and clarity I wish to split into some separate sub-templates contained within the same file. The master template calls an...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
5
by: Douglas Steen | last post by:
Here's what I have (in snippets) ** XML ** <parent> <child> <grandchild/> </child> </parent> ** XSLT ** <xsl:template match="/parent">
3
by: Alex | last post by:
I stumbled upon this while developing a custom XPathNavigator. It appears that copy action for attributes is broken in the .net framework XSLT processor. The intent was to just copy the entities...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
0
by: krina | last post by:
I am new in this field and have just started learning XML and xslt, Now I am having a XML file.......having structure as follows..... <?xml version="1.0" encoding="UTF-8"?> <page> ...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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
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...
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...

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.