在浏览器中预览XML时,我只想使用XSL查看某些项目

时间:2013-05-23 05:58:21

标签: javascript xml xslt xslt-1.0

我希望XSL只将第一个PRODUCT_IMAGE_FILE节点从每个PRODUCT / PRODUCT_IMAGES节点中的第一个ITEM节点输出到您在记事本中找到的简单列表。我一直试图让它发挥作用,但这很困难。

这是我需要看到的,最终结果:

hello/this_d_one1.jpg
hello/this_d_one33.jpg

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<products>
 <product>
  <Person>Jim</Person>
  <Product_Images>
    <item>
      <Product_Image_File>hello/this_d_one1.jpg</Product_Image_File>
    </item>
    <item>
      <Product_Image_File>hello/testNOTHISONE.jpg</Product_Image_File>
    </item>
  </Product_Images>
 </product>
 <product>
  <Person>Nancy</Person>
  <Product_Images>
    <item>
      <Product_Image_File>hello/this_d_one33.jpg</Product_Image_File>
    </item>
    <item>
      <Product_Image_File>hello/testNOTHISONE3.jpg</Product_Image_File>
    </item>
  </Product_Images>
 </product>
</products>

这是我的test.XSL:

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

   <xsl:output method="text"/>

   <xsl:template match="products">
      <xsl:call-template name="header" />
      <xsl:apply-templates select="product" />
   </xsl:template>

   <xsl:template name="header">
      <xsl:for-each select="//product[1]/product_images/item/child::*" >
         <xsl:value-of select="name()" />
         <xsl:if test="following-sibling::*">|</xsl:if>
      </xsl:for-each>
      <xsl:text>
</xsl:text>
   </xsl:template>

   <xsl:template match="product/product_images/item">
      <xsl:for-each select="child::*" >
         <xsl:value-of select="." />
         <xsl:if test="following-sibling::*">|</xsl:if>
      </xsl:for-each>
      <xsl:text>
</xsl:text>
   </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

如果您只是在浏览器中显示,则可以执行以下操作:

XML输入(已修复为格式良好)

<products>
    <product>
        <Person>Jim</Person>
        <Product_Images>
            <item>
                <Product_Image_File>hello/this_d_one1.jpg</Product_Image_File>
            </item>
            <item>
                <Product_Image_File>hello/testNOTHISONE.jpg</Product_Image_File>
            </item>
        </Product_Images>
    </product>
    <product>
        <Person>Nancy</Person>
        <Product_Images>
            <item>
                <Product_Image_File>hello/this_d_one33.jpg</Product_Image_File>
            </item>
            <item>
                <Product_Image_File>hello/testNOTHISONE3.jpg</Product_Image_File>
            </item>
        </Product_Images>
    </product>
</products>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/*">
        <xsl:apply-templates select="product/Product_Images[1]/item[1]/Product_Image_File[1]"/>
    </xsl:template>

    <xsl:template match="Product_Image_File">
        <xsl:value-of select="."/>
        <br/>
    </xsl:template>

</xsl:stylesheet>

输出(原始)

hello/this_d_one1.jpg<br>hello/this_d_one33.jpg<br>

输出(已呈现)

您好/ this_d_one1.jpg
你好/ this_d_one33.jpg