Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xslt: assigning template out put to a param

Status
Not open for further replies.

mercwrought

Programmer
Dec 2, 2004
176
US
Hi all, I’m new to xml and xslt. I’m more a SQL /vc6/c# kind of a guy. I’m working on displaying an xml file that has nested nodes. I have figured a few things out but I am now having trouble with templates. They make since but I don’t understand how to assignee the out put of one to a parameter.
Let me give you a something to work with and then ill ask some questions.
XML file
Code:
<Home>
  <Room title="Red" description="Screen">
    <Room title="Info" description="Main Info"  url="~/PassInfo.aspx">
      <Room title="Notes" description="Notes"  url="~/NotesEdit.aspx"/>
    </Room>
    <Room title="Bath" description="I don’t know"/>
    <Room title="Sink" description="I am running out of "/>
    <Room title="Weapons arsenal" description="really bad examples">
      <Room title="Pistol" description="ok I am out">
              <Room title="357" description="mag"/>
      </Room>
      <Room title="Ak47" description="there are several types"/>
    </Room>
  </Room>
</Home>

I don’t have a choice in the format of the xml it was provided in the specs. Basically it can continue as deep as it wants to go. So far I believe that I have figured out how to handle this. I will make a recursive template to work down the nodes transforming as I go. My problem (besides being new to xml and having really bad examples), I don’t know how to assign what a template would return to a xslt param.

Code:
<xsl:template name="RecursiveRooms">
  <xsl:param name="RLvl" select ="0"/>
  <xsl:param name="RString" select ="fn:concat('Home/Room/Room',***RepeateString()***)"/>

  <xsl:if test="">

    <!-- Do work here  -->

    <!-- Move The next lvl deep -->
    <xsl:call-template name="RecursiveRooms">
    <xsl:with-param name="RLvl" select="$RLvl + 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>




<xsl:template name="RepeateString">
  <xsl:param name="count">0</xsl:param>

  <xsl:if test="$count > 0">
    <!-- Do work here  -->
    <xsl:value-of select="/Room"/>
    <!-- Move The next lvl deep -->
    <xsl:call-template name="RepeateString">
    <xsl:with-param name="count" select="$count -1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>


I want to use the RepeateString template to create the path that I am checking. But I don’t know how to call the template from inside the select. I know xslt 2.0 has user defined function but I don’t know if 1.0 has and I need to keep this 1.0 friendly. Any ideas or questions.
 
mercwrought,

You are clearly struggling with recursion. Here is one tutorial I found that speaks directly to XSLT recursion, and it seems to relate to the problem I think you are trying to solve.

Give it a read and see if you can apply the lesson to your problem. I think you probably will want to recursively apply templates from within an <xsl:param></xsl:param>.

Also, it might be helpful to give a bit higher-level problem description, because there may be another way.

Tom Morrison
 
mercwrought said:
I want to use the RepeateString template to create the path that I am checking
Slight problem with this - you can't construct XPath expressions from variables (there's probably a good reason for this). Quite a good I page I occasionally refer to is Things XSLT can't do.


Most XSL elements with select attribute can also be written without it. In this case, I presume you wanted to do:
Code:
<xsl:param name="RString">
  <xsl:text>Home/Room/Room</xsl:text>
  <xsl:call-template name="RepeateString">
    <xsl:with-param name="count" select="0"/>
  </xsl:call-template>
</xsl:param>
k5tm is right, there's probably a better way to achieve what you want. Always best to describe the problem first. You may not need to use recursion, depending on what you need to do, you could just use templates:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" indent="yes" encoding="iso-8859-1" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"[/URL] omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Rooms</title>
      </head>
      <body>
        <dl>
          <xsl:apply-templates/>
        </dl>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Room">
    <dt>
      <xsl:value-of select="@title"/>
    </dt>
    <dd>
      <xsl:value-of select="@description"/>
    </dd>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top