【发布时间】:2016-09-02 08:37:22
【问题描述】:
我想实现更改属性值并用 XSL 替换元素的子字符串。
XML
<...>
<communication type="telephone">123 456 789 </communication>
<communication type="telephone">789 (EXT)</communication>
<communication type="telephone">123 456 789 </communication>
</...>
应该是
<...>
<communication type="telephone">123 456 789 </communication>
<communication type="ext">789</communication>
<communication type="telephone">123 456 789 </communication>
</...>
XSL (2.0)
<xsl:template match="communication[@type='telephone'][contains(text(),'(EXT)')]">
<xsl:copy>
<xsl:value-of select="replace(., '(EXT)', '')"/>
<xsl:attribute name="extension">true</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
撒克逊州 “在包含元素的子元素之后不能创建属性节点”
我没有实现更改属性类型的值,所以我创建了一个新属性。但即使使用这种解决方法,我也不知道如何使这两个要求(添加属性和删除子字符串)都起作用。
非常感谢任何解决此问题的想法!
【问题讨论】:
-
您的标题显示“更改属性值”,并且您的输出显示您要将
typeattribute 的值从“电话”更改为“分机”。但是您的代码正在添加一个新的extension属性。哪个是正确的? -
对此感到抱歉。我的意思是添加另一个具有相同值的属性“扩展”。