【问题标题】:xml <![CData[]]> need browser to decode itxml <![CData[]]> 需要浏览器解码
【发布时间】:2016-04-07 23:27:08
【问题描述】:

我的 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>

【问题讨论】:

  • 这是一个 XSLT 问题吗?如果是这样,请编辑您的问题并添加输入 XML 文档、您的 XSLT 样式表和您期望的输出。在这里寻求帮助:stackoverflow.com/help/mcve编辑:请不要链接到信息(在此处添加)并且不要在图像中显示代码(而是作为文本)。
  • 您以错误的方式处理此问题。与其检测何时更改行并尝试插入换行符,不如收集您想要组成行的元素并用 ... 包围它们。如果您希望有人对其发表评论,请发布一些示例输入和您当前的代码。
  • @MathiasMüller 感谢您的建议
  • @RolfRander 让我把xml文档和xsl文件贴出来让你更好的理解

标签: html xml xslt cdata


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    一种方法是最初只选择与每个表格行的第一个单元格对应的item 元素,可以这样完成

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

    这实际上对应于表的行,因此您将在 xsl:for-each 内创建一个 tr 元素。

    然后,您需要根据您选择的当前item,选择构成行的item元素,可以这样完成

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

    因此,它选择当前的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 进行了参数化,以便您轻松更改行大小。

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 2015-04-09
      • 2012-09-29
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多