XSLT 2.0 将 xml 中出现的所有 'true/false' 替换为 'yes/no'

XSLT 2.0 Replace all occurrences of 'true/false' from xml with 'yes/no'

我有一个 xml 文档,其中包含一堆节点,其中某些节点作为值是 true 和 false。我正在使用 xslt 将 xml 文档转换为逗号(或“&”)分隔的字符串。我想将所有为 true false 的值替换为 yes 和 no 而不必执行 xsl:when true then yes otherwise 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>&amp;Gender=</xsl:text>
    <xsl:value-of select="//MainNode/P1/P2/Info/Gender"/>
    <xsl:text>&amp;Married=</xsl:text>
    <xsl:value-of select="//MainNode/P1/P2/Info/Married"/>
    <xsl:text>&amp;Comments=</xsl:text>
    <xsl:value-of select="//MainNode/P1/P2/History/Comments"/>
    <xsl:text>&amp;Medication=</xsl:text>
    <xsl:value-of select="//MainNode/P1/P2/History/Medication"/>
   </xsl:template>
</xsl:stylesheet>

输出: name=Sam&Gender=Male&Married=false&Comments=some Comments&Medication=true

我希望输出中的所有 'true' 和 'false' 都替换为 'yes' 和 'no' 输出: name=Sam&Gender=Male&Married=no&Comments=some Comments&Medication=yes

现在我在 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>&amp;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>&amp;Married=</xsl:text>
    <xsl:value-of select="//MainNode/P1/P2/Info/Married"/>
    <xsl:text>&amp;Comments=</xsl:text>
    <xsl:value-of select="//MainNode/P1/P2/History/Comments"/>
    <xsl:text>&amp;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' 块

请帮我解决这个问题。对 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()"/>

我会使用一个函数:

<xsl:value-of select="my:bool(//MainNode/P1/P2/Info/Gender)"/>

然后

<xsl:function name="my:bool" as="xs:string">
  <xsl:param name="s" as="xs:string">
  <xsl:sequence select="if ($s='true') then 'yes' else 'no'"/>
</xsl:function>

另请注意,XSLT 3.0 允许您:

<xsl:for-each select="//MainNode/P1/P2/Info">
  <xsl:text>name={Name}&amp;Gender={my:bool(Gender)}&amp;Married={my:bool(Married)}</xsl:text>
</xsl:for-each>
<xsl:for-each select="//MainNode/P1/P2/History">
  <xsl:text>comments={Comments}&amp;Medication={my:bool(Medication)}</xsl:text>
</xsl:for-each>