开发者

Generating Form Fields from XML Tags

I'm trying to figure out a way to utilize a PHP script that will:

  1. Open an XML document when a link to that document is clicked (from an HTML page).
  2. Scan the XML document for tags.
  3. Create an HTML form with input fields based on the tags.
  4. Post the input data back to the XML within the tags (when form is submitted) and print the XML to HTML.

So, if I had an XML file that went like this:

<profile>
Hello, my name is <name></name>.  I am <age></age> years old.  I live in <place></place>
</profile>

Upon clicking a link to that file, PHP would generate开发者_运维技巧 a form like so:

<form> 
Name:
Age:
Place:
</form>

Then upon completing and submitting the form (let's say the person is Joel, 25, from Boston), the following would be written to the screen:

Hello, my name is Joel. I am 25 years old. I live in Boston.

Any code or points to good tutorials would be appreciated.

THX

E.


You should use XSLT for this..

With browser:

xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<profile>
Hello, my name is <name></name>.  I am <age></age> years old.  I live in <place></place>
</profile>

xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

    <xsl:template match="profile">
        <form>
            <xsl:for-each select="child::*">
                <label>
                    <xsl:value-of select="name()"/>: 
                    <input name="{name()}" type="text" />
                </label>
                <br />
            </xsl:for-each>
        </form>
    </xsl:template>
</xsl:stylesheet>

output:

<form>
    <label>name: <input type="text" name="name"></label><br />
    <label>age: <input type="text" name="age"></label><br />
    <label>place: <input type="text" name="place"></label><br />
</form>

There is an xsl extension for php you can use.


If your placeholders always take the form <tag></tag>, you can search using regular expressions or simple string searching, generate your form fields, then do a string find/replace on the original to get the merged version. That might be easier than doing it through XML parsing methods, because the content between the XML tags will be parsed as content nodes.

For that matter, I would use a placeholder like %Name% or $Name$ instead of an XML tag, because then you can just parse it with simple string matching methods, without interfering with the overall XML structure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