Category Archives: XML/XSLT

Defining XML Schema using Russian Doll Approach

Hi,

As I talked about the ‘Salami Slice’ pattern in my previous post. Now ‘Russian Doll‘ is the another design pattern which gets frequently used while Defining XML Schema.

Russian Doll
This Pattern follows a nested structure by defining only one global element, such as “Product” based on the below example. All child element are defined inside the root element in local scope manner. The root element must define within the global namespace.

 Sample XSD in a Russian Doll pattern.
<xs:element name=”Product”>

<xs:complexType>
<xs:sequence>
<xs:element name=”ProductDtls”>
<xs:complexType mixed=”false”>
<xs:sequence>
<xs:element name=”name” type=”xs:string” minOccurs=”0″ />
<xs:element name=”category” type=”xs:string” minOccurs=”0″ />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name=”PriceRange”>
<xs:complexType mixed=”false”>
<xs:sequence>
<xs:element name=”minPrice” type=”xs:string” minOccurs=”0″/>
<xs:element name=”maxPrice” type=”xs:string” minOccurs=”0″ />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>

The Russian Doll pattern provides the least extensible schema structure. In the example listed above, all the components except the root element are localized and not reusable. This schema fits into a document-style WSDL but is not well suited for RPC style since there is no global level type defined.

In all this pattern provides very less reuse for the defined Schema elements. Because it bundle everything together into a tidy, single unit and with single namespace. Russian Doll is the simplest and easiest pattern to use by instance developers.

Hope this helps.


Thanks,
R Vashi

Writing XML Schema using Salami Slice Approach

Hi,

An XML schema describes the valid form and content of a conforming XML document. It is not required that every XML document has a corresponding schema, but it is important to have if you actually want to have others consume your documents.

In plain words The schema is used to validate a document, and to assist people/programs from creating their own versions of XML according to the schema.

In this post I will discuss about one of heavily used pattern “Salami Slice”.

Salami Slice
This pattern represent the different style of design spectrum. This pattern lets you disassemble the instance of document into its individual components. In plain words it lets us to define each element separately and then assemble them together later.


Lets take an example of Product entity.

<!–?xml version=”1.0″ encoding=”UTF-8″?>
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema&#8221; elementFormDefault=”qualified” attributeFormDefault=”unqualified”>

<!– Define the Elements gloabally –>
<xs:element name=”ProductTitle” type=”xsd:string” minOccurs=”0″/>

<xs:element name=”ProductCode” type=”xsd:int” minOccurs=”0″ />

<xs:element name=”MinPrice” type=”xsd:string” minOccurs=”0″ />

<xs:element name=”MaxPrice” type=”xsd:string” minOccurs=”0″ />

<xs:element name=”ProductPriceRange”>
<xs:complexType>
<xs:sequence>
<xs:element ref=”MinPrice” minOccurs=”0″ />
<xs:element ref=”MaxPrice” minOccurs=”0″/>
</xs:sequence>
</xs:complexType>
</xs:element>

<!– Now Construct the Actual Product Element, by Using the ‘ref’ of above defined elements –>
<xs:element name=”Product”>
<xs:complexType>
<xs:sequence>
<xs:element ref=”ProductTitle”/>
<xs:element ref=”ProductCode”/>
<xs:element ref=”ProductPriceRange” />
</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>


Advantage of Salami Slice:

[*] Transparent content. The components which make up Product are visible to other schemas, and to other parts of the same schema. The impact of this is that the types and elements within Product are reusable.

[*] Global scope. All components have global scope. The impact of this is that, irrespective of the value of elementFormDefault, the namespace of  ProductTitle and ProductCode will be exposed in instance documents.

[*] Verbose. Everything is laid out and clearly visible.

[*] Coupled. In this example we saw that the Product element depends on the ProductTitle and ProductCode  elements. If those elements were to change it would impact the Product  element. Thus, this design produces a set of interconnected (coupled) components.


[NOTE]

If your document is supposed to be an XML schema, then it must start
<xs:schema xmlns:xs=”http://www.w3.org/2001/XMLSchema”&gt;
You can use a different namespace prefix if you like (some people prefer “xsd” to “xs”) but the namespace URI must be exactly as written. It’s a URI not a URL – a symbolic constant that identifies the XML vocabulary as being the XML schema vocabulary. If you use a different namespace, then it’s not an XML schema any more.

