Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   newbie: untangle nested attributes (http://www.velocityreviews.com/forums/t169877-newbie-untangle-nested-attributes.html)

iliad 09-17-2005 08:26 PM

newbie: untangle nested attributes
 
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
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:dxl='http://www.lotus.com/dxl'>

<xsl:output 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>


Joris Gillis 09-18-2005 07:44 AM

Re: newbie: untangle nested attributes
 
Hi,
Tempore 22:26:58, die Saturday 17 September 2005 AD, hinc in foro {comp.text.xml} scripsit iliad <heipmann@gmail.com>:

> 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

iliad 09-19-2005 02:11 PM

Re: newbie: untangle nested attributes
 
joris, cool. thanks. which source could i use to figuer out more about
the difference of

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

cheers, iliad


Joris Gillis 09-19-2005 02:43 PM

Re: newbie: untangle nested attributes
 
Tempore 16:11:47, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit iliad <heipmann@gmail.com>:

> 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

Joris Gillis 09-19-2005 02:47 PM

Re: newbie: untangle nested attributes
 
Tempore 16:11:47, die Monday 19 September 2005 AD, hinc in foro {comp.text.xml} scripsit iliad <heipmann@gmail.com>:

> 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

iliad 09-21-2005 12:13 PM

Re: newbie: untangle nested attributes
 
joris, thanks for that all. your explanations were a great help for me
in undestanding xslt better..
kind regards, iliad



All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.