XSLT:如何处理元素值的测试?
我有一个 xml 文件,其中有一个标签,即
或
分别根据值 M 或 F ..我尝试了这段代码..它曾经在其他情况下工作..
I have an xml file in which there is tag namely, <Gender/> It carries either 'M' or 'F' as data, now my work is to test the value and write <Gender_Tag>Male</Gender_Tag>
or <Gender_Tag>Female</Gender_Tag>
according to the values M or F respectively .. I tried this code .. It used to work in other circumstances..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
模板中表达的所有相对路径都会根据当前节点进行评估。您的模板与 Gender 元素匹配,因此如果存在任何名为“Gender”且值为“M”的 Gender 子项,Gender='M' 将返回 true。我想情况并非如此...
使用点来表示当前节点(这里是性别元素):
编辑:您也可以使用两个模板
All relative paths expressed in a template are evaluated against the current node. Your template match Gender elements, so Gender='M' returns true if there is any Gender's child named 'Gender' with the value 'M'. I guess this is not the case...
Use the dot to express the current node (here a Gender element):
EDIT: You may use two templates too
我的示例与 Scoregraphic 的示例有两点不同:
它使用 xsl:choose 来确保仅创建一个 Gender_Tag 元素(这也意味着,如果文本不是“M”,则它始终是女性)
使用
normalize-space( )
去除元素文本内容周围的空白。My example differs in two points from Scoregraphic's:
It uses xsl:choose to ensure, that only one Gender_Tag element is created (that also means, that if the text is not 'M', it is always a Female)
Use of
normalize-space()
strips white space around the text content of the element.未经测试,但可能有效...
Untested, but may work...
如果没有看到 XML,很难确定,但我认为您的示例 XSLT 应该是:
根据另一个答案使用 select 会更好(尽管我认为它应该是两个显式的when子句,而不是一个when和一个otherwise)
Without seeing XML its hard to be certain, but I think your sample XSLT should be:
Use of choose as per another answer would be better (though I think it should be two explicit when clauses rather than a when and an otherwise)