Translate

Image of Android Wireless Application Development
Image of Beginning Google Maps API 3
Image of RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302) (Certification Press)
Image of XSLT 2.0 and XPath 2.0 Programmer's Reference (Programmer to Programmer)

XSL Recognizing Newlines

One of the major improvements in XSLT2 is the support for sequences as a replacement for node-sets. One of the new functions that takes advantage of this support is the the tokenize() function. The tokenize() function is equivalent to Python’s split function which takes a string and a delimiter and returns an array of the substrings that were separated by the specified delimiter(s). Perl and Ruby have an equivalent split function while Unix shells such as zsh and ksh93 provide similar functionality via different mechanisms. Consider the following trivial example (file.xml): <?xml version="1.0"?> <root> <text>line1 line2 line3 line4</text> </root> Suppose

Remove Namespaces from XML Documents

A frequent requirement when transforming XML documents is to remove some or all of a document namespaces. XML namespaces provide a simple and straightforward way to distinguish element and attribute names used in XML documents by associating them with namespaces identified by URI references. Consider the following example XML document: <work-order-list> <wo:job xmlns:wo="http:hello" freshness:timestamp="2006-01-12" xmlns:freshness="http://freshness" history:timestamp="2006" xmlns:history="http:history" xmlns:mnr="http:mnr"> <wo:work-order id="1" status="k" freshness:timestamp="2006-01-13" /> </wo:job> <wo:job xmlns:wo="http:hello" freshness:timestamp="2006-01-13" xmlns:freshness="http://freshness" history:timestamp="2006" xmlns:history="http:history" xmlns:mnr="http:mnr"> <wo:work-order id="2" status="k" freshness:timestamp="2006-01-14" /> </wo:job> <wo:job xmlns:wo="http:hello" freshness:timestamp="2006-01-14" xmlns:freshness="http://freshness" history:timestamp="2006" xmlns:history="http:history" xmlns:mnr="http:mnr"> <wo:work-order id="3" status="k" freshness:timestamp="2006-01-15" /> </wo:job> </work-order-list> It has several namespaces in it including wo, freshness,

XSLT Namespace Handling

This post assumes that you are familiar with XML, XSLT and XPath. It shows two methods to handle multiple default namespaces using XSLT1.0 and another method using XSLT2.0. Consider the following simple XML document: <Month xmlns="urn:murphy/ws/account/10/2007"> <LastPage>true</LastPage> <Accounts xmlns="urn:murphy/xml/account"> <Account> <AccountId>6289-F891T</AccountId> <Owner>6DS</Owner> <AccountType>Customer</AccountType> <Country>Philippines</Country> <SalesPerson>Joe Goon</SalesPerson> </Account> <Account> <AccountId>7142-J219F</AccountId> <Owner>IISC</Owner> <AccountType>Customer</AccountType> <Country>Canada</Country> <SalesPerson>Sarah Cergeo</SalesPerson> </Account> </Accounts> </Month> As you can see it has two namespaces, i.e urn:murphy/ws/account/10/2007 and urn:murphy/xml/account. The requirement is to transform this input document into the following output document using a stylesheet: <?xml version="1.0"?> <Month xmlns="urn:murphy/ws/account/10/2007"> <LastPage>true</LastPage> <Accounts xmlns="urn:murphy/xml/account"> <Account> <AccountId>6289-F891T</AccountId> <SalesPerson>Joe Goon</SalesPerson> </Account> <Account> <AccountId>7142-J219F</AccountId> <SalesPerson>Sarah

Manipulating XML Attributes using XSLT

Consider the following XML document called example.xml. As you can see, the document has two elements named sourcefield which contain multiple attributes. <source> <sourcefield businessname ="" datatype ="DATE" description ="" fieldnumber ="1" fieldproperty ="0" fieldtype ="ELEMITEM" hidden ="NO" keytype ="NOT A KEY" length ="19" level ="0" name ="BUSINESS_DATE" nullable ="NULL" occurs ="0" offset ="0" physicallength ="19" physicaloffset ="0" picturetext ="" precision ="19" scale ="0" usage_flags =""/> <sourcefield businessname ="JOHNSON" datatype ="DATE" description ="SHOEMAKER" fieldnumber ="1" fieldproperty ="1" fieldtype ="ELEMITEM" hidden ="NO" keytype ="NOT A KEY" length ="19" level ="3" name ="BUSINESS_DATE" nullable ="NULL" occurs ="0" offset ="0" physicallength ="19" physicaloffset