XML Namespace Reminds OOPs Namespace

XML namespaces are used for providing uniquely named elements and attributes in an XML document. An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary is given a namespace, the ambiguity between identically named elements or attributes can be resolved.

A namespace name is a uniform resource identifier (URI). Typically, the URI chosen for the namespace of a given XML vocabulary describes a resource under the control of the author or organization defining the vocabulary, such as a URL for the author’s Web server. However, the namespace specification does not require nor suggest that the namespace URI be used to retrieve information; it is simply treated by an XML parser as a string. For example, the document at http://www.w3.org/1999/xhtml itself does not contain any code. It simply describes the XHTML namespace to human readers. Using a URI (such as “http://www.w3.org/1999/xhtml”) to identify a namespace, rather than a simple string (such as “xhtml”), reduces the probability of different namespaces using duplicate identifiers.

Ref: http://en.wikipedia.org/wiki/XML_namespace

Without futher ado let’s understand with simple example

----------------------
XML (test.xml)
----------------------
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<root xmlns:h1="http://www.w3.org/h1" xmlns:h2="http://www.w3.org/h2" >
  <h1:one>
    <two h1:attr="h1_attr1" h2:attr="h2_attr1">H1 Test</two>
  </h1:one>
  <h2:one>
    <two  h1:attr="h1_attr2" h2:attr="h2_attr2">H2 Test</two>
  </h2:one>
</root>
----------------------
XSLT (test.xslt) 
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:h1="http://www.w3.org/h1" xmlns:h2="http://www.w3.org/h2">
  <xsl:output  method="xml" indent="yes" />
  <xsl:template match="/">
    <root>
      <xsl:apply-templates />
    </root>
  </xsl:template>

  <!-- Template / Function (Easy Understanding -->
  <xsl:template match="h1:one">
    <h1_one>
      <xsl:apply-templates select="two" />
    </h1_one>
  </xsl:template>
  <xsl:template match="h2:one">
    <h1_one>
      <xsl:apply-templates select="two" />
    </h1_one>
  </xsl:template>  
  <xsl:template match="two">
    <xsl:attribute name="h1:attr">
      <xsl:value-of select="@h1:attr" />
    </xsl:attribute>
    <xsl:attribute name="h2:attr">
      <xsl:value-of select="@h2:attr" />
    </xsl:attribute>
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
----------------------
OUTPUT
----------------------
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:h1="http://www.w3.org/h1" xmlns:h2="http://www.w3.org/h2">
  <test attr="h1_attr1">H1 Test</test>
  <test attr="h1_attr2">H2 Test</test>
</root>

Note:  How apply-templates processes XML Document Very Interesting !!!

Advertisements

Foreach calls GetEnumerator, Current and MoveNext !!!

The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators.
Therefore, using foreach is recommended instead of directly manipulating the enumerator.

Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.

Best way to call GetEnumerator using foreach statement

Test with Quick Example

class Program
{        
   static void Main(string[] args)
   {        
      Testing tst = new Testing();
      foreach (var item in tst)
      {
          Console.WriteLine(item);       
      }
   }
}

public class Testing : IEnumerable
{
   public IEnumerator GetEnumerator()
   {
      return (new int[] { 1, 2, 3, 4, 5}).GetEnumerator();
   }
}

Output:
-----------------
1
2
3
4
5
Press any key to continue...

You probably do not want to call GetEnumerator explicitly. Instead it is implicitly called when you use a foreach loop in C#, or a For Each loop in VB.NET.

And all operations GetEnumerator, current, Movenext taken care by foreach, put a debug point and can be seen how foreach take care off all necessary calls

Simple & Quick Example

class Program
{
   static void Main(string[] args)
   {
      Testing tst = new Testing();
      foreach (var item in tst)
      {
          Console.WriteLine(item);
      }
   }
}

public class Testing : IEnumerable
{
   public IEnumerator GetEnumerator()
   {
      return new Testing2();
   }
}

public class Testing2 : IEnumerator
{
   string[] str= { "1", "2", "3", "4", "5","6" };
   int pointer=-1;
   public object Current
   {
      get { return str[pointer]; }
   }

   public bool MoveNext()
   {
      if (pointer < str.Length-1)
      {
          pointer++;
          return true;
      }
      return false;
   }
   public void Reset()
   {
      pointer = 0;
   }
}

XSLT 1.0 How to make text data Title Case Hmmm….. Interesting…..

Here is the solution it is messy but works for XSLT 1.0

I am getting late so straight away going for quick example

————————-
test.xml
————————-

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xslt" ?>
<data>
<input>
bhumip safi
</input>
</data>

————————-
test.xslt
————————-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<!--- Consider Below As Function -->
<xsl:template name="ConvertTitleCase">
<xsl:param name="temp" />
<xsl:call-template name="TitleCase">
<xsl:with-param name="text" select="translate(normalize-space($temp), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:call-template>
</xsl:template>
<xsl:template name="TitleCase">
<xsl:param name="text" />
<xsl:param name="lastletter" select="' '"/>
<xsl:if test="$text">
<xsl:variable name="thisletter" select="substring($text,1,1)"/>
<xsl:choose>
<xsl:when test="$lastletter=' ' or $lastletter='/' or $lastletter='-'">
<xsl:value-of select="translate($thisletter,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$thisletter"/>
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="TitleCase">
<xsl:with-param name="text" select="substring($text,2)"/>
<xsl:with-param name="lastletter" select="$thisletter"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<!--- Consider Below As Caller Of Function -->
<xsl:template match="/">
<xsl:apply-templates select="data" />
</xsl:template>
<xsl:template match="data">
<output>
<xsl:call-template name="ConvertTitleCase">
<xsl:with-param name="temp" select="input" />
</xsl:call-template>
</output>
</xsl:template>

</xsl:stylesheet>

How every need to code much compare to other languages but
Wallah — problem solved