Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<xsl:value-of select="."> selects all children?

Status
Not open for further replies.

d86

Programmer
Aug 4, 2008
2
Hello,

this the xml code
Code:
<photo>
<name>picture #1
<extratext>!!!!111</extratext>
</name>
</photo>

i'm having trouble understanding how select works.

in xPath tutorial I learned that . selects current node

this is the xsl code

Code:
<xsl:template match="/">
<html><body>
<xsl:apply-templates />
</body></html>
</xsl:template>

<xsl:template match="photo">
Name: <xsl:value-of select="." />
</xsl:template>

I thought that no output would be available since there are no text children in photo. but when i run the code the text of all children in photo is shown?

Problem #2: what is the structure of <name>?
I know that <name>picture #1</name> is really <name><unnamedtextnode>picture #1</unnamedtextnode></name>

but when i append <extratext>abc</extratext> how does that affect the text node inside?

Problem #3: assuming <extratext> stays where it is. how does one select picture #1?

if you could help
thank you


 
I think you are seeing the effects of the built-in template rules. I think you will find that the built-in template rule for text nodes copies the text node value through to the output.

Tom Morrison
 
[1] problem #1
>
<xsl:template match="photo">
Name: <xsl:value-of select="." />
</xsl:template>

I thought that no output would be available since there are no text children in photo. but when i run the code the text of all children in photo is shown?

A: This is the implementation according to the recommendation, namely, section 7.6.1 :
[tt]
quote

7.6.1 Generating Text with xsl:value-of

<!-- Category: instruction -->
<xsl:value-of
select = string-expression
disable-output-escaping = "yes" | "no" />

The xsl:value-of element is instantiated to create a text node in the result tree. The required select attribute is an expression; this expression is evaluated and the resulting object is converted to a string as if by a call to the string function. The string specifies the string-value of the created text node. If the string is empty, no text node will be created. The created text node will be merged with any adjacent text nodes.


unquote
[/tt]
The net result is a concatination of all the text nodes separated by whitespace under picture node. As to the concrete treatment of insignificant whitespace(s), it is up to the concrete implementations. Hence, there comes the slight difference amongst them. But as the output is looked at via a browser, then again the browser is designed to collapse those insignificant whitespaces, the display of it would, by mulitple twist, the same.

[2] Problems #2 and #3
To answer your problems, the best is to let you see the proper results: see it is to know it. I know it is not that easy at the start.

[2.1] Suppose you add another complexity to the xml like this.
[tt]
<photo>
<name>picture #1
<extratext>!!!!111</extratext>
[blue]another picture #2[/blue]
</name>
</photo>
[/tt]
[2.2] Check out the output of this template on picture.
[tt]
<xsl:template match="photo">
Name [1]: <xsl:value-of select="." /><br />
Name [2]: <xsl:value-of select="name/text()" /><br />
Name [3]: <xsl:value-of select="name/text()[1]" /><br />
Name [4]: <xsl:value-of select="name/text()[2]" /><br />
Name [5]: <xsl:value-of select="name/extratext/following-sibling::text()" /><br />
</xsl:template>
[/tt]
[3] The content model of the picture node is "mixed" and it makes "data" retrieval a bit more complicated. If the purpose of the xml is data-storage oriented, that complication is not compensated by other advantages, if any. Hence, data-oreinted xml doc should avoid mixed content model.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top