XSLT 2.0 if condition

谁说我不能喝 提交于 2019-12-13 23:32:05

问题


Need help on XSLT 2.0 transformation.

Input xml :

<Employee>
<Post>Manager</Post>
</Employee>

pseudo code :

if(Employee/Post = 'Manager') then
Associate/High = 'Band'
else
Associate/Low = 'Band'

Output xml :

<Associate>
<High>Band</High>
</Associate>

<Associate>
<Low>Band</Low>
</Associate>

回答1:


Construct an element dynamically with xsl:element. Other than that, your pseudo code is already pretty accurate.

XSLT Stylesheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="Employee">
      <Associate>
          <xsl:element name="{if (Post = 'Manager') then 'High' else 'Low'}">
              <xsl:value-of select="'Band'"/>
          </xsl:element>
      </Associate>
    </xsl:template>

</xsl:transform>

XML Output

<Associate>
   <High>Band</High>
</Associate>



回答2:


I would use a template

<xsl:template match="Employee">
  <Associate>
    <xsl:apply-templates/>
  </Associate>
</xsl:template>

and then

<xsl:template match="Employee/Post[. = 'Manager']">
  <High>Band</High>
</xsl:template>

<xsl:template match="Employee/Post[not(. = 'Manager')]">
  <Low>Band</Low>
</xsl:template>


来源:https://stackoverflow.com/questions/29633276/xslt-2-0-if-condition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!