我对XSLT有一个非常复杂的要求。我需要计算字段的值,该值等于其值的子字符串。我能算数,但问题是背景。请参阅以下示例:
<root>
<Manager>TC4:12345</Manager>
<Staff>
<employee>TC3:22222</employee>
<employee>TC3:11111</employee>
<employee>TC3:33333</employee>
</Staff>
</root>
<root>
<Manager>TC4:67890</Manager>
<Staff>
<employee>TC3:44444</employee>
<employee>TC3:55555</employee>
<employee>TC3:66666</employee>
</Staff>
</root>
输出应该是这样的:
<Manager>
<Seniors>
<Count>3</Count>
</Seniors>
</Manager>
<Manager>
<Seniors>
<Count>3</Count>
</Seniors>
</Manager>
第一位经理计算了TC3角色的员工数量,然后第二位经理计算了TC3角色的员工数量。我尝试过这样做,但我最终在第一个上下文中有6个,在第二个上下文中有6个而不是3和3个。
这是我的代码
<xsl:value-of select="count(//root/Staff/employee[substring(.,1,3) = 'TC3'])">
答案 0 :(得分:0)
写一个模板match="root"
,然后在里面你可以使用<xsl:value-of select="count(Staff/employee[substring(.,1,3) = 'TC3'])"/>
。
答案 1 :(得分:0)
在这里(假设您的所有xml都包含在body标签中):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="body">
<xsl:for-each select="root">
<Manager>
<Seniors>
<Count><xsl:value-of select="count(./Staff/employee[substring(.,1,3) = 'TC3'])" /></Count>
</Seniors>
</Manager>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
以下是我使用的来源:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<root>
<Manager>TC4:12345</Manager>
<Staff>
<employee>TC3:22222</employee>
<employee>TC3:11111</employee>
<employee>TC3:33333</employee>
</Staff>
</root>
<root>
<Manager>TC4:67890</Manager>
<Staff>
<employee>TC3:44444</employee>
<employee>TC3:55555</employee>
<employee>TC3:66666</employee>
</Staff>
</root>
</body>
这是输出:
<Manager>
<Seniors>
<Count>3</Count>
</Seniors>
</Manager><Manager>
<Seniors>
<Count>3</Count>
</Seniors>
</Manager>