Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 22nd, 2008, 11:56 AM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT for parsing XHTML Form

I am writing a proxy server that is going to parse all forms such that they go through the proxy server and add a few hidden form fields, e.g.
If I have the XHTML that has form like
<form id=".." name=".." action="/dosomething" method="post">

I would like to parse it so that I can store the
action and method attributes in my session and change the form to
<form id=".." name=".." action="/myserver" method="post">
<input type="hidden" name="myfield" value="value"/>
...

I am thinking about using XSL for this, but not sure if it's easy enough, basically I want to do following:
1. parse XHTML forms and store form name/action/method to my session
2. change action attribute (the exact path will be generated dynamically).
3. I want to copy all attributes of form as they are except action/method.
4. add hidden fields


Is this easily doable in XSL, alternatively I can just parse XHTML as XML and create my own XHTML output.



 
Old August 22nd, 2008, 12:24 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can easily do 2, 3 and 4 with XSLT:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefix="xhtml">

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

  <xsl:template match="xhtml:form">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="action">/myserver</xsl:attribute>
      <input type="hidden" name="myfield" value="value"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
1 (storing "form name/action/method to my session") will require use of an extension object or function.


--
  Martin Honnen
  Microsoft MVP - XML
 
Old August 22nd, 2008, 12:48 PM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin, that will get me started. Regarding #1, I am using Java so I am not sure if the APIs support callback functions but I will look into it.
 
Old August 22nd, 2008, 04:58 PM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I noticed following error when running it through Java XSLT transformer:
line 16: Attribute 'action' outside of element.

It seems like it is complaining about
      <xsl:attribute name="action">/myserver</xsl:attribute>

Any idea? Also, I would like to skip some of XHTML tags such as <html>, <title>. I think I can just match for those tags and not emit any output, but is there a better way. Thanks again.
 
Old August 23rd, 2008, 08:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I typed my stylesheet directly in the forum editor without testing it.
However when testing now I find that the only error is naming the attribute exclude-result-prefix instead of exclude-result-prefixes.
So the following correction should work fine:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="xhtml">

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

  <xsl:template match="xhtml:form">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="action">/myserver</xsl:attribute>
      <input type="hidden" name="myfield" value="value"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
When applied to the input XHTML
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
</head>
<body>
<form id=".." name=".." action="/dosomething" method="post">
<input type="text" name="foo"/>
<input type="submit"/>
</form>
</body>
</html>
the result with Saxon 6.5 is
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
</head>
<body>
<form id=".." name=".." action="/myserver" method="post"><input xmlns:xhtml="http://www.w3.org/1999/xhtml" type="hidden" name="myfield" value="value"/>
<input type="text" name="foo"/>
<input type="submit"/>
</form>
</body>
</html>
I am not sure why you get the error you mention, sounds as if you have moved an instruction to create an attribute to a position where it is not allowed to create an attribute.

As for not copying certain elements, if you want to remove the title element you only need to add the template
Code:
<xsl:template match="xhtml:title"/>
If you don't want to emit an html element then you need to add
Code:
<xsl:template match="xhtml:html">
  <xsl:apply-templates/>
</xsl:template>
that makes sure the element is not copied but its children are processed.


--
  Martin Honnen
  Microsoft MVP - XML
 
Old August 24th, 2008, 11:08 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's nothing wrong with that xsl:attribute instruction in itself, but you are probably issuing it in a context where you can't output attributes - for example, after outputting a child element.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
 
Old August 26th, 2008, 06:22 PM
Registered User
 
Join Date: Aug 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin and Michael again, though I am still getting warning about "outside of element" but transformation is working.






Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt -> xhtml validation rahulsk1947 XSLT 3 May 17th, 2007 02:54 AM
parsing XHTML document Jean-Philippe XML 2 March 22nd, 2006 03:51 AM
producing XHTML from XSLT holdmykidney XSLT 1 August 24th, 2004 09:00 AM
Converting XHTML to Word ML using XSLT debsoft XSLT 1 July 7th, 2004 04:36 AM
XHTML to Word ML using XSLT debsoft XSLT 0 June 26th, 2004 04:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.