如何通过Saxon-JS获得图像大小调整?

问题描述 投票:0回答:1

我有一个以下HTML文件,通过JavaScript测试图像大小调整。

<!DOCTYPE html SYSTEM "about:legacy-compat">
<html>
   <head>
        <title>Image Test</title>
        <style type="text/css">
            img.img-responsive {
                display: block;
                max-width: 100%;
                height: auto;
            }
            img.fig{
                display: block;
                max-width: 10000px;
                height: auto;
            }
      </style>
   </head>
   <body>
        <h2>Image Test</h2>
        <p>Image: width="100%"</p>
        <img class="img-responsive" alt="fig" src="img/fig.jpg"/>
        <script>
            window.addEventListener('load', function() {
                 [].forEach.call(document.getElementsByClassName("img-responsive"),function(elem){
                    elem.addEventListener('click',toggleClassName.bind(null,elem, "fig"));
                });
            })
            /* Toggle specified class name
               elem: DOMElement
               className: class name
            */
            function toggleClassName(elem, className){
                var s = ' ' + className;
                if (elem.className.indexOf(className) === -1){
                    elem.className += s ;
                    return true;
                }else{
                    elem.className = elem.className.replace( s , '' );
                    return false;
                }
            }
        </script>
   </body>
</html>

这很好用。所以现在我想通过Saxon-JS使用XSLT样式表做同样的事情。我的第一个样式表如下:

[手柄click.xsl]

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
    extension-element-prefixes="ixsl"
    exclude-result-prefixes="xs"
    version="3.0">
    <xsl:template match="img[contains(@class,'img-responsive')]" mode="ixsl:onclick">
        <xsl:choose>
            <xsl:when test="contains(@class,'fig')">
                <ixsl:set-style name="class" select="replace(@class,' fig','')"/>
            </xsl:when>
            <xsl:otherwise>
                <ixsl:set-style name="class" select="concat(@class,' fig')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

我已经通过oXygen 19.1编译了这个样式表并生成了handle-click.sef文件。然后我修改了上面的HTML以使用Saxon-JS库。

<!DOCTYPE html SYSTEM "about:legacy-compat">
<html>
   <head>
        <title>Image Test</title>
        <style type="text/css">
            img.img-responsive {
                display: block;
                max-width: 100%;
                height: auto;
            }
            img.fig{
                display: block;
                max-width: 10000px;
                height: auto;
            }
      </style>
   </head>
   <body>
        <h2>Image Test</h2>
        <p>Image: width="100%"</p>
        <img class="img-responsive" alt="fig" src="img/fig.jpg"/>
        <script>
            window.addEventListener('load', function() {
                SaxonJS.transform({
                stylesheetLocation: "handle-click.sef.xml"
                });
            })
        </script>
        <script type="text/javascript" src="js/SaxonJS.min.js"></script>
   </body>
</html>

但是单击图像没有任何反应。这个HTML文件(或样式表)有什么问题?

javascript html5 xslt saxon saxon-js
1个回答
0
投票

我不确定为什么你尝试使用ixsl:set-style来操作元素的类,我认为你应该使用元素的DOM classList属性并使用它的toggle方法。

我也不确定你是否可以单独用样式表调用transform方法。

下面你可以找到我测试过的一个样本(在本地使用Firefox)和Saxon-JS 1.0.2插入一个div(不想使用图像)并调用它的classList toggle方法,提供你想要的CSS类名切换为参数:

<!DOCTYPE HTML>
<html>
   <head>
        <title>Saxon-JS test</title>
        <style type="text/css">
            div.responsive {
                border: 5px solid green;
                display: block;
                max-width: 100%;
                height: auto;
            }
            div.fig{
                border: 10px solid yellow;
            }
      </style>
   </head>
   <body>
        <h2>Saxon-JS test</h2>
        <section id="saxon-target"></section>
        <script>
            window.addEventListener('load', function() {
                SaxonJS.transform({
                stylesheetLocation: "test2017122001.sef.xsl",
                initialTemplate: "Q{http://www.w3.org/1999/XSL/Transform}initial-template"
                });
            })
        </script>
        <script type="text/javascript" src="../../Saxon-JS-1.0.2/SaxonJS.min.js"></script>
   </body>
</html>

XSLT是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT"
    extension-element-prefixes="ixsl"
    exclude-result-prefixes="xs"
    version="3.0">

    <xsl:template name="xsl:initial-template">
      <xsl:result-document href="#saxon-target">
         <div class="responsive">This is a test.</div>
      </xsl:result-document>
    </xsl:template>

    <xsl:template match="div[contains(@class,'responsive')]" mode="ixsl:onclick">
        <xsl:sequence select="ixsl:call(ixsl:get(., 'classList'), 'toggle', ['fig'])[current-date() lt xs:date('2000-01-01')]"/>
    </xsl:template>

</xsl:stylesheet>

单击div时会应用CSS样式更改(例如边框颜色和宽度更改),这样可以帮助您为要操作的classList的img设置类似的代码。

示例现在也在https://martin-honnen.github.io/xslt/2017/test2017122001.html上在线,并在Windows 10上与当前版本的Edge,IE,Chrome和Firefox一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.