Interested in linking to "The ABCs of XML, Part 4"?
You may use the Headline, Deck, Byline and URL of this article on your Web site. To link to this article, select and copy the HTML code below and paste it on your own Web site.
In this sample, the namespace is assigned the explicit prefix rs and therefore is no longer the default namespace. Because no default namespace is declared, and because RecipeElement does not use the rs prefix, it belongs to the default null namespace and may be matched as RecipeElement and OtherElements respectively.
In this sample, the namespace is assigned a prefix which is not used by RecipeElement. Therefore, matching this element requires a namespace declaration with a prefix in your stylesheet. Although it may be confusing, the XSLT stylesheet prefix is not required to match the XML source document prefix because a prefix is only a shorthand local alias of the full namespace. However, I recommend using the identical prefixes in your transforms to avoid confusion.
You may be surprised to learn that OtherElements is included in the default null namespace! Only default namespaces are inherited by child nodes. The prefixed namespace used in this sample is not the default namespace, and therefore is not inherited by OtherElements. This means that RecipeElement and OtherElements are members of different namespaces.
Character Entity Escape Sequences
The most commonly used character entities and their escape sequences are shown here.
I used the top of an RSBatch master recipe for the above samples. Rockwell uses this default namespace for each recipe saved in XML format. Therefore, your transformation for any RSBatch recipe should look like this.
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.
XML and XSLT namespaces can be one of the most frustrating aspects of developing XSLT transforms if you don’t understand these fine points, so I suggest you reread this section and make sure you understand how default namespaces differ from prefixed namespaces. A little experimentation can help too.
Problem 3 – My stylesheet automatically adds namespace attributes to output elements. Can I remove these?
You can never produce an output file using namespace prefixes that have not been declared; therefore, each namespace used in the output must be declared at least once. However, you may find that your output is littered with namespace declarations in nearly every element, whether they are used by that element or not. To suppress unnecessary namespace declarations, use the attribute exclude-results-prefixes in the xsl:stylesheet element (see previous example).
The value of this attribute is a list of namespace prefixes separated by white space. Remember this will not remove all namespace declarations, but it will remove unused and unnecessary namespace declarations.
Problem 4 – Text Output
Generating text output such as a comma-separated-values (CSV) file can prove more difficult than you may expect until you understand how the processor handles text and white space. Often you may find it difficult to generate one particular character like a quotation mark (“) because it has specific meaning in XML or by the XSLT language. Here are a few pointers for generating plain text output.
Make sure to include the output element before your templates as follows: <xsl:output method=”text”/>.
Use concat() function when creating output that is a combination of many text nodes or a mix of text nodes and literal text. This sample will generate a single CSV row with two comma separated columns, where each value is contained within quotation marks.
<xsl:value-of select=”concat(‘"’,Value1, ‘",
"’, Value2, ‘" ’)”/>
Use the <xsl:text> element instead of literal text in your templates. This will give you more explicit control of your output while maintaining an easy to read transform. XSLT elements are not allowed inside an <xsl:text> element. The following verbose sample will produce the same output as the previous sample.