Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 将相邻注释转换为XSD中的xs:annotation_Xml_Xslt_Xsd_Xml Comments - Fatal编程技术网

Xml 将相邻注释转换为XSD中的xs:annotation

Xml 将相邻注释转换为XSD中的xs:annotation,xml,xslt,xsd,xml-comments,Xml,Xslt,Xsd,Xml Comments,我有一个包含大量注释的XSD,应该将其移动到前面的xs:simpleType或xs:complexTypes的a xs:annotation/xs:documentation部分。如何使用XSLT V1.0或XSLT V2.0移动这些注释 输入XSD示例: <?xml version="1.0" encoding="UTF-8"?> <xs:simpleType name="ligula"> <xs:restriction base="xs:string"/

我有一个包含大量注释的XSD,应该将其移动到前面的xs:simpleType或xs:complexTypes的a xs:annotation/xs:documentation部分。如何使用XSLT V1.0或XSLT V2.0移动这些注释

输入XSD示例:

<?xml version="1.0" encoding="UTF-8"?>
<xs:simpleType name="ligula">
    <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="ultricies">
    <xs:restriction base="xs:integer"/>
</xs:simpleType>
<!--
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
Aenean commodo ligula eget dolor. Aenean massa. Cum 
-->
<xs:simpleType name="neque">
    <xs:restriction base="xs:string"/>
</xs:simpleType>
<xs:simpleType name="tincidunt">
    <xs:restriction base="xs:string"/>
</xs:simpleType>
<!-- ligula, porttitor eu, consequat vitae, eleifend ac, enim.  -->
<!-- Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed.  -->
<!-- Integer tincidunt. Cras dapibus. Vivamus elementum  -->
</xs:schema>

输出XSD应为:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="ligula">
        <xs:restriction base="xs:string"/>
    </xs:simpleType>
    <xs:simpleType name="ultricies">
        <xs:annotation>
            <xs:documentation>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
            Aenean commodo ligula eget dolor. Aenean massa. Cum</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:integer"/>
    </xs:simpleType>
    <xs:simpleType name="neque">
        <xs:restriction base="xs:string"/>
    </xs:simpleType>
    <xs:simpleType name="tincidunt">
        <xs:annotation>
            <xs:documentation>ligula, porttitor eu, consequat vitae, eleifend ac, enim</xs:documentation>
            <xs:documentation>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed.</xs:documentation>
            <xs:documentation>Integer tincidunt. Cras dapibus. Vivamus elementum</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string"/>
    </xs:simpleType>
</xs:schema>

Lorem ipsum dolor sit amet,是一位杰出的领导者。
埃尼安·康莫多·利古拉·埃吉特·多洛。埃尼安·马萨。附有
ligula、porttitor eu、consequat vitae、eleifend ac、enim
我坐在那里,或者我坐在那里。杜伊斯·利奥。塞德。
整数tincidunt。克拉斯·达皮布斯。元素活体

以下样式表将每个注释节点转换为
xs:documentation
元素。更准确地说,我将紧跟在
xs:simpleType
xs:complexType
元素之后的注释节点序列存储在一个变量中:

<xsl:variable name="com" select="following-sibling::comment() except following-sibling::*[local-name() = 'simpleType' or local-name() = 'complexType'][1]/following-sibling::comment()"/>
<xs:annotation>
  <xsl:for-each select="$com">
     <xs:documentation>
        <xsl:value-of select="normalize-space(.)"/>
     </xs:documentation>
  </xsl:for-each>
</xs:annotation>
然后,插入
xs:annotation
元素,并将每个注释节点的字符串值转换为
xs:documentation
元素:

<xsl:variable name="com" select="following-sibling::comment() except following-sibling::*[local-name() = 'simpleType' or local-name() = 'complexType'][1]/following-sibling::comment()"/>
<xs:annotation>
  <xsl:for-each select="$com">
     <xs:documentation>
        <xsl:value-of select="normalize-space(.)"/>
     </xs:documentation>
  </xsl:for-each>
</xs:annotation>

其他详情:

  • 如果元素节点由模板匹配,则必须首先将所有属性添加到结果树中。将子元素添加到结果树后,不能再添加属性节点
  • 还有另一个模板来匹配注释节点,但什么也不做,因为它们都是在另一个模板中处理的
