Search code examples
javascriptxsltxslt-1.0href

XSLT - Open HREF Automatically


I am working with XSLT 1.0 to step through a large list of items and generate links that are used to access information regarding these items.

I have been able to successfully create HREFs for each item that, when clicked, open a new window and take me to the correct link and displaying the correct information.

While this is a big step in the right direction, there is one thing that I have not been able to figure out: How can I have the links open automatically when the XSLT is processed?

Here is an example XML document:

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

<document>
    <example>
        <number>1</number>
    </example>
    <example>
        <number>2</number>
    </example>
    <example>
        <number>3</number>
    </example>
</document>

Here is my current XSL code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:oldxsl="http://www.w3.org/TR/WD-xsl"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:local="#local-functions"
    xmlns:date="http://exslt.org/dates-and-times">      

<xsl:template match="/">

    <html>

        <xsl:for-each select="//example">

            <xsl:element name="a">
                <xsl:attribute name="href">
                    <xsl:text>http://lmgtfy</xsl:text><xsl:text>.com/?q=Example+</xsl:text><xsl:value-of select="./number"/>
                </xsl:attribute>
                <xsl:attribute name="target">_blank</xsl:attribute>
                    <xsl:value-of select="./number"/><br/>
            </xsl:element>

        </xsl:for-each>

    </html>

</xsl:template>

</xsl:stylesheet>

NOTE - The link in the example above is not meant to be offensive in any way, it is simply a short link that allows me to demonstrate what is actually taking place. (And yes, I have tried to search for a solution for this issue.)

Again, this is working as I would expect it to. When I click on any of the resulting links, I am taken where I should be.

All I would like to able to do now is open each link automatically once the page was loaded. (In this case, I should see 3 new windows appear without having to click any of the links.) If this is possible, please let me know how to accomplish this.

Thanks!

UPDATE 1 - If this can be solved, would it also be possible to open a print dialog for each link? If so, how?

UPDATE 2 - I did try to use Java to solve this, but I was unsuccessful. (Though I must be honest, I'm not very familiar with Java, let alone using it within an XSLT stylesheet.) Below is what I attempted, which is a modification of the coding above. It generated a line of text that was underlined like a link, but when clicked on, I received an error.

<xsl:element name="a">
    <xsl:attribute name="href">
        <xsl:text>#</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="onload"><xsl:text>javascript:window.open('http://lmgtfy</xsl:text><xsl:text>.com/?q=Example+</xsl:text><xsl:value-of select="./number"/><xsl:text>','displayWindow','menubar=no,toolbar=no,resizable=yes)</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="target">_blank</xsl:attribute>
        <xsl:value-of select="./number"/><br/>
</xsl:element>

UPDATE 3 - I was able to get Java to open a new a link in a new window, however this link is static. It does not vary with each item. Also, it only opens one window, even though there are three processes. Here's the code I have:

<script type="text/javascript">
    function load()
    {
    window.open("http://www.google.com");
    }
</script>

<body onload="load()">
    <xsl:element name="a">
        <xsl:attribute name="href">
            <xsl:text>#</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onload"><xsl:text>javascript:window.open('http://lmgtfy</xsl:text><xsl:text>.com/?q=Example+</xsl:text><xsl:value-of select="./number"/><xsl:text>','displayWindow','menubar=no,toolbar=no,resizable=yes)</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="target">_blank</xsl:attribute>
            <xsl:value-of select="./number"/><br/>
    </xsl:element>
</body>

UPDATE 4 - I am making progress! With the following code, I am able to get a new window to open. The only problem I have now is that only one window opens, when I should be seeing 3. (The window that is opening is for the last/final link. In this case, "Example 3". That leads me to believe that the script itself is working and that I now only need to figure out how to call the script every time the xsl:for-each loops back.) Suggestions?

<xsl:for-each select="//example">

<script type="text/javascript">
    function load()
    {
    xsl:text>javascript:window.open('http://lmgtfy</xsl:text><xsl:text>.com/?q=Example+</xsl:text><xsl:value-of select="./number"/><xsl:text>
    }
</script>

<body onload="load()">
    <xsl:element name="a">
        <xsl:attribute name="href">
            <xsl:text>#</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="onload"><xsl:text>javascript:window.open('http://lmgtfy</xsl:text><xsl:text>.com/?q=Example+</xsl:text><xsl:value-of select="./number"/><xsl:text>','displayWindow','menubar=no,toolbar=no,resizable=yes)</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="target">_blank</xsl:attribute>
            <xsl:value-of select="./number"/><br/>
    </xsl:element>
</body>

Solution

  • I was able to get it resolved thanks to the nudge in the right direction.

    The solution is to create a new unique script and function within the xsl:for-each process and then call the function before going to the next loop.

    Here's the working code:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
        xmlns:oldxsl="http://www.w3.org/TR/WD-xsl"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:local="#local-functions"
        xmlns:date="http://exslt.org/dates-and-times"
        xmlns:lookup="lookup" exclude-result-prefixes="lookup">
    
    <xsl:for-each select="//example">
    
    <script type="text/javascript">
        function load()
        {
        xsl:text>javascript:window.open('http://lmgtfy</xsl:text><xsl:text>.com/?q=Example+</xsl:text><xsl:value-of select="./number"/><xsl:text>
        }
    </script>
    
    <xsl:element name="a">
        <xsl:attribute name="href">
            <xsl:text>#</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="target">_blank</xsl:attribute>
            <xsl:value-of select="./number"/><br/>
    </xsl:element>
    
    <script>
        load.call()
    </script>
    
    </xsl:for-each>
    </html>
    </xsl:template>
    </xsl:stylesheet>
    

    Thanks again for all the help!