如何在使用XSLT转换XML时防止致命错误

时间:2014-06-10 11:07:55

标签: java xml xslt transformation

我正在使用以下XSLT,而使用此XSLT转换XML会引发以下错误

<xsl:stylesheet>    
    <xsl:output method="xml" indent="yes" />

    <xsl:key name="k1" match="*" use="."/>
    <!-- <xsl:param name="string-value" select="'STRING_TO_FIND'"/> -->

    <xsl:template match="*">
        <rss>
            <xsl:copy-of select="key('k1', 'STRING_TO_FIND')"/>
        </rss>
    </xsl:template>
</xsl:stylesheet>

ERROR

ERROR:  'Could not compile stylesheet'
FATAL ERROR:  'The string "--" is not permitted within comments.'
           :The string "--" is not permitted within comments.

我替换了以下字符串代替&#39; STRING_TO_FIND&#39;在XSLT中。该字符串可以动态生成。抛出上述错误的示例字符串如下。

                                    <description><![CDATA[                                          <p>LANSING, MI -- An unusual photo and an "off-the-cuff remark" in the Michigan House on Thursday sparked a war of words about the "war on women" that stretched far beyond the walls of the state Capitol.</p> <p>Republican state Reps. Roger Victory of Hudsonville, Pete Pettalia of Presque Isle and Ben Glardon of Owosso were photographed on the House floor holding a series of womens' fashion magazines.</p> <p>"Don't say we don't understand women," Petallia joked, according to a&nbsp;Michigan Public Radio Network&nbsp;reporter, whose tweet quickly spread online and prompted widespread criticism from Democrats.</p> <center> <blockquote class="twitter-tweet" lang="en"> <p><a href="https://twitter.com/MIHouseGOP">@MIHouseGOP</a> members Victory, Pettalia, and Glardon: "Don't say we don't understand women." <a href="https://twitter.com/search?q=%23mileg&amp;src=hash">#mileg</a> <a href="http://t.co/zsifsqrZ1N">pic.twitter.com/zsifsqrZ1N</a></p> &#8212; Jake Neher (@GJNeher) <a href="https://twitter.com/GJNeher/statuses/474599923149578240">June 5, 2014</a></blockquote> <script src="http://platform.twitter.com/widgets.js" async="" charset="utf-8"></script> </center> <p>Petallia later explained his comment -- and provided&nbsp;context -- in a statement.</p> <p>"During a short break on the House floor, Republican Rep. Gail Haines, a leading advocate on women's issues in the Legislature, gave some of us magazines to read and asked to take our photo with them," Petallia said.</p> <p>"A member of the media then walked up behind her and snapped another photo. As part of that lighthearted moment, I made an off-the-cuff remark that has since been taken way out of context."</p> <p>House Democrats responded with a clever&nbsp;tweet of their own.</p> <center> <blockquote class="twitter-tweet" lang="en"> <p>Real Women read bills not fashion mags... <a href="https://twitter.com/GJNeher">@GJNeher</a> <a href="https://twitter.com/RashidaTlaib">@RashidaTlaib</a> <a href="https://twitter.com/RepVickiBarnett">@RepVickiBarnett</a> <a href="https://twitter.com/MIHouseGOP">@MIHouseGOP</a> <a href="https://twitter.com/search?q=%23mileg&amp;src=hash">#mileg</a> <a href="http://t.co/9Tq9fBLm1a">pic.twitter.com/9Tq9fBLm1a</a></p> &#8212; MI House Democrats (@MIHouseDems) <a href="https://twitter.com/MIHouseDems/statuses/474627414514221056">June 5, 2014</a></blockquote> </center> <script src="http://platform.twitter.com/widgets.js" async="" charset="utf-8"></script> <p>It didn't take long for Democrats around the state to weigh in.</p> <p>"The war on women is alive and well in Michigan," said Ingham County Clerk Barb Byrum, a former state representative who was temporarily barred from speaking on the floor two years ago during a debate on a controversial abortion-related bill.</p> <p>"Now three Republican lawmakers today &#8211; all white men &#8211; made an open mockery of women on the House floor by posing with fashion magazines to showcase, in their words, that they understand women's issues."</p> <p>Michigan Democratic Party Chairman Lon Johnson suggested that the Republican lawmakers had turned the "House floor into a frat party" and said they should apologize for their "out of touch Republican agenda."</p> <p>Republican state Rep. Andrea LaFontaine&nbsp;of&nbsp;Columbus Township said she felt that Democrats were blowing the matter out of proportion.</p> <p>"At the end of the Day, I'm so tired of Democrats making victims out of women," she told MLive. "As a successful Republican myself, a young professional who's part of the leadership team in Lansing, this just floors me.</p> <p>"I'm sorry if I seem fired up about it, but at the end of the day, I think I'm going to succeed in my career because of my beliefs, because of my hard work and because of the results that we as Republicans are producing in the state of Michigan."</p> <p><em>Jonathan Oosting is a Capitol reporter for MLive Media Group. <a href="mailto:joosting@mlive.com">Email him</a>, find him on <a href="https://www.facebook.com/jonathan.oosting">Facebook</a> or follow him on <a href="http://twitter.com/jonathanoosting">Twitter</a></em></p>                             <script id="advancenews_mobile_app" type="text/javascript" >                                                                                                            m_blog_type = "text";                   m_blog_id = "8002007";                  m_blog_name = "Lansing News";                   m_blog_url = "http://www.mlive.com/lansing-news/index.ssf/";                    m_blog_newspaper = "";                                                                                                          m_entry_title = "Michigan lawmaker\'s joke leads to war of words over \'war on women\'";                                m_entry_categories = "";                                m_entry_author_name = "Jonathan Oosting | joosting@mlive.com";                          m_entry_author_username = "joosting";                           m_entry_author_public_email = "Unknown";                                                m_entry_basename = "michigan_lawmakers_joke_leads";                     m_entry_link = "http://www.mlive.com/lansing-news/index.ssf/2014/06/michigan_lawmakers_joke_leads.html";  

如何摆脱这个错误?

1 个答案:

答案 0 :(得分:1)

您在样式表中有<!-- <xsl:param name="string-value" select="'STRING_TO_FIND'"/> -->形式的注释,这是一个语法正确的注释。但是,如果您有<!-- <xsl:param name="string-value" select="'--'"/> -->,则会出现语法错误,请参阅http://www.w3.org/TR/xml/#sec-comments,其中包含

  

为了兼容性,绝不能出现字符串“ - ”(双连字符)   在评论中。

我不确定您为什么评论xsl:param然后尝试操纵它,最简单的方法是放弃xsl:param而不是评论它。

如果这个问题是How to get XML element based on given value的后续问题,那么看起来你想找到复杂内容的元素,这些元素不符合我在答案中提出的建议。