XSLT转换中的另一个新手问题。 (之前我曾问过类似的问题,但在这种情况下,XML有不同的格式)。
我有一个xml文档,我需要使用xslt进行大量验证。这将使用xsltproc工具完成。
<?xml version="1.0" ?>
<Company id="1" ...>
<Name>blah</Name>
<Location>
<Address>zzz</Address>
<City>aaa</City>
.....
</Location>
....
<Manager id="m1" mincount="4" grade="10"...>
<Employee id="e1"/>
<Employee id="e2"/>
.....
</Manager>
.....
</Company>
<Employee_List>
<Employee id="e1" grade="9" Location="New York" p1="value" p2="value"....... />
<Employee id="e2" grade="8" Location="New York" p1="value" p2="value"....... />
......
</Employee_List>
我只需要为<Manager>
和<Employee_List>
标记执行以下验证
即我不关心公司位置标签等。请注意,xml文档包含<company>
<Employee_List>
个标签。
TIA,感谢任何帮助!!
要验证的新规则(于2012年3月3日添加)。 1. p1属性必须是ALPHA或BETA(字符串),它不能是其他任何东西。此xsl适用于单个员工,而不适用于多个员工。
<xsl:apply-templates mode="rule5" select=
"*/*/Manager[not(key('kEmpById', Employee/@id)/@p1 = 'ALPHA' or
key('kEmpById', Employee/@id)/@p1 = 'BETA')
]
"/>
员工ID不应该是唯一的。我试过这个xsl,但它不起作用:(
<xsl:apply-templates mode="rule6" select=
“ / /经理[key('kEmpById',Employee [2] / @ id)/ @ id = key('kEmpById',Employee / @ id)/ @ id ] “/&GT;
TIA!
答案 0 :(得分:1)
类似以下内容:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kEmpById" match="Employee" use="@id"/>
<xsl:template match="/">
<xsl:apply-templates mode="rule1" select=
"*/*/Manager[@mincount > count(Employee)]"/>
<xsl:apply-templates mode="rule2" select=
"*/*/Manager[key('kEmpById', Employee/@id)/@grade > @grade]"/>
<xsl:apply-templates mode="rule3" select=
"*/*/Manager[key('kEmpById', Employee[2]/@id)/@Location
!=
key('kEmpById', Employee/@id)/@Location
]
"/>
</xsl:template>
<xsl:template match="Manager" mode="rule1">
Manager id = "<xsl:value-of select="@id"/>" has too few employees.
</xsl:template>
<xsl:template match="Manager" mode="rule2">
Manager id = "<xsl:value-of select="@id"/>" has employees with higher grade than his own.
</xsl:template>
<xsl:template match="Manager" mode="rule3">
Manager id = "<xsl:value-of select="@id"/>" has employees at different locations.
</xsl:template>
</xsl:stylesheet>
对提供的XML文档应用此转换(已编辑以删除其严重的格式错误并引入所有可能的违规行为):
<t>
<Company id="1" >
<Name>blah</Name>
<Location>
<Address>zzz</Address>
<City>aaa</City>
</Location>
<Manager id="m1" mincount="4" grade="10">
<Employee id="e1"/>
<Employee id="e2"/>
</Manager>
</Company>
<Employee_List>
<Employee id="e1" grade="11" Location="New York" p1="value" p2="value" />
<Employee id="e2" grade="8" Location="Pittsburgh" p1="value" p2="value" />
</Employee_List>
</t>
产生了想要的正确结果:
Manager id = "m1" has too few employees.
Manager id = "m1" has employees with higher grade than his own.
Manager id = "m1" has employees at different locations.