We help IT Professionals succeed at work.

Counting ancestors using XSLT for storage in a variable

552 Views
Last Modified: 2013-11-18
Hi. This is my first post. English is not my first language, and I am fairly new to xsl as well to make matters worse. Hope I can make myself understood despite this handicap.

I have been assigned the task to transform an xml document to html using an xsl style sheet. The xml file is a very special "animal". It is originally sgml but the file is to be translated using a translation tool, and for this purpose the file content has been wrapped in xml tags and saved as xml. The object of the transformation to html is to allow the translator to preview the translated document in a way that mimics the original document layout with paragraph numbering, indentation, fonts and font sizes etc.

The xml structure can be regarded as flat, all of the tags that I am interested in are on the same level. The name of the original sgml tag is stored in an attribute called DisplayText. By looking at the Type attribute it is possible to determine if it is the opening or closing sgml tag, i.e. <paragraph> or </paragraph>, for example:

<ut Type="start" DisplayText="paragraph">&lt;paragraph&gt;></ut>
Some content that should be translated
<ut Type="end" DisplayText="paragraph">&lt;/paragraph&gt;</ut>

The structure of the sgml document is more complex with headers and subitems. It contains lists where the list items are numbered from 1 and up, or A, B, C or roman numericals. And a list could be a sublist to another list. A simple sample would look like this:

<list>
      <listitem>Item 1</listitem>
      <listitem>Item 2</listitem>
      <listitem>Item 3</listitem>
      <list>
            <listitem>Item A</listitem>
            <listitem>Item B</listitem>
      </list>
</list>

And the document saved as xml for translation tool:

<ut Type="start"  DisplayText="list">&lt;list&gt;</ut>
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut>
Item 1
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut>
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut>
Item 2
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut>
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut>
Item 3
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut>
<ut Type="start"  DisplayText="list">&lt;list&gt;</ut>
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut>
Item A
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut>
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut>
Item B
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut>
<ut Type="end"  DisplayText="list">&lt;/list&gt;</ut>
<ut Type="end"  DisplayText="list">&lt;/list&gt;</ut>

Lets say that a matched ut element describes a <listitem> tag:

<ut Type="start" DisplayText="listitem">&lt;listitem&gt;></ut>

then I must find out to which list it belongs, so that I can indent the entry properly. One idea would be to count <ut> elements that describe an opening <list> tag:

<ut Type="start"  DisplayText="list">&lt;list&gt;</ut>

and then count closing </list> tags:

<ut Type="end"  DisplayText="list">&lt;/list&gt;</ut>

 If there is one more opening tag than closing tag the item would belong to a top level list. If there are two opening tags more than closing tags the item belong to a list inside of a list and so on.

Is it possible to count ancestors of a specific type, where the information we need is stored in an attribute, and put the value in a variable? I.e. count all <ut> elements above the current where the attribute DisplayText = list and the Type = start and then where Type = end?

Does this approach make any sense at all or would you more experienced in xslt choose a different solution? Is it possible for example, using xslt, to rebuild the original document structure (using the sgml tag information from the DisplayText and Type attributes) in some way and then apply a style sheet to this document?
Comment
Watch Question

> Is it possible to count ancestors of a specific type, where the information we need is
> stored in an attribute, and put the value in a variable?

You can count the ancestors of a specific type using the XPath expression:

count(ancestor-or-self::list)

To place this in the attribute of your output you would simply use the XSLT:


   .... OTHER XSLT ....



> I.e. count all  elements above the current where the attribute DisplayText = list and the
> Type = start and then where Type = end?

(SEE BELOW FOR SOURCE)

Are you trying to transform SOURCE #1 into SOURCE #2? If so, then SOURCE #3 should do the trick.


> Does this approach make any sense at all or would you more experienced in xslt choose a
> different solution?

I do not fully understand your problem. Why are you transforming into  nodes?


> Is it possible for example, using xslt, to rebuild the original document structure (using the sgml tag
> information from the DisplayText and Type attributes) in some way and then apply a style sheet to
> this document?

If you are trying to convert your SGML into HTML, then yes, you can match the same document structure as the input where elements and attributes are transformed into HTML equivalents.

You can use the extremely powerful XSLT element named  to achieve automatic section, chapter, figure, table, etc. numbering.
SOURCE #1
=========
 
<list>
      <listitem>Item 1</listitem>
      <listitem>Item 2</listitem>
      <listitem>Item 3</listitem>
      <list>
            <listitem>Item A</listitem>
            <listitem>Item B</listitem>
      </list>
</list>
 
 
SOURCE #2
=========
 
<ut Type="start"  DisplayText="list">&lt;list&gt;</ut> 
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut> 
Item 1 
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut> 
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut> 
Item 2 
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut> 
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut> 
Item 3 
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut> 
<ut Type="start"  DisplayText="list">&lt;list&gt;</ut> 
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut> 
Item A 
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut> 
<ut Type="start"  DisplayText="listitem">&lt;listitem&gt;</ut> 
Item B 
<ut Type="end"  DisplayText="listitem">&lt;/listitem&gt;</ut> 
<ut Type="end"  DisplayText="list">&lt;/list&gt;</ut> 
<ut Type="end"  DisplayText="list">&lt;/list&gt;</ut> 
 
 
SOURCE #3 : THE TRANSFORM
=========================
 
<xsl:template match="list">
	<ut Type="start" DisplayText="list" Depth="{count(ancestor-or-self::list)}">&lt;list&gt;</ut>
	<xsl:apply-templates select="listitem"/>
	<ut Type="end" DisplayText="list" Depth="{count(ancestor-or-self::list)}">&lt;/list&gt;</ut>
</xsl:template>
 
<xsl:template match="list/listitem">
	<ut Type="start" DisplayText="list" Depth="{count(ancestor::list)}">&lt;listitem&gt;</ut>
	<xsl:value-of select="."/>
	<ut Type="end" DisplayText="list" Depth="{count(ancestor::list)}">&lt;/listitem&gt;</ut>
</xsl:template>

Open in new window

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you for your suggested solution, it helped me a lot.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.