【问题标题】:When previewing XML in browser, I only want to see certain items using XSL在浏览器中预览 XML 时,我只想查看使用 XSL 的某些项目
【发布时间】:2013-05-18 09:03:28
【问题描述】:

我希望 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>

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

【问题讨论】:

  • 您当前的 xsl 究竟得到了什么?
  • 我一直试图让它显示任何东西,但它就是不显示。我想要做的只是让 XML 以任何可能的方式显示我在上面显示的最终结果。
  • 请检查您的输入 xml 有一些小的(复制?)错误。例如。 &lt;/Product_Image/File&gt;

标签: javascript xml xslt xslt-1.0


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多