Xml 在循环中调用模板(XSLT 2.0)
假设我们正在处理XSLT2.0 考虑以下XML:Xml 在循环中调用模板(XSLT 2.0),xml,templates,Xml,Templates,假设我们正在处理XSLT2.0 考虑以下XML: <?xml version="1.0" encoding="UTF-8"?> <books> <book name="history_123" attr1="HIST.123" attr2="history_123_new" attr3="1"> <chapter name="history_123_666"/> <chapter name="history_123_666
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book name="history_123" attr1="HIST.123" attr2="history_123_new" attr3="1">
<chapter name="history_123_666"/>
<chapter name="history_123_666"/>
<chapter name="history_123_666"/>
<chapter name="history_123_666"/>
</book>
<book name="geography_123" attr1="GEO.123" attr2="geography_123_new" attr3="1">
<chapter name="geography_123_666"/>
<chapter name="geography_123_666"/>
<chapter name="geography_123_666"/>
</book>
<book name="chemistry" attr1="CHEM.123" attr2="chemistry_123_new" attr3="1">
<chapter name="chemistry_123_666"/>
<chapter name="chemistry_123_666"/>
<chapter name="chemistry_123_666"/>
</book>
</books>
我正在应用以下xsl表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="book[starts-with(@attr1, 'HIST')]">
<xsl:call-template name="replace_attributes">
<xsl:with-param name="attr1Value" select="'123'"/>
<xsl:with-param name="prefix" select="'hist'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="book[starts-with(@attr1, 'GEO')]">
<xsl:call-template name="replace_attributes">
<xsl:with-param name="attr1Value" select="'456'"/>
<xsl:with-param name="prefix" select="'geo'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="book[starts-with(@attr1, 'CHEM')]">
<xsl:call-template name="replace_attributes">
<xsl:with-param name="attr1Value" select="'678'"/>
<xsl:with-param name="prefix" select="'chem'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace_attributes">
<xsl:param name="attr1Value" />
<xsl:param name="prefix"/>
MATCH <xsl:value-of select="$attr1Value"/>
MATCH <xsl:value-of select="@attr1"/>
</xsl:template>
<!-- identity transform -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
注意,我必须使用参数attr1Value和prefix的不同值调用模板3次。如何使用集合和循环,以便在循环中压缩代码并调用模板函数一次,以获得attr1Value和prefix的不同值
谢谢大家! 我不确定这里是否需要收藏。您能告诉我们如何从输入XML计算属性值吗?谢谢。假设属性值是已知的。您可以假设我们可以创建三个参数。第一个模板匹配调用的param1=GEO、param2=456和param3=GEO。我想删除第一个调用之后几乎重复的调用,并使用参数来实现这一点。