XSLT Transformation issue - how to populate attribute
I am using xslt first time. I have to 开发者_如何转开发create an element based on some criteria.
here is i/p xml:
<FirstUser>
<User id="2" description="ABC" Type="HR"/>
</FirstUser>
<SecondUser>
<User id="3" description="ABC" Type="HR"/>
<User id="4" description="xyz" Type="Admin"/>
<User id="5" description="LMN" Type="Payroll"/>
</SecondUser>
Final O/P
<AllUsers isFromHR='true'>
<User id="2" description="ABC" Type="HR"/>
<User id="3" description="ABC" Type="HR"/>
<User id="4" description="xyz" Type="Admin"/>
<User id="5" description="LMN" Type="Payroll"/>
</AllUsers>
Business Rule: AllUsers element has 1 attribute isFromHR -
Its value w'd be true if value in type attribute of <FirstUser>
or <SecondUser>
is HR else it will be false
How to populate the value of isFromHR ? Rest of xml creation I am done.
Thanks in advance.
What about
<AllUsers isFromHR="{ count((//FirstUser | //SecondUser)[@type='HR']) > 0 }">
or
<AllUsers>
<xsl:attribute
name="isFromHR"
value="count((//FirstUser | //SecondUser)[@type='HR']) > 0" />
<xsl:template match="/">
<AllUsers>
<xsl:attribute name="isFromHR">
<xsl:choose>
<xsl:when test="//*[local-name(.)='FirstUser' or local-name(.)='SecondUser']/*/@Type[.='HR']">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates />
</AllUsers >
</xsl:template>
<AllUsers isFromHR="{descendant::@type = 'HR'}">
The input XML is missing elements:
<FirstUser>
<User id="2" description="ABC" Type="HqR"/>
</FirstUser>
<SecondUser>
<User id="3" description="ABC" Type="H2R"/>
<User id="4" description="xyz" Type="Admin"/>
<User id="5" description="LMN" Type="Payroll"/>
</SecondUser>
In any case, I'd go for:
<AllUsers isFromHR="{//FirstUser/User/@Type = 'HR' or //SecondUser/User/@Type = 'HR'}">
This gives you "true" in case either FirstUser or SecondUser contains a User of type 'HR'.
精彩评论