Retrieving xml node name as string

Example of using <msxsl:script>.
The example takes the input.xml file as input.
 
<?xml version="1.0"?>
<customers>  <customer>
    <name>John Smith</name>
    <address>123 Elm St.</address>
    <phone>(123) 456-7890</phone>
  </customer>
  <customer>
    <name>Mary Jones</name>
    <address>456 Oak Ave.</address>
    <phone>(156) 789-0123</phone>
  </customer>
</customers>
 
xslt code with helps of JScript:
 
<?xml version=’1.0′?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">
  <msxsl:script language="JScript" implements-prefix="user">
    <![CDATA[
      function xml(nodelist) {
      nodelist.MoveNext();
      return nodelist.Current.Name;
    ]]>
    }
  </msxsl:script>
  <xsl:template match="customer">
    <xsl:value-of select="user:xml(.)"/>
  </xsl:template>
</xsl:stylesheet>
 
the same with helps of C#:
 
<?xml version=’1.0′?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:user="http://mycompany.com/mynamespace">
  <msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
      public string xml(XPathNodeIterator nodelist) {
        while(nodelist.MoveNext())     
          return nodelist.Current.Name;
        return ""; 
      }
    ]]>
   
  </msxsl:script>
  <xsl:template match="customer">
    <xsl:value-of select="user:xml(.)"/>
  </xsl:template>
</xsl:stylesheet>
 
See also msdn 🙂
 
 
 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s