xml <![CData[]]> 需要浏览器解码

xml <![CData[]]> need browser to decode it

我的 if 语句有问题 浏览器没有解码 那就是它没有换行,但它只显示 在浏览器上 我也用过 CData 但它不起作用 有人可以帮我吗

<table border="1">
<tr>

<xsl:for-each select="/catalogue/item">

    <td>
    <img src="{img/.}" alt="{img/@alt}" class="imgs-catalogue" title="click for description"/><br/>
    Id: <xsl:value-of select="id/."/><br/>
    Name: <xsl:value-of select="name/."/><br/>
    Type: <xsl:value-of select="type/."/><br/>
    Price: <xsl:value-of select="price/text()"/><br/><br/>
    </td>       

    <xsl:if test="(position() mod 4)=0">
        </tr><tr>
    </xsl:if>

</xsl:for-each>
</tr>
</table>

这是完整的xml

 <?xml version="1.0" encoding="utf-8" ?>
 <?xml-stylesheet  type="text/xsl" href="catalogue.xsl" ?>
 <catalogue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNameSpaceSchemaLocation="catalogue.xsd">

<!-- Item 1 -->
<item>
    <id>45</id>
    <name>Ginkgo Plus</name>
    <img alt="Ginkgo Plus">images/catalogueImg/ginkgo.jpg</img>
    <price>MUR 575</price>
    <type>Health</type>
    <description>
        A mixture of 3 chineese plant including ginkgo biloba and reishi.
        Many traditional medicines identifies that the leaves of the ginkgo biloba 
        have a positive effect on the circulatory system. The reishi is a champion
        used in chineese and japaneese traditional medicines to protect against 
        heart problems.
    </description>
</item>

<!-- Item 2 -->
<item>
    <id>46</id>
    <name>Hand Sanitizer</name>
    <img alt="Hand Sanitizer">images/catalogueImg/hand.jpg</img>
    <price>MUR 350</price>
    <type>Beauty</type>
    <description>
        Helps to clean and desinfect perfectly your hands
        wherever you are. This product is enriched in 
        Aloe Vera and honey which perfumes your hand without
        drying them.
    </description>
</item>

