在浏览器中预览XML时,我只希望使用XSL看到某些项

When previewing XML in browser, I only want to see certain items using XSL

本文关键字:XSL 希望 浏览器 XML      更新时间:2023-09-26

我希望XSL只将每个PRODUCT/PRODUCT_IMAGES节点中的第一个ITEM节点中的第一个PRODUCT_IMAGE_FILE节点输出到您可以在记事本中找到的简单列表中。我一直在试着让它工作,但是很难。

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

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>

这是我的测试。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>

如果你只是在浏览器中显示,你可以这样做:

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>

(呈现)

hello/this_d_one1.jpg
hello/this_d_one33.jpg