2

以下は私の XML ファイルです。XSLT で何らかの形式のカウント関数を使用して、XML ファイルのタイトル 3 から 4 を取得したいと考えています。助けてください...助けてくれてありがとう

<?xml version="1.0">
<catalog>
<cd>
    <title>Empire Burlesque</title>
</cd>
<cd>
    <title>Hide your heart</title>
</cd>
<cd>
    <title>Greatest Hits</title>
</cd>
<cd>
    <title>Still got the blues</title>
</cd>
</catalog>
4

3 に答える 3

0

これを試して:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<result>
 <cd><xsl:value-of select="catalog/cd[3]/title"/></cd>
 <cd><xsl:value-of select="catalog/cd[4]/title"/></cd>
</result>
</xsl:template>
</xsl:stylesheet>
于 2012-11-24T11:44:34.907 に答える
0

position()XPath 関数を探しています。

例えば:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <result>
      <xsl:copy-of 
        select="catalog/cd[position() &gt;= 3 and position() &lt;= 4]/title"/>
    </result>
  </xsl:template>
</xsl:stylesheet>
于 2012-11-24T12:12:26.593 に答える
0

この短くて完全に「プッシュ スタイル」の変換:

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

 <xsl:template match="cd/node()"/>
 <xsl:template match="cd[position() >= 3 and 4 >= position()]/title">
     <xsl:copy><xsl:apply-templates/></xsl:copy>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<catalog>
    <cd>
        <title>Empire Burlesque</title>
    </cd>
    <cd>
        <title>Hide your heart</title>
    </cd>
    <cd>
        <title>Greatest Hits</title>
    </cd>
    <cd>
        <title>Still got the blues</title>
    </cd>
</catalog>

必要な正しい結果が生成されます。

<title>Greatest Hits</title>
<title>Still got the blues</title>

説明:

  1. 空のテンプレート<xsl:template match="cd/node()"/>は、 の子の処理 (「削除」) を防ぎcdます。

  2. 2 番目のテンプレートは、3 以上 4 以下の a の子に対してのみ、最初のテンプレートをオーバーライドします。これは、一致した要素を効果的にコピーtitleします。cdposition()title

  3. この<xsl:strip-space elements="*"/>命令は、XML ドキュメントからすべての空白のみのテキスト ノードを削除することで、これらすべてを可能にします。このようにcdして、命令によって形成されたノードリスト<xsl:apply-templates>(要素の組み込み XSLT テンプレート) 内の要素の位置は、2、4、6、8 ではなく、1、2、3、4 になります。

于 2012-11-24T16:00:12.613 に答える