473,769 Members | 4,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT: Edit, remove, add nodes specified by position

How can XSLT stylesheets be used to edit, remove and add nodes specified
by their position in the document tree?

The XML document stores development tasks in a hierarchical way, i.e.
tasks can have subtasks, subsubtasks, etc. It has the following form:

<todo>
<note>Task 1</note>
<note>
Task 2
<note>
Task 2.1
</note>
<note>
Task 2.2
</note>
</note>
<note>Task 3</note>
</todo>

The aim is to implement a web interface for task management in
development projects, using parameterised XSLT stylesheets, and some cgi
scripts invoking an XSLT processor to modify the task database.

Is this choice of technologies sensible, or are there better ways to
operate on the XML document?

The stylesheets would be passed the position of the node in the document
tree, i.e. "2.2" would refer to the second subtask of the second task in
the todo list. This node would be removed or edited, or become the
parent of a new subtask (`add').

Can you give me some tips or pointers on how to add, remove and edit
nodes?

I got as far as the following for removing nodes. Of course, this would
need to be negated in some form to remove the corresponding node.

=============== =========XSL=== =============== ============
<?xml version="1.0"?>
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="todo/note[2]/note[2]"/>
</xsl:template>
</xsl:stylesheet>
=============== =========XSL=== =============== ============

tia,

Claudio
PS. The XML format originates from the software package devtodo, which
is provides an interactive command-line based interface to the XML
database.

http://swapoff.org/DevTodo

--
http://www.jolowicz.com
Jul 20 '05 #1
2 7969
On Tue, 15 Feb 2005, Claudio Jolowicz wrote:
I got as far as the following for removing nodes. Of course, this would
need to be negated in some form to remove the corresponding node.

============== ==========XSL== =============== =============
<?xml version="1.0"?>
<xsl:styleshee t version="1.0"
xmlns:xsl="htt p://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="todo/note[2]/note[2]"/>
</xsl:template>
</xsl:stylesheet>
============== ==========XSL== =============== =============

I've figured out I have to use the identity transform, but failed to
find a way to parametrise the stylesheet. This one doesn't work because
the match attribute must be a pattern, not an expression (correct?):

=============== =========XSL2== =============== =============
<?xml version="1.0"?>
<xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:param name="taskpath"/>

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

<xsl:template match="$taskpat h"/>
</xsl:stylesheet>
=============== =========XSL2== =============== =============
And this one didn't work either, probably because xsl:attribute applies
only to result node, not to the xml:template in which it occurs here.

=============== =========XSL3== =============== =============
<?xml version="1.0"?>
<xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:param name="taskid"/>

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

<xsl:template >
<xsl:attribut e name="match">
<xsl:text>tod o</xsl:text>
<xsl:call-template name="split">
<xsl:with-param name="str" select="$taskid "/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>

<xsl:template name="split">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="contains( $str,'.')">
<xsl:text>/note[</xsl:text>
<xsl:value-of select="substri ng-before($str,'.' )"/>
<xsl:text>]</xsl:text>
<xsl:call-template name="split">
<xsl:with-param name="str" select="substri ng-after($str,'.') "/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:text>/note[</xsl:text>
<xsl:value-of select="$str"/>
<xsl:text>]</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
=============== =========XSL3== =============== =============

Any ideas? At the moment I'm resorting to generating the stylesheet
dynamically with perl... Is it really not possible to use parameters
here?

Claudio

--
http://www.jolowicz.com
Jul 20 '05 #2

<xsl:template match="node()|@ *">
<xsl:choose>
<xsl:when test="local-name()="$someth ing">
<xsl:call-template name="whatever"/>
</xsl:when>
<xsl:otherwis e>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Jul 20 '05 #3

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

Similar topics

3
7359
by: Son KwonNam | last post by:
The title is quite ambiguos. Any way let me explain. In java, the following is possible. ---- String str = ""; for (int i=0; i < 10; i++) { str = str + i; } ----
5
9553
by: inquirydog | last post by:
Hi- Does anyone know a way to compare whether two nodes contain the same information in xslt (the name, attributes, and all content recursivly should be the same. I am interested in the case where node ordering matters, and also the case where it doesn't, but perhaps that is an advanced topic). Ideally the method should be available to xpath expressions, so I think that creating new templates which compare nodes will not work (well,...
1
1562
by: Oleg Konovalov | last post by:
Hi, I am new to XSLT, modifying somebody else's code: I have the following data (leaves in parallel branches): mystruct/myarray1/myvar/var2 and mystruct/myarray2/myvar/var3 I need to implement the find the first occurence of : <xsl:if test="position() != last() and
6
7222
by: datamodel | last post by:
Hello I have an XML tree of which you can see a mini-version here: http://paste.uni.cc/11838 (the tree is actually over 30,000 levels deep) How do I count the depth of a given <NODES><NODE????? The problem is I would like to "color" nodes based on their depth in the tree - ie, use alternating colors.
1
4746
by: balderdash | last post by:
Hi I am very close to achieving the output I need but I cant seem to get it right. The problem is I am looping through a table and selecting values, if there are 2 values per (row) Issuer I need to put a slash between them in the output xml. The problem is shown by the output at the bottom of this post. I am
3
2792
by: abhishek.smu | last post by:
Given an XML like: <root> <node>8</node> <node>21</node> <node>-7</node> <node>13</node> <node>43</node> <node>2</node> </root>
2
22778
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most common usage of this is in creating hyperlinks.
6
4341
by: kluge.wolfram | last post by:
Hi, i get stucked on a transformation problem using XSLT. What i need is to copy an XML Tree to an output XML without any automatic changes. Since i used <xsl:copyor <xsl:copy-ofthere occur unwanted side effects. For example i just copied a xml were several namespace declarations are present more than one time. Then the transformation do remove the declaration at the child nodes. Another funny automatism is - if i remove a node
0
9591
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
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
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
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5312
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...
1
3969
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
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.