Elements

1. Using BR tag
2. How to create an element from the content of another element
3. How to find maximum number of children
4. Element name normalisation

1.

Using BR tag

Ben Robb


Q: Expansion
My code is 

xml file:
<cart>10021</cart><break/>
<cart>10022</cart><break/>
<cart>10023</cart><break/>
=======================

xsl file:
<xsl:template match="break">
<br/>
<xsl:apply-templates />
</xsl:template>
=======================

IE viusalize:
 10021
 10022
 10023
                
Netscape visualize: 
100211002210023
    

Yes - this is a little tricky. Netscape is less forgiving when it comes to XHTML. For NS4/4.5 it works if you have <br /> instead of <br/>

For NS2 you will have to use <p> tags, I'm afraid - it simply doesn't work. I've not tried it on IE3 or NS3 - so I can't say what it would do in that case.

What are you using to do the transform? If you have access to the parsed string before it gets written to the browser or the file system, you could always do a search and replace on the fly, which would replace all of the <br/> tags with <br> tags.

2.

How to create an element from the content of another element

Michael Kay

Attribute value templates.

<xsl:element name="{substring-before(.,':')}">

3.

How to find maximum number of children

Jeni Tennison


>I would like to find the length of the longest chain of <x>'s in the
>following document:
>
><a><x/<x/></a>
><a><x/></a>
><a><x/><x/><x/><x/></a>

One way that you could do this is to do a xsl:for-each on each of the 'a' elements, sort them in order of the number of 'x' element children then have (in descending order - biggest first), and then take the first one of that list - that's the 'a' that you're interested in. So:

<xsl:for-each select="a">
  <xsl:sort select="count(x)" order="descending" />
  <xsl:if test="position() = 1">
    <!-- this is the 'a' you were after -->
    Max number of 'x's = <xsl:value-of select="count(x)" />
  </xsl:if>
</xsl:for-each>

If this will work in your situation, I *think* it's the best method. If it won't, there are other ways of doing it, just about (I can think of two - selecting the 'a' that does not have a sibling with more 'x' children, and using keys - but I haven't tried them out). Let me know if you want to see them as well.

4.

Element name normalisation

Wendell Piez

Sometimes SGML tools will do things that mess with tag names, such as folding them uniformly into upper or lower case. This is a pain because they must be switched back into their original forms (their DTD-normalized forms) to be validated as XML, if you want to move the file back into an XML environment. If their proper forms happen to include mixed-case names, you're in a hole. The perfect solution would be a DTD-aware process. A less perfect approach is to provide the correct names in an alternate spec of some sort. Since unextended XSLT has no access to the DTD's model, this stylesheet takes the latter approach.

This stylesheet is wired to run with a configuration file 'mlangnames.xml' as its listing of names (element names, attribute names and names of enumerated attribute values); but you can override that either by changing the stylesheet or by setting by switching the 'names' parameter on the command line, thus normalizing to the tag set of your choice.

So, for example, a routine invoking the Saxon processor could run:

Saxon -o fixed.xml messedup.xml xmlnames.xsl names=mynames.xml

See mlangnames.xml for the format of the names lists and this is the stylesheet