报告相应模式

时间:2017-03-08 14:24:25

标签: xml xslt xsd saxon xslt-3.0

在这个主题中,我想要比我自己提供更多的脑细胞。我想重构我的XSD(v1.0),具体取决于实际XML实例中使用/未使用的元素(仅限单个命名空间)。让我们建立一个小的场景

我只有针对相应架构的有效XML文档:

<body>
    <h1>Heading 1</h1>
    <p>paragraph</p>
    <p><bold>bold</bold>paragraph<italic>italic</italic></p>
</body>

XSD验证:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="body">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element ref="h1"/>
        <xs:element ref="h2"/>
        <xs:element ref="p"/>
        <xs:element ref="span"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="h1" type="xs:string"/>
  <xs:element name="h2" type="xs:string"/>
  <xs:element name="p">
    <xs:complexType mixed="true">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="bold"/>
        <xs:element ref="italic"/>
        <xs:element ref="underline"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="span">
    <xs:complexType mixed="true">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="bold"/>
        <xs:element ref="italic"/>
        <xs:element ref="underline"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="bold" type="xs:NCName"/>
  <xs:element name="italic" type="xs:NCName"/>
  <xs:element name="underline" type="xs:NCName"/>
</xs:schema>

在此基础上,我想创建一个报告(通过XSLT [2.0,3.0通过SAXON EE 9.6.0.5可用])关于哪些元素(标签+属性) NOT 在我的XML实例,但可以在我的XSD中使用。

简化伪待办事项/从头开始:

  • 在我的XSD中搜索所有//xs:element[@name](报告v2.0中的属性)。
  • 在我的XML中搜索所有*
  • “比较”它

问题:

在可爱的XSLT社区中,是否有关于此话题的超越我的视野?

如何以良好的方式存储和比较它?

通过XSLT 3.0使用xsl:map?存储路径[/body/h1/body/p]并比较这些路径? (棘手:从模式中获取正确的路径,处理所有定义方式,如xs:group ref="..."或通过complexTypes等。)

[AddOn:也许我必须将它扩展到我的XML中的祖先元素的上下文中。在示例中,我可能想要 区分//p/underline//span/underline。]

<xsl:message>please write your thoughts open minded. I don't request for fully functional code!</xsl:message>

3 个答案:

答案 0 :(得分:1)

查看http://saxonica.com/html/documentation/functions/saxon/type.htmlhttp://saxonica.com/html/documentation/functions/saxon/schema.html以获取节点上Saxon EE中的架构类型信息,这有望将您的实例与架构进行比较。我从来没有使用过,所以我不确定你会得到多远,我相信如果你在问题中添加,Michael Kay会在适当的时候给你一些更好的见解。

答案 1 :(得分:1)

您的帖子提醒我com.saxonica.Validate命令的一个选项:通过指定-stats:report.xml,您应该获得有关实例文档中架构组件使用情况的报告。它似乎在9.7中没有工作(我已经提出了一个错误),但是在9.5中你得到了一个表格的报告:

<schemaCoverage>
   <component kind="element" namespace="" name="PUB-DATE" count="6"/>
   <component kind="complexType" namespace="" name="weightType" count="6"/>
   <component kind="element" namespace="" name="PUBLISHER" count="6"/>
   <component kind="element" namespace="" name="AUTHOR" count="6"/>
   <component kind="element" namespace="" name="DIMENSIONS" count="6"/>
   <component kind="simpleType" namespace="" name="languageType" count="6"/>
   <component kind="element" namespace="" name="QUANTITY" count="6"/>
   <component kind="element" namespace="" name="CATEGORY" count="3"/>
   <component kind="complexType"
              namespace="http://ns.saxonica.com/anonymous-type"
              name="CATEGORIES_anonymous_type_1_at_line_23_of_books.xsd"
              count="1"/>
   <component kind="element" namespace="" name="LANGUAGE" count="6"/>
   <component kind="element" namespace="" name="PAGES" count="6"/>
   <component kind="complexType" namespace="" name="moneyType" count="6"/>
   <component kind="element" namespace="" name="ISBN" count="6"/>
   <component kind="simpleType"
              namespace="http://www.w3.org/2001/XMLSchema"
              name="IDREF"
              count="6"/>
   <component kind="simpleType"
              namespace="http://www.w3.org/2001/XMLSchema"
              name="ID"
              count="3"/>
   <component kind="complexType"
              namespace="http://ns.saxonica.com/anonymous-type"
              name="BOOKS_anonymous_type_1_at_line_14_of_books.xsd"
              count="1"/>
   <component kind="element" namespace="" name="CATEGORIES" count="1"/>
   <component kind="simpleType" namespace="" name="ISBNType" count="6"/>
   <component kind="simpleType"
              namespace="http://www.w3.org/2001/XMLSchema"
              name="string"
              count="22"/>
   <component kind="complexType"
              namespace="http://ns.saxonica.com/anonymous-type"
              name="ITEM_anonymous_type_1_at_line_39_of_books.xsd"
              count="6"/>
   <component kind="simpleType" namespace="" name="weightUnitType" count="6"/>
   <component kind="complexType"
              namespace="http://ns.saxonica.com/anonymous-type"
              name="CATEGORY_anonymous_type_1_at_line_31_of_books.xsd"
              count="3"/>
   <component kind="simpleType"
              namespace="http://www.w3.org/2001/XMLSchema"
              name="date"
              count="6"/>
   <component kind="simpleType"
              namespace="http://www.w3.org/2001/XMLSchema"
              name="integer"
              count="12"/>
   <component kind="element" namespace="" name="TITLE" count="6"/>
   <component kind="element" namespace="" name="PRICE" count="6"/>
   <component kind="element" namespace="" name="WEIGHT" count="6"/>
   <component kind="complexType" namespace="" name="dimensionsType" count="6"/>
   <component kind="element" namespace="" name="ITEM" count="6"/>
   <component kind="simpleType" namespace="" name="lengthUnitType" count="6"/>
   <component kind="element" namespace="" name="BOOKS" count="1"/>
</schemaCoverage>

这似乎正是您所寻找的。

答案 2 :(得分:1)

我为XSLT 3.0测试套件做了一个类似的练习。你可以在这里找到样式表:

https://dvcs.w3.org/hg/xslt30-test/file/24e8b98b044b/tests/misc/catalog/catalog-007.xsl

需要两个输入:

(a)使用带有-scmout选项的com.saxonica.Validate生成的SCM文件,应用于schema-for-xslt30。 SCM文件是已编译模式的表示,从XSLT分析比原始源模式更容易

(b)测试套件中的一组非错误样式表,通过递归搜索测试元数据目录获得。

它提取模式允许的元素名称/属性名称对的集合,然后提取样式表中实际存在的元素名称/属性名称对的集合(在每种情况下都被过滤,例如仅考虑XSLT命名空间中的元素)。然后,它会比较两个列表,并报告架构允许的测试样式表中不存在的任何对,以及模式中不允许的测试样式表中存在的任何对。仅当两个列表都为空时,测试才会通过。