根据XSD中的属性值创建所需的XML元素

前端之家收集整理的这篇文章主要介绍了根据XSD中的属性值创建所需的XML元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的要求是有一个XSD文件,它根据属性值检查元素.我能够将XSD编写到可以限制Application / @Type的属性值的位置.任何人都可以帮我完成XSD文件,我可以根据Application / @Type属性制作一些所需的元素吗?

我要实现

>只有当Application / @ Type为“Batch”时才需要PackageArg
>仅当Application / @ Type为“Service”时才需要版本
>仅当Application / @ Type为Web“或”Service“时才需要项目

XML文件

  1. <Applications>
  2. <Application Name="ConfigManagement" Type="Web">
  3. <ProjectDirName>ConfigManagement</ProjectDirName>
  4. <Project>Web.ConfigManagement.csproj</Project>
  5. <OutputDirName>ConfigManagement</OutputDirName>
  6. </Application>
  7. <Application Name="Util" Type="Web">
  8. <ProjectDirName>Web</ProjectDirName>
  9. <Project>Web.csproj</Project>
  10. <OutputDirName>Util</OutputDirName>
  11. </Application>
  12. <Application Name="ConfigService" Type="Service">
  13. <ProjectDirName>WebServices\ConfigService</ProjectDirName>
  14. <Project>ConfigService.csproj</Project>
  15. <Version>2015\04</Version>
  16. <OutputDirName>ConfigService</OutputDirName>
  17. </Application>
  18. <Application Name="DeliverEmail" Type="Batch">
  19. <ProjectDirName>\Batch\DeliverEmail</ProjectDirName>
  20. <PackageArg>Release</PackageArg>
  21. <OutputDirName>Tidal\DeliverEmail</OutputDirName>
  22. </Application>
  23. </Applications>

XSD文件

  1. <xs:element name="Applications" maxOccurs="1">
  2. <xs:complexType>
  3. <xs:sequence>
  4. <xs:element name="Application" maxOccurs="unbounded" minOccurs="1">
  5. <xs:complexType>
  6. <xs:all>
  7. <xs:element type="xs:string" name="ProjectDirName"/>
  8. <xs:element type="xs:string" name="Project" minOccurs="0"/>
  9. <xs:element type="xs:string" name="Version" minOccurs="0"/>
  10. <xs:element type="xs:string" name="PackageArg" minOccurs="0"/>
  11. <xs:element type="xs:string" name="OutputDirName"/>
  12. </xs:all>
  13. <xs:attribute type="xs:string" name="Name" use="optional"/>
  14. <xs:attribute name="Type" use="required">
  15. <xs:simpleType>
  16. <xs:restriction base="xs:string">
  17. <xs:enumeration value="Web"/>
  18. <xs:enumeration value="Batch"/>
  19. <xs:enumeration value="Service"/>
  20. </xs:restriction>
  21. </xs:simpleType>
  22. </xs:attribute>
  23. </xs:complexType>
  24. </xs:element>
  25. </xs:sequence>
  26. </xs:complexType>
  27. </xs:element>
更新:OP已编辑问题以删除xs:assert的使用,并在评论中声明验证必须在C#中进行. OP问题的答案现在变为:

您不能使用XSD 1.0强制执行基于属性值改变元素要求的约束,并且Microsoft不支持XSD 1.1,因此您必须放松约束或在XSD之外检查它们.

原XSD 1.1的答案

(留作未来读者的利益)

你很亲密,但你的断言,

  1. <xs:assert test="count(./PackageArg[@type eq 'Batch']) eq 1"/>

当它应该在Application上测试@Type时,对PackageArg上的@type进行测试.

以下XSD将验证您的XML并强制执行依赖于属性的要求:

XSD 1.1

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3. xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  4. vc:minVersion="1.1">
  5. <xs:element name="Applications">
  6. <xs:complexType>
  7. <xs:sequence>
  8. <xs:element name="Application" maxOccurs="unbounded" minOccurs="1">
  9. <xs:complexType>
  10. <xs:all>
  11. <xs:element type="xs:string" name="ProjectDirName"/>
  12. <xs:element type="xs:string" name="Project" minOccurs="0"/>
  13. <xs:element type="xs:string" name="Version" minOccurs="0"/>
  14. <xs:element type="xs:string" name="PackageArg" minOccurs="0"/>
  15. <xs:element type="xs:string" name="OutputDirName"/>
  16. </xs:all>
  17. <xs:attribute type="xs:string" name="Name" use="optional"/>
  18. <xs:attribute name="Type" use="required">
  19. <xs:simpleType>
  20. <xs:restriction base="xs:string">
  21. <xs:enumeration value="Web"/>
  22. <xs:enumeration value="Batch"/>
  23. <xs:enumeration value="Service"/>
  24. </xs:restriction>
  25. </xs:simpleType>
  26. </xs:attribute>
  27. <xs:assert test="PackageArg or @Type != 'Batch'"/>
  28. <xs:assert test="Version or @Type != 'Service'"/>
  29. <xs:assert test="Project or not(@Type = 'Web' or @Type = 'Service')"/>
  30. </xs:complexType>
  31. </xs:element>
  32. </xs:sequence>
  33. </xs:complexType>
  34. </xs:element>
  35. </xs:schema>

请注意,Microsoft不支持XSD 1.1. (您已使用’msxml’标记了您的问题).

猜你在找的XML相关文章