【问题标题】:Use xml comment to generate formatted html output使用 xml 注释生成格式化的 html 输出
【发布时间】:2011-05-18 14:12:13
【问题描述】:

我正在尝试从 xml 文件中的 cmets 生成格式良好的 html 文档。目前我有一个 xml 文件,用于生成 xml 表的 html 列表。为了让我添加有关表的 cmets,我手动将 cmets 添加到输出 html 文件中。

如果可能的话,我希望将 html 代码作为注释放在 xml 文件中,并让 xslt 使用该注释来创建格式正确的文档。

这里是 xml 文件中被注释的部分。这里有 html 换行语法,我希望 xslt 将其读取为 html。我认为必须有更好的方法来使用原始 xml 来创建它,但是,我不希望在 xml 文件中读取 cmets,所以不希望它作为表条目。

    <table name="ers_benchmark_defn" xmlns="">
    <!-- This table contains mapping between hierarchy nodes and their respective benchmarks.  The columns should be populated as follows:<br>
             <ul>
             <li>HIERARCHY_NODE<br>
             This column contains the name of the hierarchy node in the ERS Risk Hierarchy.</ul>
             <ul>
             <li>BENCHMARK<br>
             The column can be populated with either;<br>
             a Calypso portfolio name,<br>
             an ERS Risk Hierarchy name, or<br>
             an ERS Risk Hierarchy node name.<br><br>
             In the latter case, the column should be populated with the hierarchy node name and the hierarchy to which it belongs, separated by a percentage symbol, %.</ul>
             <ul>
             <li>BENCHMARK_TYPE<br>
             If the value in the benchmark column is an ERS hierarchy or hierarchy node name, this column should be populated with the value HIERARCHY.  Otherwise, when using a Calypso portfolio name, it should not be populated.</ul>
             <ul>
             <li>SCALING_FACTOR<br>
             This column should be populated with the scaling factor by which the benchmark results should be multiplied.  To use MTM scaling, leave this column unpopulated.</ul>
             See the ERS 10.2 Release notes for further information.<br><br> -->
    <column name="hierarchy_node" nullable="false" type="string" scale="255"/>
    <column name="benchmark" nullable="false" type="string" scale="255"/>
    <column name="benchmark_type" nullable="true" type="string" scale="32"/>
    <column name="scaling_factor" nullable="true" type="float"/>

这是我为使用注释而制作的 xsl 文件的一部分,但它不解释 html。

<tr class="info" width="100%">
        <td colspan="4"><xsl:value-of select="comment()"/></td>
    </tr>

手动格式化后所需的输出如下所示:

  <p>
     <table border="1" cellpadding="2" cellspacing="0">
        <tr class="title">
           <th colspan="4"><a name="ers_benchmark_defn"></a>ers_benchmark_defn
           </th>
        </tr>
        <tr class="info" width=100%>
             <th colspan=4 align=left>This table contains mapping between hierarchy nodes and their respective benchmarks.  The columns should be populated as follows:<br>
             <ul>
             <li>HIERARCHY_NODE<br>
             This column contains the name of the hierarchy node in the ERS Risk Hierarchy.</ul>
             <ul>
             <li>BENCHMARK<br>
             The column can be populated with either;<br>
             a Calypso portfolio name,<br>
             an ERS Risk Hierarchy name, or<br>
             an ERS Risk Hierarchy node name.<br><br>
             In the latter case, the column should be populated with the hierarchy node name and the hierarchy to which it belongs, separated by a percentage symbol, %.</ul>
             <ul>
             <li>BENCHMARK_TYPE<br>
             If the value in the benchmark column is an ERS hierarchy or hierarchy node name, this column should be populated with the value HIERARCHY.  Otherwise, when using a Calypso portfolio name, it should not be populated.</ul>
             <ul>
             <li>SCALING_FACTOR<br>
             This column should be populated with the scaling factor by which the benchmark results should be multiplied.  To use MTM scaling, leave this column unpopulated.</ul>
             See the ERS 10.2 Release notes for further information.<br><br></th>
        </tr>
        <tr class="header">
           <th>column name</th>
           <th>nullable</th>
           <th>type</th>

这些是完整代码的摘录,但我认为这应该足够有人帮助我了。

谢谢!

【问题讨论】:

    标签: html xml xls


    【解决方案1】:

    哦,我猜你说的是 XML 文件中的 &lt;!CDATA[...]]&gt;,请查看w3schools 页面。

    在您的示例中,我创建了一个 comments 元素(使用您最喜欢的名称),并引入 disable-output-escaping 属性,以避免 HTML scaping。

    会是这样的:

    <table name="ers_benchmark_defn" xmlns="">
        <comments>
            <![CDATA[This table contains mapping between hierarchy nodes and their respective benchmarks.  The columns should be populated as follows:<br>
             <ul>
             <li>HIERARCHY_NODE<br>
             This column contains the name of the hierarchy node in the ERS Risk Hierarchy.</ul>
             <ul>
             <li>BENCHMARK<br>
             The column can be populated with either;<br>
             a Calypso portfolio name,<br>
             an ERS Risk Hierarchy name, or<br>
             an ERS Risk Hierarchy node name.<br><br>
             In the latter case, the column should be populated with the hierarchy node name and the hierarchy to which it belongs, separated by a percentage symbol, %.</ul>
             <ul>
             <li>BENCHMARK_TYPE<br>
             If the value in the benchmark column is an ERS hierarchy or hierarchy node name, this column should be populated with the value HIERARCHY.  Otherwise, when using a Calypso portfolio name, it should not be populated.</ul>
             <ul>
             <li>SCALING_FACTOR<br>
             This column should be populated with the scaling factor by which the benchmark results should be multiplied.  To use MTM scaling, leave this column unpopulated.</ul>
             See the ERS 10.2 Release notes for further information.<br><br>]]>
    </comments>
    <column name="hierarchy_node" nullable="false" type="string" scale="255"/>
    <column name="benchmark" nullable="false" type="string" scale="255"/>
    <column name="benchmark_type" nullable="true" type="string" scale="32"/>
    <column name="scaling_factor" nullable="true" type="float"/>
    

    在您的 XSL 文件中:

    <tr class="info" width="100%">
        <td colspan="4"><xsl:value-of select="comments" disable-output-escaping="yes" /></td>
    </tr>
    

    Ps:如果您的输出仍然有问题,请尝试在您的 XML 版本声明之后插入 XSL 的 xsl:outputline(更多信息 here):

    <xsl:output method="html" omit-xml-declaration="no" indent="yes" />
    

    【讨论】:

    • 太棒了!一个非常好的答案!如果其他人读到这个 ​​disable-output-escaping 是正确的语法,但它不应该花费太多的谷歌搜索来弄清楚!
    • ups,你说得对,我最后写得很糟糕,我要改了!
    猜你喜欢
    • 2014-04-03
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    相关资源
    最近更新 更多