XSLT modifying an xml dom tree?

  • Thread starter Prashanth Ellina
  • Start date
P

Prashanth Ellina

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
 
M

Martin Honnen

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>
 
P

Prashanth Ellina

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
 
M

Martin Honnen

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.
 
P

Prashanth Ellina

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
 
J

Joris Gillis

Tempore 15:35:06 said:
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:eek:utput 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,
 
P

Prashanth Ellina

Hi,

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

Prashanth

Joris said:
Tempore 15:35:06 said:
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:eek:utput 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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top