【发布时间】:2015-05-22 01:35:28
【问题描述】:
我想实现以下目标。
这是一个示例 XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<database>
<rad>
<timeout> 45 </timeout>
</rad>
<tac>
<timeout> 70 </timeout>
</tac>
</database>
1) 使用 XSLT 我想检查 /database/rad/timeout 值是否大于 30(在本例中为 45),将其更改为 30。
2) 添加新标签如下:
<warnings>
<warning>Time out of RAD changed.</warning>
</warnings>
所以输出的 XML 应该包含以下内容:-
<?xml version="1.0" encoding="iso-8859-1"?>
<database>
<rad>
<timeout> 45 </timeout>
</rad>
<tac>
<timeout> 70 </timeout>
</tac>
<warnings>
<warning>Time out of RAD changed.</warning>
</warnings>
</database>
可能存在很多这样的条件。我能够完成第一部分:-
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/database/rad/timeout">
<timeout>
<xsl:choose>
<xsl:when test=". > 30">30</xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</timeout>
</xsl:template>
<!-- ignore text content of nodex -->
<xsl:template match="text()" />
</xsl:stylesheet>
但是对于第二部分,我不确定从哪里开始,任何提示将不胜感激。谢谢。
【问题讨论】:
-
"如果 /database/rad/timeout 值大于 30 ... 将其更改为 30。" 您的输出显示为 45,而您的 XSLT 将其设为 100。那就是令人困惑。