Re: [xslt] template match="form"



Well folks, I think I solved this myself.  Maybe if I would have spent less time formulating an example and more time towards solving the problem I would have been better off.  The sample stylessheet that gives me the desired result follows. I realized that I should always just match form and then check for the attribute within the template.

 

 

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

<xsl:stylesheet

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:rn="http://schemas.rightnow.com/crm/document"

    version="1.0">

 

  <xsl:output encoding="UTF-8" method="html"/>

 

  <xsl:template match="node()|@*">

    <xsl:copy>

          <xsl:apply-templates select="node()|@*" />

    </xsl:copy>

  </xsl:template>

 

 

  <xsl:template match="form">

  <xsl:choose>

      <xsl:when test="@rn:webform = 'true'">

 

        <form action="" name="_mainer">

        <input type="hidden" name="In_form_template" />

 

        <xsl:apply-templates />

 

        </form>

      </xsl:when>

      <xsl:otherwise>

        <xsl:copy>

          <xsl:apply-templates select="node()|@*" />

        </xsl:copy>

      </xsl:otherwise>

    </xsl:choose>

  </xsl:template>

 

  <xsl:template match="rn:question|rn:answer">

    <input type="hidden" AARON="in question and answer"/>

  </xsl:template>

 

</xsl:stylesheet>

 

From: xslt-bounces gnome org [mailto:xslt-bounces gnome org] On Behalf Of Schubert, Aaron
Sent: Wednesday, October 18, 2006 7:34 PM
To: xslt gnome org
Subject: [xslt] template match="form"

 

So, I am in a bind and need to figure out how I can match a form item in xsl that contains a certain attribute – and output my own formatted tag without duplicating the form tag.  I need to recognize our special forms and have a huge template to match depending on if there is a certain attribute (rn:webform).  If it is a normal form I want it to match the catchall template  <xsl:template match="node()|@*"> and output it exactly as it appears in the original xml.  An extremely stripped down example follows:

 

 

Here is the incoming xml that needs to be transformed.

-----------------------------------------------------------

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

<document>

  <html xmlns:xsl="http://www.w3.org/1999/xhtml" xmlns:rn="http://schemas.rightnow.com/crm/document">

    <head><title>Page 1</title></head>

    <body><form rn:webform="true">

      <rn:question question_id="2" split="False">#2</rn:question><p><input id="submit_btn" type="submit" value="Submit" name="submit_btn" rn:submit="true"/></p></form></body>

  </html>

</document>

-----------------------------------------------------------

Here is a sample xsl file which will illustrate the problem

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

<xsl:stylesheet

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:rn="http://schemas.rightnow.com/crm/document"

    version="1.0">

 

  <xsl:output encoding="UTF-8" method="html"/>

 

  <xsl:template match="node()|@*">

    <xsl:copy>

      <xsl:apply-templates select="node()|@*" />

    </xsl:copy>

  </xsl:template>

 

  <xsl:template match="form/@rn:webform">

 

    <form action="" name="_main">

      <input type="hidden" name="In_form_template" />

 

      <xsl:apply-templates />

 

    </form>

  </xsl:template>

 

  <xsl:template match="rn:question|rn:answer">

    <input type="hidden" AARON="in question and answer"/>

  </xsl:template>

 

</xsl:stylesheet>

-----------------------------------------------------------

The output I am getting (this can be reproduced with xsltproc) is:

-----------------------------------------------------------

<document><html>

<head><title>Page 1</title></head>

<body><form>

<form action="" name="_mainer">

<input type="hidden" name="In_form_template">true</form>

<input type="hidden" AARON="in question and answer"><p><input id="submit_btn" type="submit" value="Submit" name="submit_btn" submit="true"></p>

</form></body>

</html></document>

-----------------------------------------------------------

As you can see I am getting two opening and closing form tags.  What I want is the <form action….. and then the eventual closing form tag as everything needs to be encapsulated with this.  I tried modifying the <xsl:template match="node()|@*"> to the following (which would be a bad solution anyways but I figured I would try):

-----------------------------------------------------------

  <xsl:template match="node()|@*">

    <xsl:choose>

      <xsl:when test="@rn:webform = 'true'">

        <xsl:apply-templates select="node()|@*" />

      </xsl:when>

      <xsl:otherwise>

        <xsl:copy>

          <xsl:apply-templates select="node()|@*" />

        </xsl:copy>

      </xsl:otherwise>

    </xsl:choose>

  </xsl:template>

-----------------------------------------------------------

This allowed me to get rid of the duplication and the opening form tag is correct. However, I need the closing form tag at the end.  As can also be seen, the input tag that was in the rn:question template is only appearing after the closing form tag even though I have the <xsl:apply-templates /> before it.

 See below: HELP

-----------------------------------------------------------

<document><html>

<head><title>Page 1</title></head>

<body>

<form action="" name="_mainer">

<input type="hidden" name="In_form_template">true</form>

<input type="hidden" AARON="in question and answer"><p><input id="submit_btn" type="submit" value="Submit" name="submit_btn" submit="true"></p>

</body>

</html></document>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]