<!-- Item 3 -->
<item>
    <id>87</id>
    <name>Aloe Vera Gel</name>
    <img alt="Aloe Vera Gel">images/catalogueImg/aloe.jpg</img>
    <price>MUR 1300</price>
    <type>Health</type>
    <description>
        First product to have received the label of IASC
        (Conseil International Scientifique de l'Aloe Vera), 
        is a product to be used daily to optimise your health.
        Aloe Vera help to maintain the immune system and helps to 
        strenghen the organism efence.
    </description>
</item>

<!-- Item 4 -->
<item>
    <id>74</id>
    <name>Berry Nectar</name>
    <img alt="Berry Nectar">images/catalogueImg/berry.jpg</img>
    <price>MUR 1200</price>
    <type>Health</type>
    <description>

    </description>
</item>

<!-- Item 5 -->
<item>
    <id>18</id>
    <name>Aloe Sunscreen</name>
    <img alt="Aloe Sunscreen">images/catalogueImg/sun.jpg</img>
    <price>MUR 750</price>
    <type>Beauty</type>
    <description>
        This sun care of high protection SPF 30, protects the
        skin from the negative effects of the sun and from
        aging.
    </description>
</item>

    <!-- Item 1 -->
<item>
    <id>45</id>
    <name>Ginkgo Plus</name>
    <img alt="Ginkgo Plus">images/catalogueImg/ginkgo.jpg</img>
    <price>MUR 575</price>
    <type>Health</type>
    <description>
        A mixture of 3 chineese plant including ginkgo biloba and reishi.
        Many traditional medicines identifies that the leaves of the ginkgo biloba 
        have a positive effect on the circulatory system. The reishi is a champion
        used in chineese and japaneese traditional medicines to protect against 
        heart problems.
    </description>
</item>

<!-- Item 2 -->
<item>
    <id>46</id>
    <name>Hand Sanitizer</name>
    <img alt="Hand Sanitizer">images/catalogueImg/hand.jpg</img>
    <price>MUR 350</price>
    <type>Beauty</type>
    <description>
        Helps to clean and desinfect perfectly your hands
        wherever you are. This product is enriched in 
        Aloe Vera and honey which perfumes your hand without
        drying them.
    </description>
</item>

<!-- Item 3 -->
<item>
    <id>87</id>
    <name>Aloe Vera Gel</name>
    <img alt="Aloe Vera Gel">images/catalogueImg/aloe.jpg</img>
    <price>MUR 1300</price>
    <type>Health</type>
    <description>
        First product to have received the label of IASC
        (Conseil International Scientifique de l'Aloe Vera), 
        is a product to be used daily to optimise your health.
        Aloe Vera help to maintain the immune system and helps to 
        strenghen the organism efence.
    </description>
</item>

<!-- Item 4 -->
<item>
    <id>74</id>
    <name>Berry Nectar</name>
    <img alt="Berry Nectar">images/catalogueImg/berry.jpg</img>
    <price>MUR 1200</price>
    <type>Health</type>
    <description>

    </description>
</item>

<!-- Item 5 -->
<item>
    <id>18</id>
    <name>Aloe Sunscreen</name>
    <img alt="Aloe Sunscreen">images/catalogueImg/sun.jpg</img>
    <price>MUR 750</price>
    <type>Beauty</type>
    <description>
        This sun care of high protection SPF 30, protects the
        skin from the negative effects of the sun and from
        aging.
    </description>
</item>

</catalogue>

这是 xsl 文件

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="/">

<html>
<head>
<link rel="stylesheet" type="text/css" href="catalogueStyle.css"></link>
<link rel="stylesheet" type="text/css" href="style.css"></link>
<title>Catalogue</title>



</head>
<body>  
    <div class="body-wrapper" style="attachment:fixed;">
        <img src="images/header.png"/>
    </div>

    <table border="1">
    <tr>

    <xsl:for-each select="/catalogue/item">

        <td>

        <img src="{img/.}" alt="{img/@alt}" class="imgs-catalogue" title="click for description"/><br/>

        Id: <xsl:value-of select="id/."/><br/>
        Name: <xsl:value-of select="name/."/><br/>
        Type: <xsl:value-of select="type/."/><br/>
        Price: <xsl:value-of select="price/text()"/><br/><br/>
        </td>       

        <xsl:if test="(position() mod 4)=0">
            </tr><tr>
        </xsl:if>

</xsl:for-each>
</tr>
</table>

</body>
</html>

</xsl:template>

</xsl:stylesheet>

我没有时间提供完整的解决方案,但是一些算法的想法:

  • select所有匹配/catalogue/item的元素进入一个变量
  • 在模板中,输出前 4 个项目,将它们包装在
  • 让模板用列表的其余部分(不包括前 4 个)递归调用自身

一种方法是最初 select 只有对应于每个 table 行的第一个单元格的 item 元素,这可以像这样完成

<xsl:for-each select="/catalogue/item[position() mod 4 = 1]">

这实际上对应于 table 的行,因此您可以在 xsl:for-each.

中创建一个 tr 元素

然后,您需要 select 组成该行的 item 个元素,基于当前 item 您已经 selected,这是可以完成的像这样

<xsl:variable name="cells" select="current()|following-sibling::item[position() &lt; 4]" />
<xsl:for-each select="$cells">

因此,它 select 是当前 item 和后面的 3 个(每行 4 个单元格)。对于每一个,您将输出包含所需详细信息的 td

剩下的唯一挑战是最后一行,此时构成这一行的项目可能少于 4 件。您可以在此处创建递归模板以创建空单元格来填充行。

试试这个 XSLT

<xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="rowsize" select="4" />

<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="catalogueStyle.css"></link>
<link rel="stylesheet" type="text/css" href="style.css"></link>
<title>Catalogue</title>
</head>
<body>  
    <div class="body-wrapper" style="attachment:fixed;">
        <img src="images/header.png"/>
    </div>
    <table border="1">
    <xsl:for-each select="/catalogue/item[position() mod $rowsize = 1]">
    <tr>
        <xsl:variable name="cells" select="current()|following-sibling::item[position() &lt; $rowsize]" />
        <xsl:for-each select="$cells">
        <td>
        <img src="{img/.}" alt="{img/@alt}" class="imgs-catalogue" title="click for description"/><br/>
        Id: <xsl:value-of select="id/."/><br/>
        Name: <xsl:value-of select="name/."/><br/>
        Type: <xsl:value-of select="type/."/><br/>
        Price: <xsl:value-of select="price/text()"/><br/><br/>
        </td>       
        </xsl:for-each>
        <xsl:if test="count($cells) &lt; $rowsize">
            <xsl:call-template name="emptyCells">
                <xsl:with-param name="cells" select="$rowsize - count($cells)" />
            </xsl:call-template>
        </xsl:if>
    </tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template name="emptyCells">
    <xsl:param name="cells" />
    <xsl:if test="$cells > 0">
        <td></td>
        <xsl:call-template name="emptyCells">
            <xsl:with-param name="cells" select="$cells - 1" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>

请注意,我已将 4 参数化,以便您轻松改变行大小。