Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > newbie: untangle nested attributes

Reply
Thread Tools

newbie: untangle nested attributes

 
 
iliad
Guest
Posts: n/a
 
      09-17-2005
hi,
i've the following questions:

1. why does..
<xsl:template match='dxl:text'>
<xsl:apply-templates/>
</xsl:template>
... results in "testing<BR>123"? why does it writes this? shouldn't i
use a 'value-of' to create some output?

2. i can transform this xml piece..
<richtext>
<pardef id='3'/>
<par def='3'>
jdi
<run>
<font style='bold'/>
ojd
</run>
g
</par>
</richtext>"
... only in something like this: jdi<B>odj</B>ojdg
How can I create this: jdi<B>odj</B>g

Thanks!!
iliad

----
XML:
<?xml version='1.0'?>
<document xmlns='http://www.lotus.com/dxl' version='6.5'
maintenanceversion='2.0' replicaid='C125707700457741' form='frm201'>
<item name='txt_NavigationTitle'>
<text>testing<break/>123</text>
</item>
<item name='rtf_Body'>
<richtext><pardef id='3'/><par def='3'>jdi<run><font
style='bold'/>ojd</run>g</par></richtext>
</item>
</document>

----
XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet
xmlnssl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:dxl='http://www.lotus.com/dxl'>

<xslutput method="html" indent="yes"/>

<xsl:template match="/">
<html>
<body>
<table>
<tr>
<th>Item</th>
<th>Value</th>
</tr>
<xsl:for-each select="
dxl:document/dxl:item[@name='txt_NavigationTitle']|
dxl:document/dxl:item[@name='rtf_Body']">
<tr>
<td>
<xsl:apply-templates/>
</td>
<td>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template match='dxl:text'>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match='dxl:richtext'>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match='dxl:break'>
<BR/>
</xsl:template>

<xsl:template match="dxl:run/dxl:font[@style='bold']">
<B><xsl:value-of select = ".."/></B>
</xsl:template>

</xsl:stylesheet>

----
RESULT:
<html xmlns:dxl="http://www.lotus.com/dxl">
<body>
<table>
<tr>
<th>Item</th><th>Value</th>
</tr>
<tr>
<td>testing<BR>123</td><td></td>
</tr>
<tr>
<td>jdi<B>odj</B>ojdg</td><td></td>
</tr>
</table>
</body>
</html>

 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      09-18-2005
Hi,
Tempore 22:26:58, die Saturday 17 September 2005 AD, hinc in foro {comp.text.xml} scripsit iliad <(E-Mail Removed)>:

> 1. why does..
> <xsl:template match='dxl:text'>
> <xsl:apply-templates/>
> </xsl:template>
> .. results in "testing<BR>123"?


Because you're code commands the XSLT processor to do so.
'<xsl:apply-templates/>' is equivalent to '<xsl:apply-templates select="node()"/>'

the 'dxl:text' element cotains two types of child nodes:
1. Text nodes (they are handled by a built-in default template that simply returns their value)
2. An element node: a 'break' elemwnt. You have defined a template for this element.
<xsl:template match='dxl:break'>
<BR/>
</xsl:template>

After the processing of the '<xsl:apply-templates/>', the result tree contains 3 child nodes as well:
1. a text node with value 'testing'
2. an elment 'BR'
3. a text node with value '123'

Because the output type was defined to be 'html', this looks after serialization like:
"testing<BR>123"

If you do not want the 'BR' element, either delete the template that matches 'dxl:break' or use 'value-of'.

> 2. i can transform this xml piece..
> <richtext>
> <pardef id='3'/>
> <par def='3'>
> jdi
> <run>
> <font style='bold'/>
> ojd
> </run>
> g
> </par>
> </richtext>"
> .. only in something like this: jdi<B>odj</B>ojdg
> How can I create this: jdi<B>odj</B>g


Change the last template to:

<xsl:template match="dxl:run[dxl:font[@style='bold']]">
<B><xsl:value-of select = "."/></B>
</xsl:template>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Vincit omnia simplicitas
Keep it simple
 
Reply With Quote
 
 
 
 
iliad
Guest
Posts: n/a
 
      09-19-2005
joris, cool. thanks. which source could i use to figuer out more about
the difference of

match="element[element]"
match="element/element"

cheers, iliad

 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      09-19-2005
Tempore 16:11:47, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit iliad <(E-Mail Removed)>:

> the difference of
>
> match="element[element]"
> match="element/element"


Your situation might be simplified:

the xml becomes:
<line>
<bold/>
my text
</line>

the only template rule:
<xsl:template match="line/bold">
<b><xsl:value-of select=".."/></b>
</xsl:template>

What happens during the xslt process?

1. The 'line' element is encountered. No matching template is found.
Defaults to:
<xsl:template match="*">
<xsl:apply-templates select="node()"/>
</xsl:template>
All child nodes are processed. There 2 of them: a 'bold' element (step 2) and a text node (step 3)
2. The 'bold' element is encountered. A matching template is found.
<b>my text</b> is added to the result tree.
3. The text node is encountered. Its value is added to the result tree.

end result: "<b>my text</b>my text"
____
What was my suggestion?
<xsl:template match="line[bold]">
<b><xsl:value-of select="."/></b>
</xsl:template>
(this matches any 'line' element that has a 'bold' child element)

Again, what happens during the xslt process?

1. The 'line' element is encountered. A matching template is found.
<b>my text</b> is added to the result tree.

That's it - no further processing.

end result: "<b>my text</b>"

Hope this helps,


regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Gaudiam omnibus traderat W3C, nec vana fides
 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      09-19-2005
Tempore 16:11:47, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit iliad <(E-Mail Removed)>:

> the difference of
>
> match="element[element]"
> match="element/element"


For some clarifying examples of Xpath expressions, see

http://www.w3.org/TR/xpath.html#path-abbrev

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Gaudiam omnibus traderat W3C, nec vana fides
 
Reply With Quote
 
iliad
Guest
Posts: n/a
 
      09-21-2005
joris, thanks for that all. your explanations were a great help for me
in undestanding xslt better..
kind regards, iliad

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
attributes ala java annotations or .Net attributes? Kyle Schmitt Ruby 3 07-24-2007 07:48 PM
class attributes & data attributes james_027 Python 2 06-20-2007 03:12 PM
WebControl.Attributes.Add and custom attributes P4trykx ASP .Net 2 01-31-2007 04:33 PM
Parse reserved attributes as normal attributes Max XML 1 09-22-2006 12:04 PM
algorithm to untangle a graph grasp06110@yahoo.com Java 4 04-18-2006 07:47 PM



Advertisments