如何引用 XSLT 2.0 中的分组元素?
How to reference grouped elements in XSLT 2.0?
我想对以下 XML 文件进行分组:
<record>
<word>Hallo</word><translit>XUPPY</translit><gloss>gloss1</gloss>
<word>Bye</word><translit>EHS</translit><gloss>gloss2</gloss>
<word>Yes</word><translit>KGR</translit><gloss>gloss3</gloss>
</record>
期望的输出是
<s>
<set><word>Hallo</word><gloss>gloss1</gloss></set>
<set><word>Bye</word><gloss>gloss2</gloss></set>
<set><word>Yes</word><gloss>gloss3</gloss></set>
</s>
所以我使用 for-each-group
,如
<xsl:template match="record">
<xsl:element name="s">
<xsl:for-each-group select="*" group-starting-with="word">
<xsl:element name = "set">
<xsl:copy-of select="current-group()/word"/>
<xsl:copy-of select="current-group()/gloss"/>
</xsl:element>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
但是,我得到了这个输出,即空组:
<s><set/><set/><set/></s>
所以我在引用组中的元素时做错了。但是什么?我相信这一定很简单。
word
和 gloss
是当前组的 成员 ,而不是它的 子组 。
尝试:
<xsl:template match="/record">
<s>
<xsl:for-each-group select="*" group-starting-with="word">
<set>
<xsl:copy-of select="current-group()[self::word or self::gloss]"/>
</set>
</xsl:for-each-group>
</s>
</xsl:template>
current-group()
给你组中的所有元素,如果你想 select 某个元素使用 current-group()[self::gloss]
。
我会替换
<xsl:element name = "set">
<xsl:copy-of select="current-group()/word"/>
<xsl:copy-of select="current-group()/gloss"/>
</xsl:element>
与
<set>
<xsl:copy-of select="., current-group()[self::gloss]"/>
</set>
我想对以下 XML 文件进行分组:
<record>
<word>Hallo</word><translit>XUPPY</translit><gloss>gloss1</gloss>
<word>Bye</word><translit>EHS</translit><gloss>gloss2</gloss>
<word>Yes</word><translit>KGR</translit><gloss>gloss3</gloss>
</record>
期望的输出是
<s>
<set><word>Hallo</word><gloss>gloss1</gloss></set>
<set><word>Bye</word><gloss>gloss2</gloss></set>
<set><word>Yes</word><gloss>gloss3</gloss></set>
</s>
所以我使用 for-each-group
,如
<xsl:template match="record">
<xsl:element name="s">
<xsl:for-each-group select="*" group-starting-with="word">
<xsl:element name = "set">
<xsl:copy-of select="current-group()/word"/>
<xsl:copy-of select="current-group()/gloss"/>
</xsl:element>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
但是,我得到了这个输出,即空组:
<s><set/><set/><set/></s>
所以我在引用组中的元素时做错了。但是什么?我相信这一定很简单。
word
和 gloss
是当前组的 成员 ,而不是它的 子组 。
尝试:
<xsl:template match="/record">
<s>
<xsl:for-each-group select="*" group-starting-with="word">
<set>
<xsl:copy-of select="current-group()[self::word or self::gloss]"/>
</set>
</xsl:for-each-group>
</s>
</xsl:template>
current-group()
给你组中的所有元素,如果你想 select 某个元素使用 current-group()[self::gloss]
。
我会替换
<xsl:element name = "set">
<xsl:copy-of select="current-group()/word"/>
<xsl:copy-of select="current-group()/gloss"/>
</xsl:element>
与
<set>
<xsl:copy-of select="., current-group()[self::gloss]"/>
</set>