In my next post I will explain about using  the Russian Doll approach. Wait for that 🙂


Thanks
R Vashi

String tokenize using XSLT

Hi,

First of all this is my first post on XML/XSLT. In this post I will cover how to perform the string manipulation to generate the data for different purposes using core XSLT Function. Before starting the problem lets cover some small introduction on XSLT.

Why XSLT?

Extensible Stylesheet Language Transformations (XSLT) provides a way to automatically transform XML data from one format to another. The target form is usually another XML document, but it doesn’t have to be; you can transform XML data into virtually anything simply by creating a XSLT stylesheet and processing the data. If you want to make changes to the output, you simply make changes to the stylesheet and process the XML again. This has the additional advantage of giving power to nonprogrammers, such as designers, who can edit the stylesheet and affect the results.

Hope the basic intro has given some idea about the features of XSLT and now we will see implementing a very small piece of it for String manipulation.

Few days back I got scenario where set of values were coming from external source into some CSV type string and we have to parse them in bunch of few XML tags and feed to the other end in the form ox XML.

Though XSLT provides a function “fn:tokenize” but instead of using that I will share other approach of using “substring-before”,”substring-after” XSLT Function.

Let assume you have a below input XML.

xml-stylesheet type=”text/xsl” href=”example.xslt”?>
<product>
 <code>
  <keys>prdName=SOME_PRD_NAME;catg=SOME_CATG_NAME;price=1234;prdcd=234;</keys>
   </code>

And the expected output:

<product>
<productName>SOME_PRD_NAME</productName>
<categoryName>SOME_CATG_NAME</categoryName>
<price>1234</price>
<productCode>224</productCode>

*NOTE The above xml and output is imaginary.


The XLST Code:

xml version=”1.0″ encoding=”ISO-8859-1″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform“>
<xsl:output method=”xml” version=”1.0″
encoding=”iso-8859-1″ indent=”yes”/>

<xsl:template match=”/”>
        <xsl:for-each select=”product/code”>
 <xsl:call-template name=”extractKey”>
  <xsl:with-param name=”string”>
   <xsl:value-of select=”keys”/>
  xsl:with-param>
 xsl:call-template>
      xsl:for-each>
 
xsl:template>

<!– This template is used to first of extract the xml tag and its value –>
<xsl:template name=”extractKey”>
 <xsl:param name=”string” />
  <xsl:param name=”delimiter” select=”‘;'” />
 <xsl:choose>
 <xsl:when test=”$delimiter and contains($string, $delimiter)”>
 <xsl:variable name=”temp”>
  <xsl:value-of select=”substring-before($string,$delimiter)” />
 xsl:variable>
 <xsl:call-template name=”createTag”>
           <xsl:with-param name=”keyVal” select=”$temp” />
 xsl:call-template>
 <xsl:call-template name=”extractKey”>
           <xsl:with-param name=”string” select=”substring-after($string,$delimiter)” />
 xsl:call-template>
    xsl:when>
xsl:choose>

xsl:template>

<!– This template is used to create the xml tag and its value –>
<xsl:template name=”createTag”>
 <xsl:param name=”keyVal” />
 <xsl:param name=”delimiter1″ select=”‘='” />
 <xsl:choose>
  <xsl:when test=”$delimiter1 and contains($keyVal, $delimiter1)”>
  <xsl:variable name=”dbKeyName”>
   <xsl:value-of select=”substring-before($keyVal,$delimiter1)” />
  xsl:variable>
    <xsl:element name=”{$dbKeyName}”>
   <xsl:value-of select=”substring-after($keyVal,$delimiter1)” />
   xsl:element>
     xsl:when>
 xsl:choose>
xsl:template>
xsl:stylesheet>

 </product>
 
</catalog>

Output

<?xml version=”1.0″ encoding=”UTF-16″?>
<prdName>SOME_PRD_NAME</prdName>
<catg>SOME_CATG_NAME</catg>
<price>1234</price>
<prdcd>234</prdcd>

Hope this helps.


R Vashi