EzDevInfo.com

xslt interview questions

Top xslt frequently asked interview questions

Check if a string is null or empty in XSLT

How can I check if a value is null or empty with XSL?

For example, if categoryName is empty? I'm using a when choosing construct.

For example:

<xsl:choose>
    <xsl:when test="categoryName !=null">
        <xsl:value-of select="categoryName " />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="other" />
    </xsl:otherwise>
</xsl:choose>

Source: (StackOverflow)

How can I make XSLT work in chrome?

I have an XML document here that is served with a corresponding XSL file. The transformation is left to be executed client-side, without JavaScript.

This works fine in IE (shock horror), but in Google Chrome, just displays the document's text nodes.

I know that it is possible to do client-side XSL in Chrome, as I have seen examples of it, but I am yet to be able to replicate this success myself

What am I doing wrong?


Source: (StackOverflow)

Advertisements

How to apply an XSLT Stylesheet in C#

I want to apply an XSLT Stylesheet to an XML Document using C# and write the output to a File.


Source: (StackOverflow)

Producing a new line in XSLT

I want to produce a newline for text output in XSLT. Any ideas?


Source: (StackOverflow)

Set HTML5 doctype with XSLT

How would I cleanly set the doctype of a file to HTML5 <!DOCTYPE html> via XSLT (in this case with collective.xdv)

The following, which is the best my Google foo has been able to find:

<xsl:output
    method="html"
    doctype-public="XSLT-compat"
    omit-xml-declaration="yes"
    encoding="UTF-8"
    indent="yes" />

produces:

<!DOCTYPE html PUBLIC "XSLT-compat" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Source: (StackOverflow)

XMLHttpRequest Origin null is not allowed Access-Control-Allow-Origin for file:/// to file:/// (Serverless)

I'm trying to create a website that can be downloaded and run locally by launching its index file.

All the files are local, no resources are used online.

When I try to use the AJAXSLT plugin for jQuery to process an XML file with an XSL template (in sub directories), I receive the following errors:

XMLHttpRequest cannot load file:///C:/path/to/XSL%20Website/data/home.xml. Origin null is not allowed by Access-Control-Allow-Origin.

XMLHttpRequest cannot load file:///C:/path/to/XSL%20Website/assets/xsl/main.xsl. Origin null is not allowed by Access-Control-Allow-Origin.

The index file making the request is file:///C:/path/to/XSL%20Website/index.html while the JavaScript files used are stored in file:///C:/path/to/XSL%20Website/assets/js/.

How can I do to fix this issue?


Source: (StackOverflow)

How do I remove the BOM character from my xml file [duplicate]

This question already has an answer here:

I am using xsl to control the output of my xml file, but the BOM character is being added.


Source: (StackOverflow)

Getting the value of an attribute in XML

How would one get the value of attribute1 (blah) in the following xml using xslt:

<name attribute1="blah" attribute2="blahblah">
</name>

Source: (StackOverflow)

How to implement if-else statement in XSLT?

I am trying to implement an if -else statement in XSLT but my code just doesn't parse. Does anyone have any ideas?

  <xsl:variable name="CreatedDate" select="@createDate"/>
  <xsl:variable name="IDAppendedDate" select="2012-01-01" />
  <b>date: <xsl:value-of select="$CreatedDate"/></b> 

  <xsl:if test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:if>
  <xsl:else>
    <h2> dooooooooooooo </h2>
  </xsl:else>

Source: (StackOverflow)

Is XSLT worth it? [closed]

A while ago, I started on a project where I designed a html-esque XML schema so that authors could write their content (educational course material) in a simplified format which would then be transformed into HTML via XSLT. I played around (struggled) with it for a while and got it to a very basic level but then was too annoyed by the limitations I was encountering (which may well have been limitations of my knowledge) and when I read a blog suggesting to ditch XSLT and just write your own XML-to-whatever parser in your language of choice, I eagerly jumped onto that and it's worked out brilliantly.

I'm still working on it to this day (I'm actually supposed to be working on it right now, instead of playing on SO), and I am seeing more and more things which make me think that the decision to ditch XSLT was a good one.

I know that XSLT has its place, in that it is an accepted standard, and that if everyone is writing their own interpreters, 90% of them will end up on TheDailyWTF. But given that it is a functional style language instead of the procedural style which most programmers are familiar with, for someone embarking on a project such as my own, would you recommend they go down the path that I did, or stick it out with XSLT?


Source: (StackOverflow)

Can you put two conditions in an xslt test attribute?

