Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 11th, 2007, 08:37 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default check name of the current node element in xml

Hi All,

  How can I check whether the name of the current node element is <div> or <style> or <span> ?

Thanking you very much,

Best Regards,
Aruna.G

  #2 (permalink)  
Old October 11th, 2007, 08:41 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:if test="self::div or self::style or self::span">

However, when I see code like this I always wonder whether the stylesheet shouldn't be making more effective use of template rules.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #3 (permalink)  
Old October 11th, 2007, 08:44 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

local-name(.) will return the local part of the current context node.

It sounds however like you are trying to write your program in a procedural way (i.e. for each element: if element = a do x, else if element is b do Y).

What you should be doing is applying templates with rules:

<xsl:apply-templates select="*"/>

and then

<xsl:template match="div">
 do X
</xsl:template>
<xsl:template match="style">
 do Y
</xsl:template>
<xsl:template match="span">
 do Z
</xsl:template>

If they have similar parts that should appear each element then have a separate template and use <xsl:call-template name="similar-parts"/>.

/- Sam Judson : Wrox Technical Editor -/
  #4 (permalink)  
Old October 11th, 2007, 08:45 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Did you mean the current node or the context node? Either way you need to use the name() function, or possibly local-name(). If you don't supply an argument you get the name of the context node, if you supply a node as an argument you get that node's name.


--

Joe (Microsoft MVP - XML)
  #5 (permalink)  
Old October 11th, 2007, 08:53 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I always forget Michael's very elegant solution :)

Of course if you are just trying to match a template to one of these elements you can do:

<xsl:template match="div | style | span">
 do stuff...
</xsl:template>

/- Sam Judson : Wrox Technical Editor -/
  #6 (permalink)  
Old October 11th, 2007, 09:00 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi All,

  Thank u for ur help. I tried with ur solutions but its not working.

Here is my xml content between <![CDATA[ and ]]>

<![CDATA[
<style type="text/css">
 span.Kommentarzeichen { font-family:'Times New Roman'; font-size:8pt; }
 span.Funotenzeichen { font-family:'Times New Roman'; vertical-align: 'super'; }
 div.berschrift1 { margin-top:12pt; margin-bottom:15pt; font-family:'Arial'; font-size:16pt; font-weight:bold; }
 div.Textkrper { margin-top:3pt; margin-bottom:1pt; font-family:'Times New Roman'; font-size:11pt; }
 div.Standard { font-family:'Times New Roman'; font-size:10pt; }
ins {color:blue}
del {color:red}
</style>
<div class="hn_content" xmlns:v="urn:schemas-microsoft-com:vml"></script><div class="Textkrper"><span class="hn_para"> This document provides an introduction of some of the more important features of Hyper.Net, and also to serve as an example document for publishing on the Hyper.Net Live Demo Site.</span></div>
  <div class="Textkrper"><span class="hn_para">Many word formatting conventions have been used in the formatting of this document so that it is easy to demonstrate the features Hyper.Net specifically provides to process legacy <a class="hn_commentsymbol" name="CommentSymbol_Comment7" href="#CommentContent_Comment7" onmouseover="return PopupShow('Comment7',event)" onmouseout="return PopupHide(event)" onclick="return false;"><span class="hn_commentsymbol" style="text-decoration:none;color:#000033;font-size:x-small">[CS1]</span></a>documentation, which is almost always formatted for paper appearance, and often contains &nbsp;structure, inconsistent formatting, and inconsistent use of styles.<span class="Funotenzeichen"><a class="hn_footnotesymbol" name="FootnoteSymbol_Footnote1" href="#FootnoteContent_Footnote1" title="Footnote"><span class="hn_footnotesymbol" style="text-decoration:none;color:#003300;font-size:x-small"><sup>1</sup></span></a></span></span></div><br clear="all">
  <table class="hn_footnote" cellpadding="0" cellspacing="0" border="0">
    <tr class="hn_footnote">
      <td class="hn_footnote" width="1%" valign="baseline" style="padding-right:6pt"><a class="hn_footnotesymbol" name="FootnoteContent_Footnote1" href="#FootnoteSymbol_Footnote1" title="Back"><span class="hn_footnotesymbol" style="text-decoration:none;color:#003300;font-size:x-small"><sup>1</sup></span></a></td>
      <td class="hn_footnote" width="99%">
<div class="Standard"><span class="hn_para"><span class="Funotenzeichen"></span> This is the text in the footnote under introduction.</span></div>
</td>
    </tr>
  </table><script language="JavaScript" src="" type="text/javascript"></script><div class="hn_comment" style="display:none"><br clear="all">
  </div>
  <div class="hn_commentsymbol" style="display:none"><a class="hn_commentsymbol" name="CommentContent_Comment7" href="#CommentSymbol_Comment7"><span class="hn_commentsymbol" style="text-decoration:none;color:#000033;font-size:x-small">[CSComment7]</span></a></div>
  <div class="hn_commentcontent" id="Comment7" onmouseover="PopupShow('Comment7',event)" onmouseout="PopupHide(event)" style="display:none; border:1px solid #666666; background-color:#ffffc0; padding:2px; font-size:10pt">
<div class="Standard"><span class="hn_para"><br><span class="Kommentarzeichen"></span><span class="Kommentarzeichen"></span>Old content authored in old authoring tools.</span></div>
</div>
</div>]]></TopicContent>

