结合xml和XSL

时间:2016-08-13 19:18:08

标签: xml internet-explorer xslt

这是先前问题Decoding base64 image data using xslt

中的后续问题

我仍然希望向最终用户发送一个文件,xml并使用样式表将其转换为漂亮的html 但是我想向最终用户发送单个文件。一个独立工作的XML文件(不调用任何服务器或任何其他依赖项)

嵌入了样式表的单个XML文件,但我不知道是否/如何执行此操作 考虑该问题的xml

<?xml-stylesheet type="text/xsl" href="test2016080901.xsl"?>
<catalogue>
    <item>
        <item_id>1234</item_id>
        <item_desc>hi-fi sanio</item_desc>
        <price>12.50</price>
        <image>iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZlAAA</image>
    </item>
    <item>
        <item_id>4614</item_id>
        <item_desc>lace work</item_desc>
        <price>1.50</price>
        <image>QN0lEQVR4nO2dCXQTxxnHl0LT5jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEw</image>
    </item>
    <item>
        <item_id>614</item_id>
        <item_desc>bicycle</item_desc>
        <price>150</price>
        <image>jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEwlEQVR4nO2dCXQTxxnHl0L</image>
    </item>
</catalogue>

和转换xsl

<xsl:output method="html" doctype-public="about:legacy-compat" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

<xsl:template match="/">
  <html>
    <head>
      <title>data URI test</title>
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:template match="catalogue">
    <table>
        <thead>
            <xsl:apply-templates select="item[1]" mode="header"/>
        </thead>
        <tbody>
            <xsl:apply-templates/>
        </tbody>
    </table>
</xsl:template>

<xsl:template match="item" mode="header">
    <tr>
        <xsl:apply-templates mode="header"/>
    </tr>
</xsl:template>

<xsl:template match="item/*" mode="header">
    <th>
        <xsl:value-of select="local-name()"/>
    </th>
</xsl:template>

<xsl:template match="item">
    <tr>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

<xsl:template match="item/*">
    <td>
        <xsl:value-of select="."/>
    </td>
</xsl:template>

<xsl:template match="item/image">
    <td>
        <img src="data:image/png;base64,{.}"/>
    </td>
</xsl:template>

如何将两者合并,以便我可以将一个结果文件提供给最终用户?这样他们就可以查看结果输出 没有任何依赖关系(当然,除了他们的Web浏览器,假设IE11是最终用户的首选浏览器)

此致

1 个答案:

答案 0 :(得分:0)

Firefox和Chrome似乎支持将样式表插入XML并使用例如XML引用它。 <?xml-stylesheet type="text/xsl" href="#id-of-stylesheet-root-element"?>,这是上一个适用于该机制的示例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalogue [
  <!ATTLIST xsl:transform
     id ID #REQUIRED>
]>
<?xml-stylesheet type="text/xsl" href="#style"?>
<catalogue>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" id="style">

    <xsl:output method="html" doctype-public="about:legacy-compat" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="xsl:transform"/>

    <xsl:template match="/">
      <html>
        <head>
          <title>data URI test</title>
        </head>
        <body>
          <xsl:apply-templates/>
        </body>
      </html>
    </xsl:template>

    <xsl:template match="catalogue">
        <table>
            <thead>
                <xsl:apply-templates select="item[1]" mode="header"/>
            </thead>
            <tbody>
                <xsl:apply-templates/>
            </tbody>
        </table>
    </xsl:template>

    <xsl:template match="item" mode="header">
        <tr>
            <xsl:apply-templates mode="header"/>
        </tr>
    </xsl:template>

    <xsl:template match="item/*" mode="header">
        <th>
            <xsl:value-of select="local-name()"/>
        </th>
    </xsl:template>

    <xsl:template match="item">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

    <xsl:template match="item/*">
        <td>
            <xsl:value-of select="."/>
        </td>
    </xsl:template>

    <xsl:template match="item/image">
        <td>
            <img src="data:image/png;base64,{.}"/>
        </td>
    </xsl:template>

</xsl:transform>

    <item>
        <item_id>1007</item_id>
        <item_desc>test</item_desc>
        <price>3.14</price>
        <image>iVBORw0KGgoAA
AANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0l
EQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6
P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC</image>
    </item>
    <item>
        <item_id>1234</item_id>
        <item_desc>hi-fi sanio</item_desc>
        <price>12.50</price>
        <image>iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZlAAA</image>
    </item>
    <item>
        <item_id>4614</item_id>
        <item_desc>lace work</item_desc>
        <price>1.50</price>
        <image>QN0lEQVR4nO2dCXQTxxnHl0LT5jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEw</image>
    </item>
    <item>
        <item_id>614</item_id>
        <item_desc>bicycle</item_desc>
        <price>150</price>
        <image>jVteHlN+5q+JCKBJITLmHIfKzBHHCCYBAiEwlEQVR4nO2dCXQTxxnHl0L</image>
    </item>
</catalogue>

然而,Microsoft IE和Edge在XSLT转换期间报告错误,并且只是呈现所有文本节点,因此对于那些浏览器而言,这种方法似乎没有用处。在服务器上创建HTML并发送出来似乎更安全。

对于使用嵌入式XSLT以编程方式创建XML,使用Saxon 9的商业版本(即PE或EE),可以使用以下XSLT 2.0样式表,该样式表将样式表的URI嵌入为参数{{1 }}:

sheet-uri