我有一个 xml 文档,其中包含一堆节点,其中某些节点的值为 true 和 false。我正在开发一个 xslt,将该 xml 文档转换为逗号(或“&”)分隔的字符串。我想将所有 true false 值替换为 yes 和 no,而不必执行 xsl:when true then yes else no,因为有很多值。我有办法做到这一点吗?非常感谢任何帮助。
XML:
<MainNode>
<P1>
<someNode/>
<P2>
<History>
<MedicalHistory>
<Medication>true</Medication>
<Medical_Treatment>true</Medical_Treatment>
<Hospital>false</Hospital>
<Comments>some Comment</Comments>
</MedicalHistory>
</History>
<Info>
<Name>Sam</Name>
<Gender>male</Gender>
<Married>false</Married>
</Info>
</P2></P1></MainNode>
XSLT:
xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:text>name=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/Info/Name"/>
<xsl:text>&Gender=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/Info/Gender"/>
<xsl:text>&Married=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/Info/Married"/>
<xsl:text>&Comments=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/History/Comments"/>
<xsl:text>&Medication=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/History/Medication"/>
</xsl:template>
</xsl:stylesheet>
输出: 姓名=山姆&性别=男&已婚=假&评论=一些评论&药物=真
我希望输出将所有“true”和“false”替换为“yes”和“no” 输出: 姓名=山姆&性别=男&已婚=否&评论=一些评论&药物=是
现在我正在 xslt 中遵循这种方法
xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:text>name=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/Info/Name"/>
<xsl:text>&Gender=</xsl:text>
<xsl:choose>
<xsl:when test="//MainNode/P1/P2/Info/Gender = 'true'">
<xsl:text>yes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>no</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>&Married=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/Info/Married"/>
<xsl:text>&Comments=</xsl:text>
<xsl:value-of select="//MainNode/P1/P2/History/Comments"/>
<xsl:text>&Medication=</xsl:text>
xsl:choose>
<xsl:when test="//MainNode/P1/P2/History/Medication = 'true'">
<xsl:text>yes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>no</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当然,主要的 xslt 和 xml 相当大且复杂,导致一堆“when/other” block
请帮我解决这个问题。对 xslt 不太有经验。 谢谢!
最佳答案
有很多方法可以解决这个问题。 IE。将这两个模板添加到您的 xslt 中:
<xsl:template match="text()[.='true']" >
<xsl:text>yes</xsl:text>
</xsl:template>
<xsl:template match="text()[.='false']" >
<xsl:text>no</xsl:text>
</xsl:template>
然后代替
<xsl:choose>
<xsl:when test="//MainNode/P1/P2/Info/Gender = 'true'">
<xsl:text>yes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>no</xsl:text>
</xsl:otherwise>
</xsl:choose>
做:
<xsl:apply-templates select="//MainNode/P1/P2/Info/Gender/text()"/>
关于xml - XSLT 2.0 将 xml 中所有出现的 'true/false' 替换为 'yes/no',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67591656/