样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>    

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="xs:simpleType|xs:complexType">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:variable name="com" select="following-sibling::comment() except following-sibling::*[local-name() = 'simpleType' or local-name() = 'complexType'][1]/following-sibling::comment()"/>
            <xsl:if test="$com">
                <xs:annotation>
                    <xsl:for-each select="$com">
                        <xs:documentation>
                            <xsl:value-of select="normalize-space(.)"/>
                        </xs:documentation>
                    </xsl:for-each>
                </xs:annotation>
            </xsl:if>

            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="comment()"/>

</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:simpleType name="ligula">
      <xs:restriction base="xs:string"/>
   </xs:simpleType>
   <xs:simpleType name="ultricies">
      <xs:annotation>
         <xs:documentation>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:integer"/>
   </xs:simpleType>
   <xs:simpleType name="neque">
      <xs:restriction base="xs:string"/>
   </xs:simpleType>
   <xs:simpleType name="tincidunt">
      <xs:annotation>
         <xs:documentation>ligula, porttitor eu, consequat vitae, eleifend ac, enim.</xs:documentation>
         <xs:documentation>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed.</xs:documentation>
         <xs:documentation>Integer tincidunt. Cras dapibus. Vivamus elementum</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:string"/>
   </xs:simpleType>
</xs:schema>

XML输出

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>    

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="xs:simpleType|xs:complexType">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:variable name="com" select="following-sibling::comment() except following-sibling::*[local-name() = 'simpleType' or local-name() = 'complexType'][1]/following-sibling::comment()"/>
            <xsl:if test="$com">
                <xs:annotation>
                    <xsl:for-each select="$com">
                        <xs:documentation>
                            <xsl:value-of select="normalize-space(.)"/>
                        </xs:documentation>
                    </xsl:for-each>
                </xs:annotation>
            </xsl:if>

            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="comment()"/>

</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:simpleType name="ligula">
      <xs:restriction base="xs:string"/>
   </xs:simpleType>
   <xs:simpleType name="ultricies">
      <xs:annotation>
         <xs:documentation>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:integer"/>
   </xs:simpleType>
   <xs:simpleType name="neque">
      <xs:restriction base="xs:string"/>
   </xs:simpleType>
   <xs:simpleType name="tincidunt">
      <xs:annotation>
         <xs:documentation>ligula, porttitor eu, consequat vitae, eleifend ac, enim.</xs:documentation>
         <xs:documentation>Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed.</xs:documentation>
         <xs:documentation>Integer tincidunt. Cras dapibus. Vivamus elementum</xs:documentation>
      </xs:annotation>
      <xs:restriction base="xs:string"/>
   </xs:simpleType>
</xs:schema>

Lorem ipsum dolor sit amet,是一位杰出的领导者。埃尼安·康莫多·利古拉·埃吉特·多洛。埃尼安·马萨。附有
利古拉,波尔蒂托欧盟,康塞卡特维塔,埃利芬德ac,埃尼姆。
我坐在那里,或者我坐在那里。杜伊斯·利奥。塞德。
整数tincidunt。克拉斯·达皮布斯。元素活体

如果要进行更改,请联机尝试此解决方案。

尝试

<xsl:template match="xs:schema">
  <xsl:copy>
    <xsl:for-each-group group-starting-with="xs:simpleType|xs:complexType">
      <xsl:apply-templates select="." mode="include-comments">
        <xsl:with-param name="comments" select="current-group()/self::comment()"/>
      </
    </
  </
</

<xsl:template match="*" mode="include-comments">
  <xsl:param name="comments" as="comment()*"/>
  <xsl:copy>
    <xs:documentation>
      <xsl:for-each select="$comments">
        <xs:annotation><xsl:value-of select="."/>
      </xsl:for-each>
    </
  </
</


你怎么知道评论的归属?例如,XSLT代码应该如何决定是否将第二组注释转换为
neque
xs:annotation
tincidunt
?此XSD中有一个逻辑结构。后续注释始终属于前面的simpleType或complexType。例如,在本例中,以
开头的注释是一个可以解决的有趣的编程问题。详见我的答案。两个方案都有效。非常感谢您的快速回复。