简体   繁体   中英

How to transform xsl that XML document has namespace

  • I have XML document with namespace ns2 and default namespace:

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

And xsl file to display table, use data of xml file

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

  • I import 2 file(xml and xsl) and run 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}"/> 

But when run jsp page, data of row table is same i don't know

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

Please help me fix this bug, Thank you so much

Because you start everything with // it will search the whole document, so every time your for-each finds a product it will go and find the first element that matches ProductName instead of the ProductName in the current Product element.

Try the following xsl instead:

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

With the input of

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

It gives me the output of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM