【问题标题】:How to transform xsl that XML document has namespace如何转换 XML 文档具有命名空间的 xsl
【发布时间】:2019-03-27 05:41:18
【问题描述】:
  • 我有一个带有命名空间 ns2 和默认命名空间的 XML 文档:

 <ns2:Products xmlns="https://www.schema.product.com" xmlns:ns2="https://www.schema.products.com">
        <Product ProductId="1">
            <ProductName> Hộp Hoa Hồng Trắng</ProductName>
            <ProductPrice>550000</ProductPrice>
            <ProductImage>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</ProductImage>
        </Product>
        <Product ProductId="2">
            <ProductName>An Lành</ProductName>
            <ProductPrice>780000</ProductPrice>
            <ProductImage>https://dienhoa24gio.net//assets/upload/product/17-10-2015/an-lanh-1445039808/274_default.jpg</ProductImage>
        </Product>
    </ns2:Products> 

和xsl文件显示表格,使用xml文件的数据

 <xsl:template match="//*[local-name()='Products']">
        <table border="1">
                    <tr>
                        <th>name</th>
                        <th>price</th>
                        <th>image</th>
                    </tr>
                    <xsl:for-each select="//*[local-name()='Product']">
                        <tr>
                            <td><xsl:value-of select="//*[local-name()='ProductName']"/></td>
                       
                            <td><xsl:value-of select="//*[local-name()='ProductPrice']"/></td>
                        
                            <td><xsl:value-of select="//*[local-name()='ProductImage']"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
    </xsl:template>
  • 我导入 2 个文件(xml 和 xsl)并运行 jsp

<c:import url="test.xml" var="xmlDoc" charEncoding="UTF-8"/>
        <c:import url="test.xsl" var="xslDoc" charEncoding="UTF-8"/>
        <x:transform xml="${xmlDoc}" xslt="${xslDoc}"/>

但是当运行jsp页面时,行表的数据是一样的我不知道

<table border="1">
<tr>
<th>name</th><th>price</th><th>image</th>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
</table>

请帮我修复这个错误, 非常感谢

【问题讨论】:

标签: xml xslt namespaces xml-namespaces


【解决方案1】:

因为您以 // 开始所有内容,它将搜索整个文档,因此每次您的 for-each 找到产品时,它都会查找第一个匹配 ProductName 而不是当前 Product 元素中的 ProductName 的元素。

请尝试以下 xsl:

     <xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"><xsl:template match="//*[local-name()='Products']">
            <table border="1">
                        <tr>
                            <th>name</th>
                            <th>price</th>
                            <th>image</th>
                        </tr>
                        <xsl:for-each select="//*[local-name()='Product']">
                            <tr>
                                <td><xsl:value-of select="*[local-name()='ProductName']"/></td>

                                <td><xsl:value-of select="*[local-name()='ProductPrice']"/></td>

                                <td><xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="*[local-name()='ProductImage']"/></xsl:attribute></xsl:element></td>
                            </tr>
                        </xsl:for-each>
                    </table>
        </xsl:template>
    </xsl:stylesheet>

随着

的输入
<ns2:Products xmlns="https://www.schema.product.com" xmlns:ns2="https://www.schema.products.com">
        <Product ProductId="1">
            <ProductName> Hộp Hoa Hồng Trắng</ProductName>
            <ProductPrice>550000</ProductPrice>
            <ProductImage>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</ProductImage>
        </Product>
        <Product ProductId="2">
            <ProductName>An Lành</ProductName>
            <ProductPrice>780000</ProductPrice>
            <ProductImage>https://dienhoa24gio.net//assets/upload/product/17-10-2015/an-lanh-1445039808/274_default.jpg</ProductImage>
        </Product>
    </ns2:Products> 

它给了我输出

<table border="1">
   <tr>
      <th>name</th>
      <th>price</th>
      <th>image</th>
   </tr>
   <tr>
      <td> Hộp Hoa Hồng Trắng</td>
      <td>550000</td>
      <td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
   </tr>
   <tr>
      <td>An Lành</td>
      <td>780000</td>
      <td>https://dienhoa24gio.net//assets/upload/product/17-10-2015/an-lanh-1445039808/274_default.jpg</td>
   </tr>
</table>

【讨论】:

  • 非常感谢,我解决了这个bug,如果我想把这个图片链接添加到
  • 将获取图像 url 的行更改为 来构建一个带有 src 属性的 img 元素。我已经编辑了答案以反映 htis。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 2010-10-08
相关资源
最近更新 更多