我想用一个元素替换一个特殊的字符。当单独执行最后一个模板时,这已经起作用了。
<!-- copy whole xml -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- rename delement to replacedElement and only copy its text> -->
<xsl:template name="delement">
<xsl:element name="replacedElement"
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:template>
<!-- replace special char with element -->
<xsl:template match="descendant-or-self::text()">
<xsl:analyze-string select="." regex="-">
<xsl:matching-substring>
<elementForSC/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>当复制更深/嵌套元素的文本时,问题似乎类似于第二个模板匹配。此文本也包含特殊字符,但未被替换。
示例:
<body>
just some text but - char is replaced here.
<delement>
here text with - in it but it didn't get replaced
</delement>
</body>结果:
<body>
just some text but <elementForSC/> char is replaced here.
<replacedElement>
here text with - in it but it didn't get replaced
</replacedElement>
</body>你知道如何在修改/复制的文本上递归地应用特殊的字符替换“规则”吗?
另请参阅帮助以获得更好的标题欢迎;-)
发布于 2018-01-30 16:54:30
如果您希望将文本节点的模板应用于所有文本节点,则必须使用apply-templates而不是value-of来保持递归处理的活动状态。
<xsl:template match="delement">
<replacedElement>
<xsl:apply-templates/>
</replacedElement>
</xsl:template>您还可以简化文本节点的匹配模式,使其只使用match="text()"。
https://stackoverflow.com/questions/48516208
复制相似问题