【问题标题】:XSL Template outputting massive chunk of text, rather than HTML. But only on one sectionXSL 模板输出大量文本,而不是 HTML。但只有一节
【发布时间】:2010-03-18 10:49:13
【问题描述】:

我在使用 XSL 模板时遇到了一些奇怪的情况。大部分输出都很好,但是某个 for-each 循环给我带来了问题。

这是 XML:

<area>
<feature type="Hall">
    <Heading><![CDATA[Hall]]></Heading> 
    <Para><![CDATA[Communal gardens, pathway leading to PVCu double glazed communal front door to]]></Para> 
</feature>
<feature type="Entrance Hall">
    <Heading><![CDATA[Communal Entrance Hall]]></Heading> 
    <Para><![CDATA[Plain ceiling, centre light fitting, fire door through to inner hallway, wood and glazed panelled front door to]]></Para> 
</feature>
<feature type="Inner Hall">
    <Heading><![CDATA[Inner Hall]]></Heading> 
    <Para><![CDATA[Plain ceiling with pendant light fitting and covings, security telephone, airing cupboard housing gas boiler serving domestic hot water and central heating, telephone point, storage cupboard housing gas and electric meters, wooden panelled doors off to all rooms.]]></Para> 
</feature>
<feature type="Lounge (Reception)" width="3.05" length="4.57" units="metre">
    <Heading><![CDATA[Lounge (Reception)]]></Heading> 
    <Para><![CDATA[15' 6" x 10' 7" (4.72m x 3.23m) Window to the side and rear elevation, papered ceiling with pendant light fitting and covings, two double panelled radiators, power points, wall mounted security entry phone, TV aerial point.]]></Para> 
</feature>
<feature type="Kitchen" width="3.05" length="3.66" units="metre">
    <Heading><![CDATA[Kitchen]]></Heading> 
    <Para><![CDATA[12'  x 10'  (3.66m x 3.05m) Double glazed window to the rear elevation, textured ceiling with strip lighting, range of base and wall units in Beech with brushed aluminium handles, co-ordinated working surfaces with inset stainless steel sink with mixer taps over, co-ordinated tiled splashbacks, gas and electric cooker points, large storage cupboard with shelving, power points.]]></Para> 
</feature>
<feature type="Entrance Porch">
    <Heading><![CDATA[Balcony]]></Heading> 
    <Para><![CDATA[Views across the communal South facing garden, wrought iron balustrade.]]></Para> 
</feature>
<feature type="Bedroom" width="3.35" length="3.96" units="metre">
    <Heading><![CDATA[Bedroom One]]></Heading> 
    <Para><![CDATA[13' 6" x 11' 5" (4.11m x 3.48m) Double glazed windows to the front and side elevations, papered ceiling with pendant light fittings and covings, single panelled radiator, power points, telephone point, security entry phone.]]></Para> 
</feature>
<feature type="Bedroom" width="3.05" length="3.35" units="metre">
    <Heading><![CDATA[Bedroom Two]]></Heading> 
    <Para><![CDATA[11' 4" x 10' 1" (3.45m x 3.07m) Double glazed window to the front elevation, plain ceiling with centre light fitting and covings, power points.]]></Para> 
</feature>
<feature type="bathroom">
    <Heading><![CDATA[Bathroom]]></Heading> 
    <Para><![CDATA[Obscure double glazed window to the rear elevation, textured ceiling with centre light fitting and extractor fan, suite in white comprising of low level WC, wall mounted wash hand basin and walk in shower housing 'Triton T80' electric shower, co-ordinated tiled splashbacks.]]></Para> 
</feature>
</area>

这是我的模板中处理它的部分:

<xsl:for-each select="area">
    <li>
        <xsl:for-each select="feature">
            <li>
                <h5>
                    <xsl:value-of select="Heading"/>
                </h5>
                <xsl:value-of select="Para"/>
            </li>
        </xsl:for-each>
    </li>
</xsl:for-each>

这是输出:

Hall Communal gardens, pathway leading to PVCu double glazed communal front door to Communal Entrance Hall Plain ceiling, centre light fitting, fire door through to inner hallway, wood and glazed panelled front door to Inner Hall Plain ceiling with pendant light fitting and covings, security telephone, airing cupboard housing gas boiler serving domestic hot water and central heating, telephone point, storage cupboard housing gas and electric meters, wooden panelled doors off to all rooms. Lounge (Reception) 15' 6" x 10' 7" (4.72m x 3.23m) Window to the side and rear elevation, papered ceiling with pendant light fitting and covings, two double panelled radiators, power points, wall mounted security entry phone, TV aerial point. Kitchen 12' x 10' (3.66m x 3.05m) Double glazed window to the rear elevation, textured ceiling with strip lighting, range of base and wall units in Beech with brushed aluminium handles, co-ordinated working surfaces with inset stainless steel sink with mixer taps over, co-ordinated tiled splashbacks, gas and electric cooker points, large storage cupboard with shelving, power points. Balcony Views across the communal South facing garden, wrought iron balustrade. Bedroom One 13' 6" x 11' 5" (4.11m x 3.48m) Double glazed windows to the front and side elevations, papered ceiling with pendant light fittings and covings, single panelled radiator, power points, telephone point, security entry phone. Bedroom Two 11' 4" x 10' 1" (3.45m x 3.07m) Double glazed window to the front elevation, plain ceiling with centre light fitting and covings, power points. Bathroom Obscure double glazed window to the rear elevation, textured ceiling with centre light fitting and extractor fan, suite in white comprising of low level WC, wall mounted wash hand basin and walk in shower housing 'Triton T80' electric shower, co-ordinated tiled splashbacks.

作为参考,这里是整个 XSLT:http://pastie.org/private/eq4gjvqoc1amg9ynyf6wzg

编辑:这是完整的 XML:http://pastie.org/private/upu3g9rstw8pilemtaq

其余的输出都很好 - 我在上面的部分中遗漏了什么?

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    你不应该到处使用xsl:for-each,而是模板:

        <xsl:template match="area">
            <li>
                <xsl:apply-templates />
            </li>
        </xsl:template>
    
        <xsl:template match="feature">
            <li>
                <h5>
                    <xsl:value-of select="Heading"/>
                </h5>
                <xsl:value-of select="Para"/>
            </li>
        </xsl:template>
    

    我猜你的匹配规则实际上都没有被调用,所以默认模板被调用(这个模板只是输出所有文本,正如你所描述的)。

    阅读Understanding How the Default Templates Work 了解更多信息。

    更新

    Stephen Denne 发现问题的根本原因是您的 area 元素被包裹在 text 元素中,因此从未被选中,导致调用默认模板,输出文本节点.

    【讨论】:

    • 为什么所有的标签内容都在CDATA标签中?这似乎有点矫枉过正。
    • @scunliffe,真希望我知道,这不是我的 XML - 它是来自客户系统的提要。
    • @oded,感谢您的建议 - 自从我做 XSL 以来已经有一段时间了。将其切换到模板后,我仍然遇到同样的问题 - 可能与 CDATA 标签有关吗?
    • 绝对推荐模板而不是 for-each 循环。
    • +1 我会专门使用&lt;xsl:apply-templates select="feature" /&gt;,因为这样做可以大大消除样式表的歧义并减少输出中不需要的空白。
    【解决方案2】:

    您的区域元素在您的文本元素内。

    【讨论】:

      【解决方案3】:

      您的样式表有一个与 property 匹配的模板,但您的 XML 中没有可匹配的 property 元素。

      因此,默认模板规则是匹配的,它发出文本节点。

      如果您根据属性匹配更改模板:

      <xsl:template match="property">
      

      到根节点上匹配:

      <xsl:template match="/">
      

      你得到以下输出,我认为这是你想要的:

      <ul id="property" xmlns:php="http://php.net/xsl">
      <li>
      <ul class="pictures"></ul>
      </li>
      <li>
      <address></address>
      </li>
      <li>
                      Price: </li>
      <li>
      <li>
      <h5>Hall</h5>Communal gardens, pathway leading to PVCu double glazed communal front door to</li>
      <li>
      <h5>Communal Entrance Hall</h5>Plain ceiling, centre light fitting, fire door through to inner hallway, wood and glazed panelled front door to</li>
      <li>
      <h5>Inner Hall</h5>Plain ceiling with pendant light fitting and covings, security telephone, airing cupboard housing gas boiler serving domestic hot water and central heating, telephone point, storage cupboard housing gas and electric meters, wooden panelled doors off to all rooms.</li>
      <li>
      <h5>Lounge (Reception)</h5>15' 6" x 10' 7" (4.72m x 3.23m) Window to the side and rear elevation, papered ceiling with pendant light fitting and covings, two double panelled radiators, power points, wall mounted security entry phone, TV aerial point.</li>
      <li>
      <h5>Kitchen</h5>12'  x 10'  (3.66m x 3.05m) Double glazed window to the rear elevation, textured ceiling with strip lighting, range of base and wall units in Beech with brushed aluminium handles, co-ordinated working surfaces with inset stainless steel sink with mixer taps over, co-ordinated tiled splashbacks, gas and electric cooker points, large storage cupboard with shelving, power points.</li>
      <li>
      <h5>Balcony</h5>Views across the communal South facing garden, wrought iron balustrade.</li>
      <li>
      <h5>Bedroom One</h5>13' 6" x 11' 5" (4.11m x 3.48m) Double glazed windows to the front and side elevations, papered ceiling with pendant light fittings and covings, single panelled radiator, power points, telephone point, security entry phone.</li>
      <li>
      <h5>Bedroom Two</h5>11' 4" x 10' 1" (3.45m x 3.07m) Double glazed window to the front elevation, plain ceiling with centre light fitting and covings, power points.</li>
      <li>
      <h5>Bathroom</h5>Obscure double glazed window to the rear elevation, textured ceiling with centre light fitting and extractor fan, suite in white comprising of low level WC, wall mounted wash hand basin and walk in shower housing 'Triton T80' electric shower, co-ordinated tiled splashbacks.</li>
      </li>
      </ul>
      

      【讨论】:

      • 啊,我很遗憾没有发布完整的 XML 文件。我已将其添加到问题的末尾 - 非常抱歉有任何混淆。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-21
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      相关资源
      最近更新 更多