XSL模板输出大量文本,而不是HTML。但只有一部分

XSL模板输出大量文本,而不是HTML。但只有一部分,html,xml,xslt,Html,Xml,Xslt,我在使用XSL模板时遇到了一个稍微奇怪的情况。它的大部分输出都很好,但每个循环都有一个特定的输出,这给我带来了问题 以下是XML: <area> <feature type="Hall"> <Heading><![CDATA[Hall]]></Heading> <Para><![CDATA[Communal gardens, pathway leading to PVCu double glazed

我在使用XSL模板时遇到了一个稍微奇怪的情况。它的大部分输出都很好,但每个循环都有一个特定的输出,这给我带来了问题

以下是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>
以下是完整的XSLT供参考:

编辑:下面是完整的XML:


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

您不应该到处使用
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>

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

    阅读了解更多信息

    更新


    正如Stephen Denne所发现的,问题的根本原因是您的
    区域
    元素被包装在
    文本
    元素中,因此从未被选中,导致调用默认模板,输出文本节点。

    您不应该对每个位置都使用
    xsl:

        <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>
    
    
    
  • 我猜您的匹配规则实际上都没有被调用,因此默认模板被调用(正如您所描述的,这个模板只是输出所有文本)

    阅读了解更多信息

    更新


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

    您的样式表有一个与属性匹配的模板,但是XML中没有可供匹配的属性元素

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

    如果从属性上的匹配更改模板:

    <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>
    
    
    
    • 价格:
    • 走廊公共花园,通向PVCu双层玻璃公共前门的通道
    • 公共入口走廊普通天花板、中央照明装置、通往内部走廊的防火门、通往内部走廊的木质和玻璃嵌板前门
    • 内厅普通天花板,带吊灯配件和装饰罩、安全电话、装有家用热水和中央供暖的燃气锅炉的通风柜、电话插座、装有煤气表和电表的储物柜、通往所有房间的木质镶板门
    • 休息室(接待处)侧立面和后立面的15英尺6英寸x 10英尺7英寸(4.72米x 3.23米)窗户、带吊灯配件和装饰罩的纸制天花板、两个双面板散热器、电源插座、壁挂式安全入口电话、电视天线插座
    • 厨房后立面12'x 10'(3.66m x 3.05m)双层玻璃窗,带条形照明的纹理天花板,一系列山毛榉木底座和墙壁单元,带拉丝铝把手,带插入式不锈钢水槽的协调工作面,带搅拌器水龙头,协调的瓷砖防溅板,煤气灶和电灶点,大型储物柜,带搁板、电源插座
    • 阳台对面的公共南向花园,熟铁栏杆
    • 卧室1扇13英尺6英寸x 11英尺5英寸(4.11米x 3.48米)双层玻璃窗,正面和侧面立面,纸制天花板,带吊灯配件和装饰罩,单面板散热器,电源插座,电话插座,安全入口电话
    • 卧室两扇11英尺4英寸x 10英尺1英寸(3.45米x 3.07米)的双层玻璃窗,位于前立面,平顶,带中央灯具和饰边,电源插座
    • 浴室后立面为不透明的双层玻璃窗,带中央照明装置和抽风机的纹理天花板,白色套房,包括低层卫生间、壁挂式洗手盆和步入式淋浴房“Triton T80”电动淋浴器、协调瓷砖防溅板

    • 样式表有一个与属性匹配的模板,但XML中没有可供匹配的属性元素

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

      如果从属性上的匹配更改模板:

      <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>
      
      
      
      • 价格:
      • 走廊公共花园,通向PVCu双层玻璃公共前门的通道
      • 公共入口走廊普通天花板、中央照明装置、通往内部走廊的防火门、通往内部走廊的木质和玻璃嵌板前门
      • 内厅普通天花板,带吊灯配件和装饰罩、安全电话、装有家用热水和中央供暖的燃气锅炉的通风柜、电话插座、装有煤气表和电表的储物柜、通往所有房间的木质镶板门
      • 休息室(接待处)侧立面和后立面的15英尺6英寸x 10英尺7英寸(4.72米x 3.23米)窗户、带吊灯配件和装饰罩的纸制天花板、两个双面板散热器、电源插座、壁挂式安全入口电话、电视天线插座
      • 厨房后立面12'x 10'(3.66m x 3.05m)双层玻璃窗,带条形照明的纹理天花板,一系列山毛榉木底座和墙壁单元,带拉丝铝把手,带嵌入式不锈钢水槽的协调工作面,带搅拌龙头