使用模式识别的XSLT进行代码覆盖警告

问题描述

如果有任何代码覆盖警告,我很感兴趣-基本上是警告说代码行或代码段不能进行类型检查,或者有确保这些段不存在的任何基本技术。

为了说明我有一个XSD。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

  <xs:complexType name="SQUARETYPE">
    <xs:sequence>
      <xs:element name="contains">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="SQUARE"/>
            <xs:element ref="TRIANGLE"/>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="kind"/>
    <xs:attribute name="width" type="xs:int"/>
    <xs:attribute name="x" type="xs:int"/>
    <xs:attribute name="y" type="xs:int"/>
  </xs:complexType>
  <xs:complexType name="FILLEDSQUARETYPE">
    <xs:sequence>
      <xs:element name="contains">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="SQUARE"/>
            <xs:element ref="TRIANGLE"/>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="kind"/>

    <xs:attribute name="colour" type="xs:string"/>
    <xs:attribute name="width" type="xs:int"/>
    <xs:attribute name="x" type="xs:int"/>
    <xs:attribute name="y" type="xs:int"/>
  </xs:complexType>
  <xs:complexType name="TRIANGLETYPE">
    <xs:sequence>
      <xs:element name="contains">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element ref="SQUARE"/>
            <xs:element ref="TRIANGLE"/>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="rotation" type="xs:int"/>
    <xs:attribute name="x" type="xs:int"/>
    <xs:attribute name="y" type="xs:int"/>
  </xs:complexType>
  <xs:element name="SQUARE">
    <xs:alternative test="@kind = 'FILLEDSQUARETYPE'" type="FILLEDSQUARETYPE"/>
    <xs:alternative test="@kind = 'SQUARETYPE'" type="SQUARETYPE"/>
    <xs:alternative type="xs:error"/>
  </xs:element>
  <xs:element name="TRIANGLE">
    <xs:alternative type="TRIANGLETYPE"/>
  </xs:element>
  <xs:element name="rootShape">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="SQUARE"/>
        <xs:element ref="TRIANGLE"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

一个xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="xs msxsl"
    version="2.0">

  <xsl:output method="xml" indent="yes" encoding="UTF-8" version="1.0"/>

  <xsl:import-schema schema-location="MessingAbout.xsd"/>
  <xsl:template match="/">
    <xsl:apply-templates select="schema-element(SQUARE)"/>
  </xsl:template>
  <xsl:template match="element(SQUARE,FILLEDSQUARETYPE)">
    <foo>
      <xsl:value-of select="@colour"/>
    </foo>
  </xsl:template>
</xsl:stylesheet>

我认为这具有100%的覆盖率(尽管未选中“ /”的初始匹配项)。

如果我要修改xslt这样说。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="xs msxsl"
    version="2.0">

  <xsl:output method="xml" indent="yes" encoding="UTF-8" version="1.0"/>

  <xsl:import-schema schema-location="MessingAbout.xsd"/>
  <xsl:template match="/">
    <xsl:apply-templates select="SQUARE"/>
  </xsl:template>
  <xsl:template match="element(SQUARE,FILLEDSQUARETYPE)">
    <foo>
      <xsl:value-of select="@colour"/>
    </foo>
  </xsl:template>
</xsl:stylesheet>

然后在那里

    <xsl:apply-templates select="SQUARE"/>

未选中,我可以将“ SQUARE”替换为“ SQUARE1”,并且不会选中(可以理解)。

是否可以打开一些警告我的设置?因此,我可以强制要求始终对XSLT进行类型检查? (即使这排除了某些“有效”但非类型可检查的程序(这是常见的折衷方案))。

解决方法

都进行了类型检查,但是静态类型检查的彻底性取决于可用的类型信息。因此,它不是黑白的0%或100%。基本上,在编译时,处理器会进行所有可能的类型检查,并将其余的推迟到运行时。撒克逊人进行了一些检查,即使从理论上讲它也不会尝试,例如,涉及到涉及子轴,子轴和后代的轴的任何事情。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...