472,779 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 7891
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.