Re: [xsl] in-document references

Subject: Re: [xsl] in-document references
From: S Woodside <sbwoodside@xxxxxxxxx>
Date: Mon, 6 Jan 2003 19:41:07 -0500
I'm not sure if the word "flatten" was right. I want it to expand all of the defines so that everywhere there is a "ref" it is replaced by whatever is in the "define". I could pre-process the file with XSLT that does that, but I'd rather skip that step since it could result in a potentially very large intermediate file.

On Monday, January 6, 2003, at 06:51 PM, Martinez, Brian wrote:

From: S Woodside [mailto:sbwoodside@xxxxxxxxx]
   <xsl:template match="ref">
     <li>Ref:
       <b><xsl:value-of select="@name"/></b>
       <ul>
         <xsl:apply-templates
               select="//define[@name=current()/@name]"
               mode="def"/>
       </ul>
     </li>
   </xsl:template>
[snip]

Your current select expression tells the processor to select all 'define'
nodes that are descendants of the document element.

That's what I want to do. Here's a complete example:



=============data.xml==================


<!--This is a Relax NG schema-->
<grammar>
	<element name="Id">
			<ref name="string"/>
	</element>
	<define name="string">
			<data type="string"/>
	</define>
</grammar>


=============form.xsl==================


[...]

  <xsl:template match="element">
    <li>Element:
          <xsl:value-of select="@name"/>
          <ul>
            <xsl:apply-templates/>
          </ul>
    </li>
  </xsl:template>

 <xsl:template match="ref">
    <li>Ref:
      <b><xsl:value-of select="@name"/></b>
      <ul>
        <xsl:apply-templates
              select="//define[@name=current()/@name]"
              mode="def"/>
      </ul>
    </li>
  </xsl:template>

  <xsl:template match="define" mode="def">
    <xsl:apply-templates/>
  </xsl:template>

<xsl:template match="data">
<li>
<xsl:value-of select="ancestor-or-self::element[@name][1]/@name"/>
</li>
</xsl:template>


<!--This rule is just to kill the defines when they are encountered later-->
<xsl:template match="define">
<li>[Define:
<b><xsl:value-of select="@name"/></b>]
</li>
</xsl:template>



==================output should be=============== (ignoring whitespace issues)

<li>Element: Id
    <ul>
      <li>Ref: <b>string</b>
        <ul>
          <li>
                 Id   <!--This is the hard part-->
          </li>
	  </ul>
    </ul>
</li>
<li>
  [Define: <b>string</b>]
</li>

=================what I get right now================

<li>Element: Id
    <ul>
      <li>Ref: <b>string</b>
        <ul>
          <li>
                 <!--I get nothing here since there is no "element"
                   parent of "define" in the data-->
          </li>
	  </ul>
    </ul>
</li>
<li>
  [Define: <b>string</b>]
</li>

To select just
descendants of the context node, do:

<xsl:apply-templates select=".//define[@name=current()/@name]" mode="def"/>

The problem is that the "context" of the context node includes the ancestor elements and so on, which I wish to access from <xsl:template match="define">. Does that make sense?


(This doesn't address optimization of course--this search pattern can be
quite expensive.)

I can live with that, I'm using AxKit and static data.


simon

---
www.simonwoodside.com


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread