473,403 Members | 2,323 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,403 software developers and data experts.

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:stylesheet 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 7922
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:stylesheet 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====================== ========

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:stylesheet 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="$taskpath"/>
</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:stylesheet 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:attribute name="match">
<xsl:text>todo</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="substring-before($str,'.')"/>
<xsl:text>]</xsl:text>
<xsl:call-template name="split">
<xsl:with-param name="str" select="substring-after($str,'.')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<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()="$something">
<xsl:call-template name="whatever"/>
</xsl:when>
<xsl:otherwise>
<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
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
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...
1
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...
6
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...
1
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...
3
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
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...
6
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.