For this XML I have to retrieve only the text in between the <div>, <style> and all the <html> tags since I am transforming this xml to wml I need to throw those tags. But my xslt resulting the output with the <style> , <div> as it is.
part of My XSLT

<xsl:template match="TopicContent">
     <xsl:text>Hi</xsl:text>

      <xsl:copy>
        <xsl:apply-templates select="node()"/>
     </xsl:copy>
 </xsl:template>
<!-- <xsl:template match="node()">
     <xsl:copy>
            <xsl:apply-templates select="node()"/>
            <xsl:apply-templates select="@*"/>
     </xsl:copy>
 </xsl:template>-->
 <xsl:template match="node()">
    <xsl:if test="self::div or self::style or self::span">
          <xsl:value-of select="current()"/>
    </xsl:if>
 </xsl:template>

For that xml I need to check each node whether its div tag or span tag or style tag .. then i have to throw awy that tag and display on the text(if it contains) in those tags.

Could you please give me any idea.

Thanking you very much,

Best Regards
Aruna

  #7 (permalink)  
Old October 11th, 2007, 09:01 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by samjudson
 I always forget Michael's very elegant solution :)

Of course if you are just trying to match a template to one of these elements you can do:

<xsl:template match="div | style | span">
 do stuff...
</xsl:template>

/- Sam Judson : Wrox Technical Editor -/

Hi Mike,

  to try with your solution. I need write so many templates for all html tags. Is this the only way.

Aurna

  #8 (permalink)  
Old October 11th, 2007, 03:11 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I need write so many templates for all html tags.

If you need to process each element differently then you're going to have to write code for each one. If you can apply the same logic to different elements then you can common it up (match="style|script|xxx").

Either way, using template rules is the XSLT way to do it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
  #9 (permalink)  
Old October 12th, 2007, 07:49 AM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by mhkay
 >I need write so many templates for all html tags.

If you need to process each element differently then you're going to have to write code for each one. If you can apply the same logic to different elements then you can common it up (match="style|script|xxx").

Either way, using template rules is the XSLT way to do it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
I tried with

<xsl:template match="span|style|div">
  <xsl:value-of select="text()"/>
</xsl:template>

but its not displaying any output.

Is it because of the tags between <![CDATA[ and ]]>(you can see the xml esp bold coded). If the tags are between <![CDATA[ and ]]>, is it possible to apply the rules to them?

Thanking you very much,






  #10 (permalink)  
Old October 12th, 2007, 08:06 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Correct - everything inside the CDATA is just text, not part of the XML at all.

/- Sam Judson : Wrox Technical Editor -/




Similar Threads
Thread Thread Starter Forum Replies Last Post
search value of XML element/node in general deean XML 1 June 14th, 2008 03:17 AM
Please help to check the current node... darshil XSLT 1 May 9th, 2007 02:42 AM
Position of a node outside current context QuickSilver002 XSLT 2 April 19th, 2007 02:07 PM
XSLT Going up a level from current node. lafilip XSLT 4 February 23rd, 2007 03:06 PM
"for-the-current-node" instead of "for-each" ? webhead XSLT 2 August 25th, 2006 02:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.