Quinn Carver
Quinn Carver

Reputation: 605

Apply-templates which recurse Elements that contain attribute values to reference another Element

I am having difficulty applying templates to an Element which is the result of a selection of Elements by an attribute.

The code is essentially attempting to recurse an Element which contains other elements referenced by a name attribute.

The xml 'design' is not mine to change. The goal is to figure out how many widgets (eg: data fields) it would take to represent all the fields and 'contained' fields in a "page" Element.

Given has_depth.xml:

<?xml version="1.0" encoding="UTF-8"?>
<root> 
<page name="Locale" page_type="VIRTUAL">
    <datum name="lat"><type name="double" complex='0'/></datum>
    <datum name="lon"><type name="double" complex='0'/></datum>
</page>
<page  name="how_many_fields_do_i_have" page_type="CONCRETE">
   <datum mnemonic="foo"><type name="int32" complex='0'/></datum>
   <datum mnemonic="foo_locale"><type name="Locale" complex='1'/></datum>
</page>
</root>

and find_depth.xslt:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text" indent="yes"/>
 
 <xsl:template match="/root">
     <xsl:apply-templates select="//page[@name='how_many_fields_do_i_have']" />
 </xsl:template>

<xsl:template match="page" >
    <xsl:message><xsl:value-of select="concat('getting depth for page ',@name)"/></xsl:message>
    <xsl:variable name="depth">
            <!-- ask the Page's first datum for its depth --> 
            <xsl:apply-templates select="./datum[1]" />
    </xsl:variable>
    <xsl:message><xsl:value-of select="concat('It would take ',$depth,' widgets to show this page in a gui')"/></xsl:message>
</xsl:template>

<xsl:template match="datum[./type[@complex='0']]"  >
    <xsl:message> <xsl:value-of select="concat('matched simpletype:',current()/type/@name,':',@mnemonic)"/></xsl:message>
    <xsl:variable name="below"> 
    <xsl:choose>
        <xsl:when test='current()=last()'><xsl:value-of select='0'/></xsl:when>
        <xsl:otherwise><xsl:apply-templates select="following-sibling::datum"/></xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:message> <xsl:value-of select="concat('depth after simple ',@mnemonic,': ', 1 + $below)"/></xsl:message>
    <xsl:value-of select='1 + $below'/>
</xsl:template>

<xsl:template match="datum[./type[@complex='1']]" >
    <xsl:message> <xsl:value-of select="concat('matched complextype:',./type/@name,':',@mnemonic)"/></xsl:message>
    <!-- start depth again recursing into the page[type="'VIRTUAL'"] -->
    <xsl:apply-templates select="//page[@name='./type/@name']" />
<xsl:if test='current()!=last()'>
    <xsl:apply-templates select="following-sibling::datum"/>
</xsl:if>
</xsl:template>

I get the following when the bash command: xsltproc find_depth.xslt has_depth.xml

getting depth for page how_many_fields_do_i_have
matched simpletype:int32:foo
matched complextype:Locale:foo_locale
depth after simple foo: NaN
It would take NaN widgets to show this page in a gui

Where I would expect the output:

getting depth for page how_many_fields_do_i_have
matched simpletype:int32:foo
matched complextype:Locale:foo_locale
getting depth for page Locale
matched simpletype:double:lat
matched simpletype:double:lon
depth after simple lon: 1
depth after simple lat: 2
depth after simple foo: 3
It would take 3 widgets to show this page in a gui

It looks like things go sour when I try to apply-templates to Locale after selecting it by ./type/@name which evaluations in the message statement too "Locale" (see between colons in the 3rd line of the output statement.) However to be sure I linted using xmllint --xpath "//page[@name='Locale']" has_depth.xml and found:

xmllint --xpath "//page[@name='Locale']" has_depth.xml 
<page name="Locale" page_type="VIRTUAL">
    <datum name="lat"><type name="double" complex="0"/></datum>
    <datum name="lon"><type name="double" complex="0"/></datum>
</page>

which is as I expect.

Questions is: How can I get this thing to recurse as one would expect and treat the sums as numbers as recusion bubbles up?

Upvotes: 0

Views: 53

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

I find this very confusing, but maybe the following stylesheet can give you some starting point:

XSLT 1.0

<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:strip-space elements="*"/>

<xsl:key name="page" match="page" use="@name" />

<xsl:template match="/root">
    <xsl:variable name="temp">
        <xsl:apply-templates select="page[@name='how_many_fields_do_i_have']"/>
    </xsl:variable>
    <result>
        <xsl:value-of select="string-length($temp)"/>
    </result>
 </xsl:template>
 
<xsl:template match="type[@complex='0']">x</xsl:template>
 
<xsl:template match="type[@complex='1']">
    <xsl:apply-templates select="key('page', @name)"/>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, this will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<result>3</result>

Of course, this is no more than a wild guess intended more to clarify the problem than to solve it.

Upvotes: 1

Related Questions