xslt根据其孙节点的属性值选择祖父母节点 [英] xslt select grandparent node depending on an attribute value of its grandchild node
问题描述
我试图根据其孙节点的属性值向节点添加不同的标签.
I'm trying to add different tags to a node depending on an attribute value of its grandchild node.
样本输入(1x3表格):
Sample Input (a 1x3 table):
<table>
<row>
<cell row="1" column="1" >heading text one</cell>
</row>
<row>
<cell row="2" column="1" >body text one</cell>
</row>
<row>
<cell row="3" column="1" >body text two</cell>
</row>
</table>
需要这样的输出:
<TableElmt>
<HeadingElmt>
<RowElmt>
<CellElmt>heading text one</CellElmt>
</RowElmt>
</HeadingElmt>
<BodyElmt>
<RowElmt>
<CellElmt>body text one</CellElmt>
</RowElmt>
<RowElmt>
<CellElmt>body text two</CellElmt>
</RowElmt>
</BodyElmt>
</TableElmt>
基本上,我只能基于单元格的@row元素确定行是否为标题行.
Basically I can only decide if the row is a heading row based on the @row element of the cell.
这是我尝试过的:
<xsl:template name="matcheverything" match="table">
<xsl:apply-templates select="row" />
</xsl:template>
<xsl:template name="matchheadings" match="table[*/*/@row=1]">
<BodyElmt>
<xsl:apply-templates select="row" />
</BodyElmt>
</xsl:template>
<xsl:template match="row">
<xsl:choose>
<xsl:when test="*/@row=1">
<HeadingElmt><RowElmt>
<xsl:apply-templates select="cell"/>
</RowElmt></HeadingElmt>
</xsl:when>
<xsl:otherwise>
<RowElmt>
<xsl:apply-templates select="cell"/>
</RowElmt>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="cell">
<CellElmt><xsl:apply-templates select="*"/></CellElmt>
</xsl:template>
我当时认为具有更具体匹配要求的"matchheadings"模板应该识别标题行,但是实际上它匹配表中的每一行.
I was thinking the "matchheadings" template, having a more specific match requirement, should recognize the heading row, however it's actually matching every single row in the table.
因此,我从此样式表实际输出的结果是将每一行都视为标题行-非常糟糕:(
So my actual out put from this stylesheet is every row treated as a heading row - very bad :(
<TableElmt>
<HeadingElmt>
<RowElmt>
<CellElmt>heading text one</CellElmt>
</RowElmt>
<RowElmt>
<CellElmt>body text one</CellElmt>
</RowElmt>
<RowElmt>
<CellElmt>body text two</CellElmt>
</RowElmt>
</HeadingElmt>
</TableElmt>
推荐答案
此转换:
<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="table">
<TableElmt>
<xsl:apply-templates/>
</TableElmt>
</xsl:template>
<xsl:template match="row[cell/@row='1']">
<HeadingElmt>
<xsl:apply-templates select="." mode="copy"/>
</HeadingElmt>
</xsl:template>
<xsl:template match="row[cell[not(@row='1')]][1]">
<BodyElmt>
<xsl:apply-templates select=".|following-sibling::row" mode="copy"/>
</BodyElmt>
</xsl:template>
<xsl:template match="row" mode="copy">
<RowElmt>
<xsl:apply-templates/>
</RowElmt>
</xsl:template>
<xsl:template match="cell">
<CellElmt>
<xsl:value-of select="."/>
</CellElmt>
</xsl:template>
<xsl:template match="row"/>
</xsl:stylesheet>
应用于提供的XML文档:
<table>
<row>
<cell row="1" column="1" >heading text one</cell>
</row>
<row>
<cell row="2" column="1" >body text one</cell>
</row>
<row>
<cell row="3" column="1" >body text two</cell>
</row>
</table>
产生想要的正确结果:
<TableElmt>
<HeadingElmt>
<RowElmt>
<CellElmt>heading text one</CellElmt>
</RowElmt>
</HeadingElmt>
<BodyElmt>
<RowElmt>
<CellElmt>body text one</CellElmt>
</RowElmt>
<RowElmt>
<CellElmt>body text two</CellElmt>
</RowElmt>
</BodyElmt>
</TableElmt>
这篇关于xslt根据其孙节点的属性值选择祖父母节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!