XSLT 转换以验证 XML 文档中的规则 [英] XSLT Transformation to validate rules in XML document

查看:33
本文介绍了XSLT 转换以验证 XML 文档中的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XSLT 转换中的另一个新手问题.(我之前也问过类似的问题,但在这种情况下,XML 具有不同的格式).

Another Newbie question in XSLT transformation. (I have asked similar question before, but in this case the XML has different formats).

我有一个 xml 文档,需要使用 xslt 进行大量验证.这将使用 xsltproc 工具完成.

I've a xml document that I need to do bunch of validations using xslt. This will be done using the xsltproc tool.

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

我只需要对 标签进行以下验证即我不关心公司位置标签等.请注意 xml 文档具有 标签.

I need to do the following validations only for the <Manager> and <Employee_List> tags i.e I do not care about Company Location tags etc. Please note that xml document has <company> <Employee_List> tags.

  1. 经理下的员工人数应 >= mincount.
  2. 经理下属员工的等级应<经理的级别.
  3. 经理下属的所有员工都应该在同一位置.(我需要额外的检查,比如员工的属性 p1 和 p2 也应该匹配).

TIA 并感谢您的帮助!!

TIA and appreciate any help!!

要验证的新规则(于 12 年 3 月 5 日添加).1. p1 属性必须是 ALPHA 或 BETA(字符串),不能是其他任何内容.此 xsl 适用于单个员工,而不适用于多个员工.

New rules to be validated (added on 3/5/12). 1. The p1 attribute must be either ALPHA or BETA (string), it cannot be anything else. This xsl works for a single employee, not for multiple employees.

    <xsl:apply-templates mode="rule5" select=
  "*/*/Manager[not(key('kEmpById', Employee/@id)/@p1 = 'ALPHA' or
               key('kEmpById', Employee/@id)/@p1 = 'BETA')
              ]
  "/>

  1. 下面的员工 ID 应该是唯一的.我试过这个 xsl,但它不起作用:(

  1. The employee id under should be unique. I tried this xsl, but it doesn't work :(

<xsl:apply-templates mode="rule6" select=

"//Manager[key('kEmpById', Employee[2]/@id)/@id=键('kEmpById',员工/@id)/@id]"/>

"//Manager[key('kEmpById', Employee[2]/@id)/@id = key('kEmpById', Employee/@id)/@id ] "/>

TIA!

推荐答案

类似于以下内容:

<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 文档时(经过编辑以消除其严重的格式错误并引入所有可能的违规行为):

When this transformation is applied on the provided XML document (edited to remove its severe malformedness and to introduce all possible violations):

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

这篇关于XSLT 转换以验证 XML 文档中的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