Xml xslt:在处理过程中如何将两个模板应用于同一个节点?
我有一个XSL模板,它匹配任何带有Xml xslt:在处理过程中如何将两个模板应用于同一个节点?,xml,xslt-2.0,Xml,Xslt 2.0,我有一个XSL模板,它匹配任何带有子元素的元素: <xsl:template match="*[var]"> <xsl:copy > <xsl:attribute name="editable"> <xsl:for-each select="var[@attr]"> <xsl:value-of selec
子元素的元素:
<xsl:template match="*[var]">
<xsl:copy >
<xsl:attribute name="editable">
<xsl:for-each select="var[@attr]">
<xsl:value-of
select="concat(@attr,
substring(' ',
1 div (position()!=last())))"/>
</xsl:for-each>
</xsl:attribute>
<xsl:attribute name="constraints">
<xsl:for-each select="var[@ok]">
<xsl:value-of
select="concat(@attr,':',@ok,
substring(';',
1 div (position()!=last())))"/>
</xsl:for-each>
</xsl:attribute>
<!-- if this is an <a> then we have to put the stuff inside it inside it -->
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
这只是将其转换为
,如果字段有名称,则名称与字段相同,否则为“true”
我遇到的问题是,*[var]
匹配一个字段,如果该字段的子字段是
。但是我想要做的是首先匹配*[var]
,然后匹配字段,但之后再匹配
目前,输入为
<field name="field1">
<var attr="class" ok="small,smaller" />
Text
</field>
正文
我明白了
正文
但是我想要
<span field="field1" editable="class" constraints="class:small,smaller">
Text
</span>
正文
我找到了一些关于做两次传球的答案,但我不确定这是否是正确的答案,也不确定如何实现我找到的答案。我应该如何处理这个问题,如果有一个简单的答案,它是什么?:)
短暂性脑缺血发作
阿尔特鲁斯
真的
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[var]">
<xsl:copy>
<xsl:call-template name="makeVarAttribute"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="field" priority="1">
<span field="{concat(@name,
substring('true',
1 div not(@name)
)
)}">
<xsl:apply-templates select="." mode="makeVarAttribute"/>
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="var"/>
<xsl:template match="node()" mode="makeVarAttribute"/>
<xsl:template match="*[var]"
name="makeVarAttribute"
mode="makeVarAttribute">
<xsl:attribute name="editable">
<xsl:for-each select="var[@attr]">
<xsl:value-of
select="concat(@attr,
substring(' ',
1 div (position()!=last())
)
)"/>
</xsl:for-each>
</xsl:attribute>
<xsl:attribute name="constraints">
<xsl:for-each select="var[@ok]">
<xsl:value-of
select="concat(@attr,':',@ok,
substring(';',
1 div (position()!=last())
)
)"/>
</xsl:for-each>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
注意:中的序列处理,作为表达式中的最后一步,@separator
如果要将两个模板应用于同一节点,可以使用xsl:next match:从匹配节点的最高优先级规则中,可以调用xs:next match来调用下一个最高优先级,依此类推。当然,您可以使用xsl:template上的priority属性“手动”调整优先级
这里还有其他几种可能性:
(a) 在我看来,快速浏览“var”元素的处理可以在与var元素匹配的模板中完成,而不是在*[var]中完成,在这种情况下,xsl:apply templates可以以通常的方式调用它
(b) 第一个模板可以在特殊模式下使用apply templates调用第二个模板
(c) 第一个模板可以在变量中构造一个临时元素,然后将模板应用于该变量 从我所有的搜索中,没有发现下一场比赛的魔力。我确实尝试了你建议的一些事情,但我敢说我做得不对:)像这样移出var元素是我考虑过的一件事;但是我不确定我将如何执行第二个连接-带分号的连接。恐怕我没有完全理解原文——它只是起作用了——而且我还没有花时间坐下来解决它:哦!也许在项目迭代的最后。。。
<span field="field1" editable="class" constraints="class:small,smaller">
Text
</span>
<xsl:template match="var">
<xsl:attribute name="editable">
<xsl:value-of select="@attr"/>
</xsl:attribute>
<xsl:attribute name="constraints">
<xsl:value-of select="concat(@attr,':',@ok)"/>
</xsl:attribute>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="field">
<span>
<xsl:attribute name="field">
<xsl:choose>
<xsl:when test="@name">
<xsl:value-of select="@name"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>true</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[var]">
<xsl:copy>
<xsl:call-template name="makeVarAttribute"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="field" priority="1">
<span field="{concat(@name,
substring('true',
1 div not(@name)
)
)}">
<xsl:apply-templates select="." mode="makeVarAttribute"/>
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="var"/>
<xsl:template match="node()" mode="makeVarAttribute"/>
<xsl:template match="*[var]"
name="makeVarAttribute"
mode="makeVarAttribute">
<xsl:attribute name="editable">
<xsl:for-each select="var[@attr]">
<xsl:value-of
select="concat(@attr,
substring(' ',
1 div (position()!=last())
)
)"/>
</xsl:for-each>
</xsl:attribute>
<xsl:attribute name="constraints">
<xsl:for-each select="var[@ok]">
<xsl:value-of
select="concat(@attr,':',@ok,
substring(';',
1 div (position()!=last())
)
)"/>
</xsl:for-each>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
<span field="field1" editable="class" constraints="class:small,smaller">
Text
</span>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[var]">
<xsl:copy>
<xsl:call-template name="makeVarAttribute"/>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="field" priority="1">
<span field="{(@name,'true')[1]}">
<xsl:apply-templates select="." mode="makeVarAttribute"/>
<xsl:apply-templates />
</span>
</xsl:template>
<xsl:template match="var"/>
<xsl:template match="node()" mode="makeVarAttribute"/>
<xsl:template match="*[var]"
name="makeVarAttribute"
mode="makeVarAttribute">
<xsl:attribute name="editable" select="var/@attr"/>
<xsl:attribute name="constraints"
select="var[@ok]/concat(@attr,':',@ok)"
separator=";"/>
</xsl:template>
</xsl:stylesheet>