変数内のノードをフィルタリング/グループ化しようとしています。XSLT:変数のフィルタ/グループノード
XML:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="row">
<xsl:element name="row">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="cell">
<xsl:variable name="attribute" select="@n"/>
<xsl:variable name="sequence">
<xsl:call-template name="seq">
<xsl:with-param name="attribute" select="$attribute"/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each-group select="$sequence" group-by="cell/@val">
<xsl:sequence select="."></xsl:sequence>
</xsl:for-each-group>
</xsl:template>
<xsl:template name="seq">
<xsl:param name="attribute"/>
<xsl:element name="cell">
<xsl:attribute name="val">
<xsl:value-of select="$attribute"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
意図した出力:
<?xml version="1.0" encoding="UTF-8"?>
<row>
<cell val="1"/>
<cell val="2"/>
<cell val="3"/>
<cell val="4"/>
<cell val="5"/>
</row>
XML-コードがはるかに複雑なファイルのための単なる一例である2
<?xml version="1.0" encoding="UTF-8"?>
<row>
<cell n="1"/>
<cell n="2"/>
<cell n="3"/>
<cell n="4"/>
<cell n="2"/>
<cell n="5"/>
</row>
XSLT。私がしたいことは、すべての要素cell
を最初に処理し、その結果を変数に保存することです。 2回目の実行では、シーケンスのノードをフィルタリングまたは再グループ化します。
Iはまた、述語と$sequence
をフィルタリングすることを試みた:
<xsl:sequence select="$sequence/*[@val != preceding-sibling::cell/@val]"/>
この場合、出力ファイルが空です。
EDIT 2(今それが動作する):ここ
<?xml version="1.0" encoding="UTF-8"?>
<xsl:template match="row">
<xsl:variable name="sequence">
<xsl:for-each select="cell">
<xsl:variable name="attribute" select="@n"/>
<xsl:call-template name="seq">
<xsl:with-param name="attribute" select="$attribute"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<row>
<xsl:for-each-group group-by="@val" select="$sequence/*">
<xsl:sequence select="."/>
</xsl:for-each-group>
</row>
</xsl:template>
<xsl:template name="seq">
<xsl:param name="attribute"/>
<xsl:element name="cell">
<xsl:attribute name="val">
<xsl:value-of select="$attribute"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
です。すべての 'cell'要素を最初に処理し、結果を変数に格納して処理したい場合は、' row'(またはそれ以上)のコンテキストから行う必要があります。私はそれがこの時点で言えることすべてについてだと思う。 –
私は以前のコードを編集しようとしましたが、今は '$属性 'を渡すことができません。私はXSLTについての私の知識がここに限界を感じるのではないかと心配しています。 – Ferestes
Ditto Michaelのコメント - あなたがしようとしていることははっきりしていません。また、 '$ sequence'変数には単一の' cell'要素、 ' '。 '$ sequence'は一つの要素しか持っていないので、' preceding-sibling'を使って '$ sequence'をフィルタリングしようとすると失敗します。 Michaelが言っているように、一度に複数の「セル」をターゲットにするには、 'row'以上のコンテキストから開始する必要があります。 –