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

XSLT modifying an xml dom tree?

Hi,

I am an XSLT newbie. I need to solve something which I will express in
pseudo-code.

function f1()
{
xmltree = <emptydom>
addnamevaluepair(xmltree, "size","100")
addnamevaluepair(xmltree, "color","blue")
}

function addnamevaluepair(dom, name, value)
{
//add element : <attrib>
// <name>....</name>
// <value>...</value>
// </attrib>
//to the dom and return it
return xmldom
}

What would the equivalent be in XSLT?

Thanks in advance,
Prashanth

Aug 1 '05 #1
6 1492


Prashanth Ellina wrote:

I am an XSLT newbie. I need to solve something which I will express in
pseudo-code.

function f1()
{
xmltree = <emptydom>
addnamevaluepair(xmltree, "size","100")
addnamevaluepair(xmltree, "color","blue")
}

function addnamevaluepair(dom, name, value)
{
//add element : <attrib>
// <name>....</name>
// <value>...</value>
// </attrib>
//to the dom and return it
return xmldom
}

What would the equivalent be in XSLT?

<xsl:template match="/">
<root>
<xsl:call-template name="addnamevaluepair">
<xsl:with-param name="name" select="'size'" />
<xsl:with-param name="value" select="100" />
</xsl:call-template>
<xsl:call-template name="addnamevaluepair">
<xsl:with-param name="name" select="'color'" />
<xsl:with-param name="value" select="'blue'" />
</xsl:call-template>
</root>
</xsl:template>

<xsl:template name="addnamevaluepair">
<xsl:param name="name" />
<xsl:param name="value" />
<attrib>
<name><xsl:value-of select="$name" /></name>
<value><xsl:value-of select="$value" /></value>
</attrib>
</xsl:template>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '05 #2
Hi,

What should I do if I want to add another attrib element between the
two already created. Can I treat the generated xml as a dom tree and do
an AddAfterNode() or something?

Thanks,
Prashanth

Aug 1 '05 #3


Prashanth Ellina wrote:

What should I do if I want to add another attrib element between the
two already created. Can I treat the generated xml as a dom tree and do
an AddAfterNode() or something?


No, XSLT is template based, while an input tree (in the sense of the
XPath data model which is different from the DOM model) is transformed
to an ouput tree there are no methods or functions inserting or adding
nodes at a particular node. The whole approach is different, templates
are processed and their contents is instantiated as part of the result
tree being formed.
It does not help to think in DOM terms (and the W3C DOM does not have a
AddAfterNode method at all), if you want to learn XSLT then you would
better look at tutorials and introductions and look at example solutions.
If you want help here implementing an XSLT transformation then it is
usually a good idea to show us your input XML data and the desired (XML
or HTML or text) output.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '05 #4
Hi,

I find the XSLT programming model very alien and un-intuitive. The most
restrictive thing is that I cannot maintain state across
function/template calls.

This is what I am looking to do

Input document:

<booksinformation>
<book>
<title>book title 1</title>
<author>Kramer</author>
</book>
<book>
<title>book title 2</title>
<author>Terry</author>
</book>
<book>
<title>book title 3</title>
<author>Terry</author>
</book>
</booksinformation>

Output document:

<booksinformation>
<bookdata>
<book author='1'>
<title>book title 1</title>
</book>
<book author='2'>
<title>book title 2</title>
</book>
<book author='2'>
<title>book title 3</title>
</book>
</bookdata>
<authordata>
<author id="1" name="Kramer"/>
<author id="2" name="Terry"/>
</authordata>
</booksinformation>

The output document avoid duplication of data by maintaining a subtree
for authors into which the bookdata can point.

Thank you,
Prashanth

Aug 1 '05 #5
Tempore 15:35:06, die Monday 01 August 2005 AD, hinc in foro {comp.text.xml} scripsit Prashanth Ellina <pr*************@gmail.com>:
I find the XSLT programming model very alien and un-intuitive. You'll be used to it in no time;) It's just another way of thinking.
This is what I am looking to do
...


This a grouping problem, here's a solution with the Muenchian grouping technique, one of the most alien things for beginners:-)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="author" match="author" use="."/>
<xsl:variable name="allAuthors" select="//author[generate-id()=generate-id(key('author',.)[1])]"/>

<xsl:template match="booksinformation">
<xsl:copy>
<bookdata>
<xsl:apply-templates/>
</bookdata>
<authordata>
<xsl:for-each select="$allAuthors">
<author id="{position()}" name="{.}"/>
</xsl:for-each>
</authordata>
</xsl:copy>
</xsl:template>

<xsl:template match="book">
<xsl:variable name="author" select="author"/>
<xsl:copy>
<xsl:attribute name="author">
<xsl:for-each select="$allAuthors">
<xsl:if test=". = $author"><xsl:value-of select="position()"/></xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:copy-of select="title"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Deserta faciunt et innovationem appelant
Aug 1 '05 #6
Hi,

That looks great. I will "muench" that. Thanks a lot.

Prashanth

Joris Gillis wrote:
Tempore 15:35:06, die Monday 01 August 2005 AD, hinc in foro {comp.text.xml} scripsit Prashanth Ellina <pr*************@gmail.com>:
I find the XSLT programming model very alien and un-intuitive.

You'll be used to it in no time;) It's just another way of thinking.
This is what I am looking to do
...


This a grouping problem, here's a solution with the Muenchian grouping technique, one of the most alien things for beginners:-)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="author" match="author" use="."/>
<xsl:variable name="allAuthors" select="//author[generate-id()=generate-id(key('author',.)[1])]"/>

<xsl:template match="booksinformation">
<xsl:copy>
<bookdata>
<xsl:apply-templates/>
</bookdata>
<authordata>
<xsl:for-each select="$allAuthors">
<author id="{position()}" name="{.}"/>
</xsl:for-each>
</authordata>
</xsl:copy>
</xsl:template>

<xsl:template match="book">
<xsl:variable name="author" select="author"/>
<xsl:copy>
<xsl:attribute name="author">
<xsl:for-each select="$allAuthors">
<xsl:if test=". = $author"><xsl:value-of select="position()"/></xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:copy-of select="title"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Deserta faciunt et innovationem appelant


Aug 2 '05 #7

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...
5
by: Don Garrett | last post by:
I have an XML document at the root of a directory tree that contains relative URIs to resources in a directory tree. During XSLT processing, these URI's can be used without any problems to...
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"/>...
3
by: Jack Fox | last post by:
I've never had the need to work with XML, but I believe I now have an appropriate application. I have time-series data in objects organized as a tree that I want an ASP.NET program to write out to...
6
by: Mike P | last post by:
I am quite new to XML and XSLT, and I know you can apply XSLT to XML to display data in an XML file according to the XSLT file, but is it possible to apply an XSLT file to page/s of HTML, so that...
2
by: RJN | last post by:
Hi Can XSLT be applied for transforming .txt files or are they applicable only for xml to xml transformation. Currently all the clients send file to a server in one particular format. The...
2
by: sebastian.langer | last post by:
Hi! Certainly somebody had my problem before and could give me just some hints, how to solve it: I have an xml file, which contains scientific data in a tree form. The data should be changed...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.