1

我有一个非常大的 xml,其中一个块不断重复。现在我只想要第一个块的标签名称。我的 xsl 很差。我试过但徒劳无功。请任何人帮助。我的 xml 如下所示,

<catalog>
<product>
<title>GATE MCQ For Electronics &amp; Communication Engineering</title>
<originalprice>Rs 680</originalprice>
<sellingprice>Rs 680Rs 530</sellingprice>
<discount>22% Off</discount>
<payment>Cash on delivery available</payment>
<review/>
</product>
<product>
<title>Gate Guide Computer Science / Information Technology (with CD)</title>
<originalprice>Rs 695</originalprice>
<sellingprice>Rs 695Rs 480</sellingprice>
<discount>31% Off</discount>
<payment>Cash on delivery available</payment>
<review/>
</product>

产品块不断重复但具有不同的值。我现在使用的 xsl 是,

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns="http://www.java2s.com"
            xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
            version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="//*[local-name() = 'product'][1]/*">

<xsl:value-of select="name()"/>
</xsl:template>

</xsl:stylesheet>

得到的输出是,

    title
    originalprice
    sellingprice
    discount
    payment 
    review 


   Gate Guide Computer Science / Information Technology (with CD)
   Rs 695
   Rs 695Rs 480
   31% Off
   Cash on delivery available

但所需的输出是,

    title
    originalprice
    sellingprice
    discount
    payment 
    review 

谢谢你。

4

3 回答 3

2

你需要从根开始你的模板(通过匹配'/'),然后限制你想要使用'apply-templates'处理的元素,如下所示:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns="http://www.java2s.com"
            xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
            version="1.0">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <xsl:apply-templates select="/catalog/product[position() = 1]"/>
  </xsl:template>
  <xsl:template match="product">
    <xsl:for-each select="./*">
      <xsl:value-of select="name()"/>
      <xsl:text>
      </xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

这将产生所需的输出......

于 2013-01-04T08:22:53.597 回答
2

就这么简单

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

 <xsl:template match="product[1]/*">
     <xsl:value-of select="concat(name(), '&#xA;')"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<catalog>
    <product>
        <title>GATE MCQ For Electronics &amp; Communication Engineering</title>
        <originalprice>Rs 680</originalprice>
        <sellingprice>Rs 680Rs 530</sellingprice>
        <discount>22% Off</discount>
        <payment>Cash on delivery available</payment>
        <review/>
    </product>
    <product>
        <title>Gate Guide Computer Science / Information Technology (with CD)</title>
        <originalprice>Rs 695</originalprice>
        <sellingprice>Rs 695Rs 480</sellingprice>
        <discount>31% Off</discount>
        <payment>Cash on delivery available</payment>
        <review/>
    </product>
</catalog>

产生了想要的正确结果:

title
originalprice
sellingprice
discount
payment
review
于 2013-01-04T12:59:06.183 回答
1

"//*"是“从根开始的所有节点”,您可能需要更严格的 XPath,例如“/catalog/product/*”(local-name如果您不想正确处理命名空间,则与函数类似)。

于 2013-01-04T08:03:25.503 回答