Subject: RE: [xsl] filtering descendent text nodes From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx> Date: Mon, 18 Mar 2002 09:10:39 -0000 |
> i am new to xslt. Given below is the files i am using. > > <?xml version="1.0" ?> > <custdet> > <customer> > <name> > <firstname>FIRST1</firstname> > <secondname>NAME1 > <secondname1>SECONDNAME1</secondname1> > </secondname> > </name> > </customer> > </custdet> > > ------------------- > I AM TRYING TO FILTER OUT ALL DESCENDENT TEXT NODES > VALUES (I.E. FIRST1,NAME1,SECONDNAME1) AND AM USING > THE FOLLOWING XSL FOR THAT. First point: every node except the root node is a descendant of something. The root node is the parent of the <custdet> element (think of it as representing the document itself). Certainly every text node is a descendant of something. So filtering out all the descendant text nodes is the same as filtering out all the text nodes. > ------------------- > > <?xml version="1.0"?> > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0"> > <xsl:template match="/"> > <xsl:apply-templates /> > </xsl:template> > <xsl:template match="*"> > <xsl:apply-templates/> > </xsl:template> > <xsl:template match="text()"> > <xsl:apply-templates/> > </xsl:template> This last rule is rather peculiar. You are saying, when a text node is encountered, apply templates to its children. But text nodes don't have children. So your template rule is equivalent to <xsl:template match="text()"/> - which is fine if that's what you want to do, it means that when a text node is encountered, you output nothing. > <!-- --> > <xsl:template match="customer//node()"> > First Name : > <b><i> > <font color="red"> > <xsl:value-of select="." /> > <br/> > </font> > </i></b> > > </xsl:template> This rule has a higher precedence than the rules above, so it will be chosen in preference when processing any node (which may be an element node or text node) that is a descendant of the <customer> element. So your <xsl:template match="*"/> rule will be used to process the customer element itself. This rule calls <xsl:apply-templates/> to process the children of the customer element. Apart from whitespace-only text nodes, there is only one child of <customer>, the <name> element. So the customer//node() rules is invoked to process the <name> element, and also to process the whitespace-only text nodes that precede and follow the <name> element. For each of these three nodes, the template outputs "First name:" followed by the string-value of the node, together with some HTML markup. For the whitespace text nodes, the string-value of the node is whitespace. For the <name> element, it is the concatenation of all the text contained within the <name> element (including whitespace), without any tags. Your example output shows five lines. I think your XML document must have contained an extra element after the <name> element, which you haven't shown us. Also, I don't think you've shown us the (HTML) output of the transformation, you've shown us the way this HTML is rendered by the browser. The customer//node() template rule doesn't do an <xsl:apply-templates/>, so the children of the <name> element are never processed. Michael Kay Software AG home: Michael.H.Kay@xxxxxxxxxxxx work: Michael.Kay@xxxxxxxxxxxxxx > <!----> > </xsl:stylesheet> > > > THE expected result is three lines with "FIRST NAME:" > prefix and the corresponding text value attached. The > output i am getting is as follows > > > First Name : > First Name : FIRST1 NAME1 SECONDNAME1 > First Name : > First Name : 20one1 > First Name > > Could somebody explain what actually is happening and > also some light on the difference between child nodes > and descendent nodes. > > Thanks > Aseef.J > > __________________________________________________ > Do You Yahoo!? > Yahoo! Sports - live college hoops coverage > http://sports.yahoo.com/ > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] filtering descendent text nod, Aseef Jamaluddin | Thread | RE: [xsl] filtering descendent text, Aseef Jamaluddin |
RE: [xsl] XSL v. XSLT, Michael Kay | Date | [xsl] XSLT 2.0 question, Bryan Rasmussen |
Month |