2010-01-11 37 views
1

我第一次使用xslt。我必须根据一些标准创建一个元素。XSLT转换问题 - 如何填充属性

这里是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> 

最终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> 

业务规则:AllUsers的元素有1个属性isFromHR - 它的值w'd是如果为true值类型属性为<FirstUser><SecondUser> 为HR否则为假

如何填充isFromHR的值?剩下的xml创建完成了。

在此先感谢。

+0

XML示例格式不正确。 – jelovirt 2010-01-11 13:05:24

回答

2

什么

<AllUsers isFromHR="{ count((//FirstUser | //SecondUser)[@type='HR']) &gt; 0 }"> 

<AllUsers> 
    <xsl:attribute 
     name="isFromHR" 
     value="count((//FirstUser | //SecondUser)[@type='HR']) &gt; 0" /> 
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> 
+0

很多很多谢谢SIR ......这个技巧奏效了。 我会非常高兴如果我可以得到一些软拷贝材料来阅读,因为我是新的xslt世界。 关于各种xslt函数和变量的使用,我还有几个疑问...所以如果你能给我提供一些软拷贝来学习......对我来说很好。 再次很多谢谢:-) – Amit 2010-01-11 13:36:36

+0

查看XSLT http://www.w3schools.com/xsl/default.asp和XPATH http://www.w3schools.com/xpath/default.asp关于http: //w3schools.com,开始。 Dave Pawson维护一个非常有用的网站:http://www.dpawson.co.uk/xsl/常见问题解答提取许多常见问题,解释和代码片段。 – 2010-01-12 03:36:34

0
<AllUsers isFromHR="{descendant::@type = 'HR'}"> 
0

输入XML缺少的元素:

<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> 

在任何情况下,我会去:

<AllUsers isFromHR="{//FirstUser/User/@Type = 'HR' or //SecondUser/User/@Type = 'HR'}"> 

这给了你“真”的情况下,无论是FirstUser或SecondUser包含类型“HR”的用户。