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

"//*"は「ルートから始まるすべてのノード」です。おそらく、「/catalog/product/*」のようなより制限的な XPath が必要になるでしょう (または、local-name名前空間を適切に処理したくない場合は関数で同様のものを使用します)。

于 2013-01-04T08:03:25.503 に答える