开发者

Can XSLT populate input fields using returned XML data?

this may well be a very basic question, but I haven't been able to find anything on here or after an hour checking via google. I have a form with a load of inputs on it and a search autocomplete that will call xml data for the selected record. I intend to use an XSLT to transform this XML data and reproduce the completed version of the form for that client. I am new to XSLT's so I have been experimenting with smaller examples so far, however I cannot, even in this smaller set up, get the produced HTML to have it's inputs populated by the data in the XML.

Xalan will produce the HTML so I think both the XML and XSLT are at least vaild. The example code for both is shown below. I am aware that there is redundant code, (on key ups etc) in the XSLT. It is just a remenant from the full form that I will hopefully be getting working once I can sort tis example out.

XML

<?xml version="1.0"?>
<!-- clienttest.xml -->
<?xml-stylesheet type="text/xsl" href="XSLTtest.xsl"?>
<clientdetails>
    <clientcode>
        <CL.CREF>testsubjectcode</CL.CREF>
    </clientcode>
    <clientname>
        <CL.NAME>testsubjectname</CL.NAME>
    </clientname>
    <clientaccount>
        <CL.ACCT>testsubjectaccount</CL.ACCT>
    </clientaccount>
</clientdetails>

XSLT

<?xml version="1.0"?>
<!-- greeting.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" indent="yes开发者_JAVA技巧" omit-xml-declaration="no"
            encoding="UTF-8"
            doctype-public="-//W3C//DTD XHTML 1.1//EN"
            doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>XSLT Trials</title>
            </head>
            <body>
                    <xsl:apply-templates select="clientdetails"/>
            </body>
        </html> 
    </xsl:template>             

    <xsl:template match="clientdetails">
        <form>
            <label for="clientcode">Client code:</label>     
            <input class="medium" name="clientcode" method="post" onkeyup="changed2()" type="text" id="clientcode" placeholder="New client's code">
                <xsl:value-of select="./clientcode/CL.CREF"/>
            </input>
            <br></br>
            <label for="clientname">Client name:</label>
            <input class="medium" name="clientname" method="post" onkeyup="changed()" type="text" id="clientname" placeholder="New client's name"> 
                <xsl:value-of select="./clientname/CL.NAME"/>
            </input>
            <br></br>
            <label for="secondaccountcode">Second account code:</label>
            <input class="medium" id="secondaccountcode" name="secondaccountcode" type="text"> 
                <xsl:value-of select="./clientaccount/CL.ACCT"/>
            </input>
        </form>
    </xsl:template>
</xsl:stylesheet>

If anyone can give me a hand that would be great. I think I'm quite close but I just can't get it to behave atm. Thanks


If I underdstand your question correctly - you need to fill dynamically value attribute of an input element.

For this as value for your attributes you can use active value templates - xPath expression limited with curved brackets ("{" and "}"). Therefore your XSLT "clientdetails" template would be (see value attribute):

<xsl:template match="clientdetails">
    <form>
        <label for="clientcode">Client code:</label>     
        <input class="medium" name="clientcode" method="post" onkeyup="changed2()" type="text" id="clientcode" placeholder="New client's code" value="{./clientcode/CL.CREF}">
        </input>
        <br></br>
        <label for="clientname">Client name:</label>
        <input class="medium" name="clientname" method="post" onkeyup="changed()" type="text" id="clientname" placeholder="New client's name" value="{./clientname/CL.NAME}"> 
        </input>
        <br></br>
        <label for="secondaccountcode">Second account code:</label>
        <input class="medium" id="secondaccountcode" name="secondaccountcode" type="text" value="{./clientaccount/CL.ACCT}"> 
        </input>
    </form>
</xsl:template>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