2012-02-22 2 views
2

XSLT 2.0では、node-setパラメータをposition()関数の一部として渡すことができます。残念ながら、これはXSLT 1.0では利用できません。この動作を模倣する方法はありますか?例えばXSLT 1.0の位置(ノードセット)を模倣していますか?

、このXMLを与え:

<wishlists> 
    <wishlist name="Games"> 
    <product name="Crash Bandicoot"/> 
    <product name="Super Mario Brothers"/> 
    <product name="Sonic the Hedgehog"/> 
    </wishlist> 
    <wishlist name="Movies"> 
    <product name="Back to the Future"/> 
    </wishlist> 
</wishlists> 

このXSLT 2.0:

<xsl:value-of select="position(/wishlists/wishlist/product)"/> 

最終 "バック・トゥ・ザ・フューチャー" ノードを処理するときに "4" が返される値。

残念ながら、私はXSLT 1.0で取得することができるように見える最も近いは次のとおりです:

<xsl:template match="product"> 
    <xsl:value-of select="position()"/> 
</xsl:template> 

しかし、私は同じ「バック・トゥ・ザ・フューチャー」ノードに「1」の値になるだろう私が本当に欲しい "4"の値とは対照的です。

+1

'position()'の引数を使用できるXSLT 2.0プロセッサはありますか? –

+0

私はそれがXSLT 2.0で可能だったと思った。たぶん私は私が読んだことを誤解したのかもしれません(あるいは誤ってcount()をposition()などとして誤解しているかもしれません)。私がXSLT 2.0について誤解している場合、私の質問は私がやろうとしていることについてどうやって行くのかについてはまだ立っています。 :) –

+0

位置(node-set())関数で想像しているセマンティクスがわからないので、実装方法を説明するのは難しいです。 –

答えて

3

preceding axisを使用できます。

このXSLT 1.0スタイルシート:あなたのXML入力に印加

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

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="product"> 
    <product position="{count(preceding::product) + 1}"> 
     <xsl:apply-templates select="@*"/> 
    </product>  
    </xsl:template> 

</xsl:stylesheet> 

生成:

<wishlists> 
    <wishlist name="Games"> 
     <product position="1" name="Crash Bandicoot"/> 
     <product position="2" name="Super Mario Brothers"/> 
     <product position="3" name="Sonic the Hedgehog"/> 
    </wishlist> 
    <wishlist name="Movies"> 
     <product position="4" name="Back to the Future"/> 
    </wishlist> 
</wishlists> 
+0

素晴らしい、前の軸のような音は間違いなく私が探していたものです。すぐにそれを試して、それが動作するとすぐに回答をマークします。ありがとう! –

+0

大歓迎です! –

2

XSLT 2.0は 位置の一部としてノードセットのPARAMを渡すことの利点を提供しています() 関数。

このステートメントは間違っています。 position()関数には、XSLT 2.0が使用するXPath 1.0またはXPath 2.0のいずれの引数もありません。何をしたい

です:

count(preceding::product) +1 

、あるいは、xsl:number命令を使用することができます。この変換は、提供されるXML文書に適用される場合

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

<xsl:variable name="vLastProd" select= 
    "//product[@name='Back to the Future']"/> 

<xsl:template match="/"> 
    <xsl:value-of select="count($vLastProd/preceding::product) +1"/> 
========= 
<xsl:text/> 
    <xsl:apply-templates select="$vLastProd"/> 
</xsl:template> 

<xsl:template match="product"> 
    <xsl:number level="any" count="product"/>  
</xsl:template> 
</xsl:stylesheet> 

:ここ

は、これらの方法両方の実証である

<wishlists> 
    <wishlist name="Games"> 
     <product name="Crash Bandicoot"/> 
     <product name="Super Mario Brothers"/> 
     <product name="Sonic the Hedgehog"/> 
    </wishlist> 
    <wishlist name="Movies"> 
     <product name="Back to the Future"/> 
    </wishlist> 
</wishlists> 

希望、正しい結果が得られました両方の方法と出力を使用して

4 
========= 
4 

:それは直接出力されない場合xsl:numberの結果は、変数の体内に捕捉される必要があります。

+0

ええ、DevNullは質問のコメントに言及し、私は明らかにどこかで読んだことを誤って解釈しました! –

+0

@MattHuggins:はい、あなたは "位置"と呼ばれるものを得るために2つの方法を提供しています。どちらもXSLT 1.0またはXSLT 2.0で使用できます。 –

+0

クール、ありがとう!私が最初にコメントを書いたとき、あなたの編集はそこになかった。 ;) –

関連する問題