Is this right for When 4 < 5 and 1 < 2 ?

<xsl:when test="4 &lt; 5 AND 1 &lt; 2" >
<!-- do something -->
</xsl:when>

Source: (StackOverflow)

Is there any way to do a "Replace Or Insert" using web.config transformation?

I'm using web.config transformation as described in the below post in order to generate configs for different environments.

http://vishaljoshi.blogspot.com/2009/03/web-deployment-webconfig-transformation_23.html

I can do a "Replace" transformation by matching on the key, e.g.

<add key="Environment" value="Live" xdt:Transform="Replace" xdt:Locator="Match(key)" />

And I can do "Inserts" e.g.

<add key="UseLivePaymentService" value="true" xdt:Transform="Insert" />

But what I would really find useful is a ReplaceOrInsert transformation, as I can't always rely on the original config file having/not having a certain key.

Is there any way to do this?


Source: (StackOverflow)

Is there an XSLT name-of element?

In XSLT there is the

<xsl:value-of select="expression"/>

to get the value of an element, but is there something to select the tag-name of the element?

In a situation like this:

<person>
  <!-- required stuff -->
  <name>Robert</name>
  <!-- optional stuff, free form for future extension. 
       Using XMLSchema's xsd:any -->
  <profession>programmer</profession>
  <hobby>photography</hobby>
</person>

<xsl:for-each select="person">
   <xsl:tag-of select="."/> : <xsl:value-of select="."/>
</xsl:for-each>

To get output like this:

name : Robert
profession : programmer
hobby : photography

Of course the above XSLT won't compile because

 <xsl:tag-of select="expression"/>

doesn't exist. But how could this be done?


Source: (StackOverflow)

Counter inside xsl:for-each loop

How to get a counter inside xsl:for-each loop that would reflect the number of current element processed.
For example my source XML is

<books>
    <book>
    	<title>The Unbearable Lightness of Being </title>
    </book>
    <book>
    	<title>Narcissus and Goldmund</title>
    </book>
    <book>
    	<title>Choke</title>
    </book>
</books>

What I want to get is:

<newBooks>
    <newBook>
    	<countNo>1</countNo>
    	<title>The Unbearable Lightness of Being </title>
    </newBook>
    <newBook>
    	<countNo>2</countNo>
    	<title>Narcissus and Goldmund</title>
    </newBook>
    <newBook>
    	<countNo>3</countNo>
    	<title>Choke</title>
    </newBook>
</newBooks>

The XSLT to modify:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
    	<newBooks>
    		<xsl:for-each select="books/book">
    			<newBook>
    				<countNo>???</countNo>
    				<title>
    					<xsl:value-of select="title"/>
    				</title>
    			</newBook>
    		</xsl:for-each>
    	</newBooks>
    </xsl:template>
</xsl:stylesheet>

So the question is what to put in place of ???. Is there any standard keyword or do I simply must declare a variable and increment it inside the loop?

As the question is pretty long I should probably expect one line or one word answer :)


Source: (StackOverflow)

Pretty printing XML with javascript

I have a string that represents a non indented XML that I would like to pretty-print. For example:

<root><node/></root>

should become:

<root>
  <node/>
</root>

Syntax highlighting is not a requirement. To tackle the problem I first transform the XML to add carriage returns and white spaces and then use a pre tag to output the XML. To add new lines and white spaces I wrote the following function:

function formatXml(xml) {
    var formatted = '';
    var reg = /(>)(<)(\/*)/g;
    xml = xml.replace(reg, '$1\r\n$2$3');
    var pad = 0;
    jQuery.each(xml.split('\r\n'), function(index, node) {
        var indent = 0;
        if (node.match( /.+<\/\w[^>]*>$/ )) {
            indent = 0;
        } else if (node.match( /^<\/\w/ )) {
            if (pad != 0) {
                pad -= 1;
            }
        } else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
            indent = 1;
        } else {
            indent = 0;
        }

        var padding = '';
        for (var i = 0; i < pad; i++) {
            padding += '  ';
        }

        formatted += padding + node + '\r\n';
        pad += indent;
    });

    return formatted;
}

I then call the function like this:

jQuery('pre.formatted-xml').text(formatXml('<root><node1/></root>'));

This works perfectly fine for me but while I was writing the previous function I thought that there must be a better way. So my question is do you know of any better way given an XML string to pretty-print it in an html page? Any javascript frameworks and/or plugins that could do the job are welcome. My only requirement is this to be done on the client side.


Source: (StackOverflow)