为什么我会收到“文件提前结束”消息执行 XSLT 转换时出错?

发布于 2024-10-31 12:01:46 字数 4075 浏览 2 评论 0原文

获取第 #-1 行;第#-1栏; xslt 转换时出现文件过早结束错误

XSL:

<xsl:template match="/">
    <html>
        <head>
            <title>Real's HowTo</title>
        </head>
        <body>
            <table border="1">
                <tr>
                    <th>Titleamit</th>
                    <th>Link</th>
                </tr>
                <xsl:for-each select="mx:feed/mx:entry">
                    <tr>
                        <td>
                            <xsl:value-of select="mx:title" />
                        </td>
                        <td>
                            <xsl:value-of select="mx:published" />
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:clearspace="http://www.jivesoftware.com/xmlns/clearspace/rss"
xmlns:dc="http://purl.org/dc/elements/1.1/">
    <title>
        Sprint Community: Space Polls - Android
    </title>
    <link rel="alternate" href="http://community.sprint.com/baw/poll.jspa?containerType=14&container=2130" />
    <subtitle>
        List of polls
    </subtitle>
    <id>
        http://community.sprint.com/baw/poll.jspa?containerType=14&container=2130
    </id>
    <generator uri="http://jivesoftware.com/products/clearspace/" version="4.5.4.1">
        Jive SBS
    </generator>
    <updated>
        2010-02-08T17:30:00Z
    </updated>
    <dc:date>
        2010-02-08T17:30:00Z
    </dc:date>
    <dc:language>
        en
    </dc:language>
    <entry>
        <title>
            What's Valuable
        </title>
        <link rel="alternate" href="http://community.sprint.com/baw/poll.jspa?poll=1062" />
        <author>
            <name>
                wengla02
            </name>
            <uri>
                /people/wengla02
            </uri>
            <email>
                [email protected]
            </email>
        </author>
        <updated>
            2010-02-08T17:30:00Z
        </updated>
        <published>
            2010-02-08T17:30:00Z
        </published>
        <summary type="html" />
        <dc:date>
            2010-02-08T17:30:00Z
        </dc:date>
        <clearspace:dateToText>
            1 year, 2 months ago
        </clearspace:dateToText>
        <clearspace:objectType>
            0
        </clearspace:objectType>
    </entry>
</feed>

Java 代码:

// load xslt fromxsltFilePath
url = getClass().getClassLoader().getResource("com/sprint/mysprint/phoneMedia/myPhoneAndMedia/xsl/announcementFeed.xsl");

BufferedInputStream bis3= new BufferedInputStream(url.openStream());

if (isLoggingDebug()) {
    BufferedInputStream bis1= new BufferedInputStream(url.openStream());
    byte[] buffer1 = new byte[1024];
    int bytesRead1 = 0;

    while ((bytesRead1 = bis1.read(buffer1)) != -1) {
        String chunk1 = new String(buffer1, 0, bytesRead1);
        logDebug(" " + chunk1);
    }
}

TransformerFactory transformerFactory = TransformerFactory.newInstance();

// create xslt Template
Templates template = transformerFactory.newTemplates(
  new StreamSource(new DataInputStream(url.openStream())));

// Create Transformer 
Transformer transformer = template.newTransformer();
transformer.transform(
  new StreamSource(bis), new StreamResult(xhtmlContent));

Getting Line #-1; Column #-1; Premature end of file Error while xslt transform

XSL :

<xsl:template match="/">
    <html>
        <head>
            <title>Real's HowTo</title>
        </head>
        <body>
            <table border="1">
                <tr>
                    <th>Titleamit</th>
                    <th>Link</th>
                </tr>
                <xsl:for-each select="mx:feed/mx:entry">
                    <tr>
                        <td>
                            <xsl:value-of select="mx:title" />
                        </td>
                        <td>
                            <xsl:value-of select="mx:published" />
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:clearspace="http://www.jivesoftware.com/xmlns/clearspace/rss"
xmlns:dc="http://purl.org/dc/elements/1.1/">
    <title>
        Sprint Community: Space Polls - Android
    </title>
    <link rel="alternate" href="http://community.sprint.com/baw/poll.jspa?containerType=14&container=2130" />
    <subtitle>
        List of polls
    </subtitle>
    <id>
        http://community.sprint.com/baw/poll.jspa?containerType=14&container=2130
    </id>
    <generator uri="http://jivesoftware.com/products/clearspace/" version="4.5.4.1">
        Jive SBS
    </generator>
    <updated>
        2010-02-08T17:30:00Z
    </updated>
    <dc:date>
        2010-02-08T17:30:00Z
    </dc:date>
    <dc:language>
        en
    </dc:language>
    <entry>
        <title>
            What's Valuable
        </title>
        <link rel="alternate" href="http://community.sprint.com/baw/poll.jspa?poll=1062" />
        <author>
            <name>
                wengla02
            </name>
            <uri>
                /people/wengla02
            </uri>
            <email>
                [email protected]
            </email>
        </author>
        <updated>
            2010-02-08T17:30:00Z
        </updated>
        <published>
            2010-02-08T17:30:00Z
        </published>
        <summary type="html" />
        <dc:date>
            2010-02-08T17:30:00Z
        </dc:date>
        <clearspace:dateToText>
            1 year, 2 months ago
        </clearspace:dateToText>
        <clearspace:objectType>
            0
        </clearspace:objectType>
    </entry>
</feed>

Java code:

// load xslt fromxsltFilePath
url = getClass().getClassLoader().getResource("com/sprint/mysprint/phoneMedia/myPhoneAndMedia/xsl/announcementFeed.xsl");

BufferedInputStream bis3= new BufferedInputStream(url.openStream());

if (isLoggingDebug()) {
    BufferedInputStream bis1= new BufferedInputStream(url.openStream());
    byte[] buffer1 = new byte[1024];
    int bytesRead1 = 0;

    while ((bytesRead1 = bis1.read(buffer1)) != -1) {
        String chunk1 = new String(buffer1, 0, bytesRead1);
        logDebug(" " + chunk1);
    }
}

TransformerFactory transformerFactory = TransformerFactory.newInstance();

// create xslt Template
Templates template = transformerFactory.newTemplates(
  new StreamSource(new DataInputStream(url.openStream())));

// Create Transformer 
Transformer transformer = template.newTransformer();
transformer.transform(
  new StreamSource(bis), new StreamResult(xhtmlContent));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-11-07 12:01:46

& 和符号在 xml 中不会转义。您应该将它们更改为 & 以便能够解析 xml 数据,否则它将无法工作。
在解析、转换、包装等之后,您可以通过将这些字符改回来来更新结果。

The & ampersand signs are not escaped in your xml. You should change them to & in order to be able to parse the xml data, otherwise it won't work.
After parsing and transforming, wrapping, whatever, you can update the results by changing these characters back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文