可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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>