如何从xmlschema(xsd)和#x27;什么是complextype?

如何从xmlschema(xsd)和#x27;什么是complextype?,c,xml,regex,xsd,C,Xml,Regex,Xsd,如何从xml模式获取常规exp?我们知道xml模式有明确的规则表达式,但如何实现它呢?(例如,编写一个程序以获得常规exp) 只考虑复合型。例如:在xsd中,我们有: <xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:ele

如何从xml模式获取常规exp?我们知道xml模式有明确的规则表达式,但如何实现它呢?(例如,编写一个程序以获得常规exp)

只考虑复合型。例如:在xsd中,我们有:

<xs:element name="letter">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="orderid" type="xs:positiveInteger"/>
      <xs:element name="shipdate" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

或:


查找序列的意思是“,”,因此结果是“字母(名称、订单ID、发货日期)” 结果是名为regex

另一个示例:(如果xsd有以下内容:)


表示complexType的名称为“请求”。 …表示…的关系是“全部”(如果我们用&)表示“全部”) 全部都是“&”,因此结果是“请求url&组件&操作&参数&会话&cgi数据”

有没有人可以帮我编写一个程序来实现这个功能? 或者告诉我一个算法

我编写了一个程序,但它有太多错误,逻辑上是错误的。 这是我的节目:

#include<stdio.h> 
#include<string.h>
#include <stdlib.h>
#include<io.h> 
#define MatchMaxLen 69500
void process(char *result,char *str,char *str2,FILE* fp1)
{
if(strstr(str2,"complexType>")) fgets(str2,3000,fp1);
        //strcat(str,   str2);
    char c[5],*pem;
    int m=0;   if(!strstr(str2,"xs:sequence")&&!strstr(str2,"xs:choice")&&!strstr(str2,"xs:all")) return;
    if(strstr(str,"element name=\""))
        {
            pem=strstr(str,"element name=\"")+strlen("element name=\"");

            while(*pem!='\"')*result++=*pem++;//put element name in purec

            *result++=' ';
        }   
    if(strstr(str2,"xs:sequence")) {*result++='(';c[m++]=',';}
    if(strstr(str2,"xs:choice")) {*result++='(';c[m++]='|';}
    if(strstr(str2,"all")) {*result++='(';c[m++]='&';}
    fgets(str2,3000,fp1);
    while(!(strstr(str2,"sequence>")||strstr(str2,"choice>")||strstr(str2,"all>")))
        {
            if(str2,"complexType>") process(result,str,str2,fp1);
    if(strstr(str2,"element name=\""))
    {
    pem=strstr(str2,"element name=\"");
    pem=pem+strlen("element name=\"");
    }
    if(strstr(str2,"element ref=\""))
    {
    pem=strstr(str2,"element ref=\"");
    pem=pem+strlen("element ref=\"");
    }
    while(*pem!='\"') *result++=*pem++;

    char *pmax,*pmin;
    pmin=strstr(str2,"minOccurs=\"");
    pmin=pmin+strlen("minOccurs=\"");
    pmax=strstr(str2,"maxOccurs=\"");
    pmax=pmax+strlen("maxOccurs=\"");
    if(strstr(str2,"minOccurs")&&strstr(str2,"maxOccurs"))
    {
        if(*pmax=='1'&&*pmin=='0')
        {
        *result++=')';
        *result++='?';
        }
    if(*pmax=='u'&&*pmin=='0')
        {   
        *result++=')';
        *result++='*';
        }
    if(*pmax=='u' && *pmin=='1')
        {
        *result++=')';
        *result++='+';
        }
    *result++=c[m-1];
    }

    fgets(str2,3000,fp1);}
    m--;
}

void main()
{
char type[100][20];
char name[100][20];
char res[3000];
char *result=res;
int flag=0;
char *str, *str2,*psp;
char destfname[20];
char *path="E:\\study\\research\\summer\\program\\*.xsd";/*use this when needed in future extension.Well, been used currently*/
char path1[]="E:\\study\\research\\summer\\program\\";
int len;
FILE *fp1,*fp2; 
char string[MatchMaxLen];
char string2[3000];
struct _finddata_t   ffblk;
long done= _findfirst(path,&ffblk);  
do{ /*sprintf(filename,"dtd%d.dtd",i); 
In the future if the filename is not regular or we don't know the exact numbersome day, 
you may need function like _findnext,_findfirst.Well, currently being used*/
strcat(path1,ffblk.name);
if ((fp1=fopen(path1,"r"))==NULL)
{ /* read source file*/
        printf("cannot open file\n");
        exit(0);
}
sprintf(destfname,"IncludeNamexsdRe%s.txt",ffblk.name);
if((fp2=fopen(destfname,"w"))==NULL)
{ /* destination file*/
        printf("cannot open file\n"); 
        exit(0);
}   
char *pem;
int j(0);
int i(0);
while (fgets(string,MatchMaxLen,fp1))
{//first while's purpose is to create the map of name and type      
str=string;
if(!strstr(str,"element name")) continue;// the thing u need to process
i++;
pem=strstr(str,"element name");
pem=pem+strlen("element name=")+1;//pem: pointer of the element name
while(*pem!='\"')//while not the end point """
{
name[i][j++]=*pem++;//put the element name to the name[][]
}
name[i][j]='\0';
if(!(psp=strstr(string,"type="))) continue;
memccpy(type[i],psp+6,'\"',22); 
}
rewind(fp1);//normal process, here we go
char purecomplex[20];
char *purec=purecomplex;
char ctype[20];
char *cotype=ctype;
while (fgets(string,MatchMaxLen,fp1))
{   
        if( strlen( string ) == 0 ) 
            continue;
    str=string;
    if (str == NULL) 
        continue; 
    if( strlen( string ) == 1 ) continue;   
    //printf("%s",string);

    str2=string2;
    if(strstr(str,"<!--")) 
    {
        while(!strstr(str,"-->"))
        {
        fgets(str2,3000,fp1);
        strcat(str, str2);
    //delete useless char
    //printf("%s\n",str2); 
        }
    continue;
    }
    if(!(pem=strstr(str,"element name=\""))&&!strstr(str,"complexType name=")) continue;

    /*while(match(str))
    {*/
        fgets(str2,3000,fp1);
        if(strstr(str2,"annotation>"))
        {
            do{ fgets(str2,3000,fp1);}while(!strstr(str2,"</xs:annotation>"));
            fgets(str2,3000,fp1);
        }

        if(strstr(str,"complexType name=\""))
        {
            char* pt;
            pt=strstr(str,"complexType name=\"")+strlen("complexType name=\"");
    //if(strstr(str,"element name=\"")) pt=strstr(str,"element name=\"")+strlen("element name=\"");

            while(*pt!='"') *result++=*pt++;//if has complextype name, put it in cotype

            *result++=' ';
        }
        process(result,str,str2,fp1);
    }//}
    *result='\0';
    fputs(res,fp2 );
    //fputc('\n',fp2 );

    //printf("%s\n",ffblk.name);
    printf("2===%s\n",string);
    fclose(fp1);
    fclose(fp2);
}while(!_findnext(done,&ffblk));

_findclose(done);         

}
#包括
#包括
#包括
#包括
#定义MatchMaxLen 69500
作废过程(char*result、char*str、char*str2、文件*fp1)
{
if(strstr(str2,“complexType>”)fgets(str23000,fp1);
//strcat(str,str2);
char c[5],*pem;
int m=0;如果返回(!strstr(str2,“xs:sequence”)&&&!strstrstr(str2,“xs:choice”)&&!strstrstr(str2,“xs:all”);
if(strstrstr,“元素名=\”)
{
pem=strstrstr(str,“元素名=\”)+strlen(“元素名=\”);
while(*pem!='\')*result++=*pem++;//将元素名放在purec中
*结果+='';
}   
if(strstr(str2,“xs:sequence”){*result++='('c[m++]=',';}
if(strstr(str2,“xs:choice”){*result++='('c[m++]='|'}
如果(strstr(str2,“all”){*result++='(';c[m++]='&';}
fgets(STR23000,fp1);
而(!(strstr(str2,“序列>”)| | str(str2,“选择>”)| | str(str2,“全部>”))
{
if(str2,“complexType>”)进程(结果、str、str2、fp1);
if(strstr(str2,“元素名=\”))
{
pem=strstr(str2,“元素名=\”);
pem=pem+strlen(“元素名=\”);
}
if(strstr(str2,“元素引用=\”))
{
pem=strstr(str2,“元素引用=\”);
pem=pem+strlen(“元素引用=\”);
}
而(*pem!='\')*结果+=*pem++;
字符*pmax,*pmin;
pmin=strstr(str2,“minOccurs=\”);
pmin=pmin+strlen(“minOccurs=\”);
pmax=strstr(str2,“maxOccurs=\”);
pmax=pmax+strlen(“maxOccurs=\”);
if(strstr(str2,“最小发生”)&&strstr(str2,“最大发生”))
{
如果(*pmax=='1'&&&*pmin=='0')
{
*结果++=')';
*结果++='?';
}
如果(*pmax=='u'&&*pmin=='0')
{   
*结果++=')';
*结果+++='*';
}
如果(*pmax=='u'&&*pmin=='1')
{
*结果++=')';
*结果++='+';
}
*结果+++=c[m-1];
}
fgets(str23000,fp1);}
m--;
}
void main()
{
字符类型[100][20];
字符名[100][20];
charres[3000];
char*result=res;
int标志=0;
字符*str,*str2,*psp;
char destfname[20];
char*path=“E:\\study\\research\\summer\\program\\\*.xsd”/*在将来的扩展中需要时使用此选项。目前已使用*/
char path1[]=“E:\\study\\research\\summer\\program\\”;
内伦;
文件*fp1,*fp2;
字符字符串[MatchMaxLen];
char string2[3000];
结构(finddata)ffblk ;;
long done=\u findfirst(路径和ffblk);
do{/*sprintf(文件名,“dtd%d.dtd”,i);
将来如果文件名不规则或者某一天我们不知道确切的数字,
您可能需要像_findnext、_findfirst.Well这样的函数,当前正在使用*/
strcat(路径1,ffblk.name);
if((fp1=fopen(路径1,“r”))==NULL)
{/*读取源文件*/
printf(“无法打开文件\n”);
出口(0);
}
sprintf(destfname,“includenamexdre%s.txt”,ffblk.name);
if((fp2=fopen(destfname,“w”))==NULL)
{/*目标文件*/
printf(“无法打开文件\n”);
出口(0);
}   
char*pem;
intj(0);
int i(0);
while(fgets(字符串、MatchMaxLen、fp1))
{//while的第一个目的是创建名称和类型的映射
str=字符串;
如果(!strstrstr(str,“element name”))继续;//您需要处理的事情
i++;
pem=strstr(str,元素名称);
pem=pem+strlen(“element name=”)+1;//pem:元素名的指针
while(*pem!=“\”)//而不是终点“”
{
name[i][j++]=*pem++;//将元素名置于名称[][]
}
名称[i][j]='\0';
如果(!(psp=strstr(字符串,“type=”))继续;
memccpy(类型[i],psp+6,“\”,22);
}
倒带(fp1);//正常过程,开始
煤焦纯络合物[20];
char*purec=purecomplex;
char-ctype[20];
char*cotype=ctype;
while(fgets(字符串、MatchMaxLen、fp1))
{   
if(strlen(string)==0)
继续;
str=字符串;
如果(str==NULL)
继续;
如果(strlen(string)==1)继续;
//printf(“%s”,字符串);
str2=string2;
if(strstr(str,“”)
{
fgets(STR23000,fp1);
strcat(str,str2);
//删除无用字符
//printf(“%s\n”,str2);
}
继续;
}
如果(!(pem=strstrstr(str,“element name=\”)&&&!strstrstr(str,“complexType name=”))继续;
/*while(匹配(str))
{*/
fgets(STR23000,fp1);
if(strstr(str2,“注释>”))
{
do{fgets(str23000,fp1);}而(!strstr(str2,“”);
fgets(STR23000,fp1);
}
if(strstrstr,“complexType name=\”)
{
char*pt;
pt=strstrstr(str,“complexType名称=\”)+strlen(“complexType名称=\”);
//if(strstr(str,“元素名=\”))pt=strstrstr(str,“元素名=\”)+strlen(元素名=\”);
while(*pt!='“')*result++=*pt++;//如果有complextype名称,则将其放入cotype中
*结果+='';
}
过程(结果、str、str2、fp1);
}//}
*结果='\0';
FPUT(res,fp2);
//fputc('\n',fp2);
//printf(“%s\n”,ffblk.name);
printf(“2===%s\n”,字符串);
fclose(fp1);
fclose(fp2);
}而(!_findnext(done,&ffblk));
_findclose(完成);
}
有没有人可以帮我编写一个程序来实现这个功能?
或者告诉我一个算法?

因为每个XSD内容模型都是c
<xs:complexType name="request">
<xs:all>
  <xs:element name="url" type="xs:string"/>
  <xs:element name="component" type="xs:string"/>
  <xs:element name="action" type="xs:string" minOccurs="0"/>
  <xs:element name="params" type="varList" minOccurs="0"/>
  <xs:element name="session" type="varList" minOccurs="0"/>
  <xs:element name="cgi-data" type="varList" minOccurs="0"/>
</xs:all>
#include<stdio.h> 
#include<string.h>
#include <stdlib.h>
#include<io.h> 
#define MatchMaxLen 69500
void process(char *result,char *str,char *str2,FILE* fp1)
{
if(strstr(str2,"complexType>")) fgets(str2,3000,fp1);
        //strcat(str,   str2);
    char c[5],*pem;
    int m=0;   if(!strstr(str2,"xs:sequence")&&!strstr(str2,"xs:choice")&&!strstr(str2,"xs:all")) return;
    if(strstr(str,"element name=\""))
        {
            pem=strstr(str,"element name=\"")+strlen("element name=\"");

            while(*pem!='\"')*result++=*pem++;//put element name in purec

            *result++=' ';
        }   
    if(strstr(str2,"xs:sequence")) {*result++='(';c[m++]=',';}
    if(strstr(str2,"xs:choice")) {*result++='(';c[m++]='|';}
    if(strstr(str2,"all")) {*result++='(';c[m++]='&';}
    fgets(str2,3000,fp1);
    while(!(strstr(str2,"sequence>")||strstr(str2,"choice>")||strstr(str2,"all>")))
        {
            if(str2,"complexType>") process(result,str,str2,fp1);
    if(strstr(str2,"element name=\""))
    {
    pem=strstr(str2,"element name=\"");
    pem=pem+strlen("element name=\"");
    }
    if(strstr(str2,"element ref=\""))
    {
    pem=strstr(str2,"element ref=\"");
    pem=pem+strlen("element ref=\"");
    }
    while(*pem!='\"') *result++=*pem++;

    char *pmax,*pmin;
    pmin=strstr(str2,"minOccurs=\"");
    pmin=pmin+strlen("minOccurs=\"");
    pmax=strstr(str2,"maxOccurs=\"");
    pmax=pmax+strlen("maxOccurs=\"");
    if(strstr(str2,"minOccurs")&&strstr(str2,"maxOccurs"))
    {
        if(*pmax=='1'&&*pmin=='0')
        {
        *result++=')';
        *result++='?';
        }
    if(*pmax=='u'&&*pmin=='0')
        {   
        *result++=')';
        *result++='*';
        }
    if(*pmax=='u' && *pmin=='1')
        {
        *result++=')';
        *result++='+';
        }
    *result++=c[m-1];
    }

    fgets(str2,3000,fp1);}
    m--;
}

void main()
{
char type[100][20];
char name[100][20];
char res[3000];
char *result=res;
int flag=0;
char *str, *str2,*psp;
char destfname[20];
char *path="E:\\study\\research\\summer\\program\\*.xsd";/*use this when needed in future extension.Well, been used currently*/
char path1[]="E:\\study\\research\\summer\\program\\";
int len;
FILE *fp1,*fp2; 
char string[MatchMaxLen];
char string2[3000];
struct _finddata_t   ffblk;
long done= _findfirst(path,&ffblk);  
do{ /*sprintf(filename,"dtd%d.dtd",i); 
In the future if the filename is not regular or we don't know the exact numbersome day, 
you may need function like _findnext,_findfirst.Well, currently being used*/
strcat(path1,ffblk.name);
if ((fp1=fopen(path1,"r"))==NULL)
{ /* read source file*/
        printf("cannot open file\n");
        exit(0);
}
sprintf(destfname,"IncludeNamexsdRe%s.txt",ffblk.name);
if((fp2=fopen(destfname,"w"))==NULL)
{ /* destination file*/
        printf("cannot open file\n"); 
        exit(0);
}   
char *pem;
int j(0);
int i(0);
while (fgets(string,MatchMaxLen,fp1))
{//first while's purpose is to create the map of name and type      
str=string;
if(!strstr(str,"element name")) continue;// the thing u need to process
i++;
pem=strstr(str,"element name");
pem=pem+strlen("element name=")+1;//pem: pointer of the element name
while(*pem!='\"')//while not the end point """
{
name[i][j++]=*pem++;//put the element name to the name[][]
}
name[i][j]='\0';
if(!(psp=strstr(string,"type="))) continue;
memccpy(type[i],psp+6,'\"',22); 
}
rewind(fp1);//normal process, here we go
char purecomplex[20];
char *purec=purecomplex;
char ctype[20];
char *cotype=ctype;
while (fgets(string,MatchMaxLen,fp1))
{   
        if( strlen( string ) == 0 ) 
            continue;
    str=string;
    if (str == NULL) 
        continue; 
    if( strlen( string ) == 1 ) continue;   
    //printf("%s",string);

    str2=string2;
    if(strstr(str,"<!--")) 
    {
        while(!strstr(str,"-->"))
        {
        fgets(str2,3000,fp1);
        strcat(str, str2);
    //delete useless char
    //printf("%s\n",str2); 
        }
    continue;
    }
    if(!(pem=strstr(str,"element name=\""))&&!strstr(str,"complexType name=")) continue;

    /*while(match(str))
    {*/
        fgets(str2,3000,fp1);
        if(strstr(str2,"annotation>"))
        {
            do{ fgets(str2,3000,fp1);}while(!strstr(str2,"</xs:annotation>"));
            fgets(str2,3000,fp1);
        }

        if(strstr(str,"complexType name=\""))
        {
            char* pt;
            pt=strstr(str,"complexType name=\"")+strlen("complexType name=\"");
    //if(strstr(str,"element name=\"")) pt=strstr(str,"element name=\"")+strlen("element name=\"");

            while(*pt!='"') *result++=*pt++;//if has complextype name, put it in cotype

            *result++=' ';
        }
        process(result,str,str2,fp1);
    }//}
    *result='\0';
    fputs(res,fp2 );
    //fputc('\n',fp2 );

    //printf("%s\n",ffblk.name);
    printf("2===%s\n",string);
    fclose(fp1);
    fclose(fp2);
}while(!_findnext(done,&ffblk));

_findclose(done);         

}
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                >

  <xsl:output method="text"/>

  <!--* Ignore anything that looks complicated *-->
  <xsl:template match="xsd:attribute 
                       | xsd:attributeGroup
                       | xsd:group
                       | xsd:schema/xsd:element[@type]
                       | xsd:notation
                       | xsd:annotation
                       "/>

  <!--* Ignore text nodes (otherwise the output will be
      * inundated with whitespace) *-->
  <xsl:template match="text()"/>

  <!--* Top-level elements with local complex types; those
      * we want to handle.
      *-->
  <xsl:template match = "xsd:schema/xsd:element[xsd:complexType]">
    <xsl:apply-templates/>
  </xsl:template>

  <!--* Aha!  A complex type whose content model we want to turn 
      * into a regular expression 
      *-->
  <xsl:template match = "xsd:element/xsd:complexType
                         [xsd:sequence | xsd:choice | xsd:all]">
    <!--* write out the name for the named regex *-->
    <xsl:value-of select="concat('&#xA;&#xA;',
                          @name, parent::xsd:element/@name, 
                          ' ')"/>
    <!--* write out the regex *-->
    <xsl:apply-templates/>
  </xsl:template>

  <!--* Simple recursive case:  we encounter a model group. *-->
  <xsl:template match = "xsd:sequence|xsd:choice|xsd:all">
    <!--* Parenthesize the group and handle its children. *-->
    <xsl:text>(</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>)</xsl:text>

    <!--* Append *, ?, +, or {min, max}. *-->
    <xsl:call-template name="occurrence-indicator"/>

    <!--* If our parent has further children, 
        * append the appropriate connector. *-->
    <xsl:call-template name="connector"/>
  </xsl:template>

  <!--* An element in a content model. *-->
  <xsl:template match = "xsd:element[ancestor::xsd:complexType]">
    <!--* Write out the element's name.  We're lazy so 
        * we don't bother with a QName for a local element.
        * Also, we don't recur. *-->
    <xsl:value-of select="concat(@ref, @name)"/>

    <!--* Handle occurrence indicator and connect
        * just as for groups. *-->
    <xsl:call-template name="occurrence-indicator"/>
    <xsl:call-template name="connector"/>
  </xsl:template>


  <!--* Emit the appropriate occurrence indicator for
      * a group or element.
      * Use {min,max}, {min,}, or {n} notation for 
      * non-standard occurrence counts.
      *-->
  <xsl:template name="occurrence-indicator">
    <xsl:choose>
      <xsl:when test="(@minOccurs='1' or not(@minOccurs)) 
                      and 
                      (@maxOccurs='1' or not(@maxOccurs))">
        <xsl:text></xsl:text>
      </xsl:when>
      <xsl:when test="@minOccurs='0' 
                      and 
                      (@maxOccurs='1' or not(@maxOccurs))">
        <xsl:text>?</xsl:text>
      </xsl:when>
      <xsl:when test="@minOccurs='0' and @maxOccurs='unbounded'">
        <xsl:text>*</xsl:text>
      </xsl:when>
      <xsl:when test="(@minOccurs='1' or not(@minOccurs)) 
                      and 
                      @maxOccurs='unbounded'">
        <xsl:text>+</xsl:text>
      </xsl:when>
      <xsl:when test="@minOccurs=@maxOccurs">
        <xsl:value-of select="concat('{', @minOccurs,'}')"/>
      </xsl:when>
      <xsl:when test="@maxOccurs='unbounded'">
        <xsl:value-of select="concat('{', @minOccurs,',}')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat('{', 
                              @minOccurs,
                              ',',
                              @maxOccurs,
                              '}')"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="connector">
    <!--* Emit the appropriate connector, if we need one. *-->
    <xsl:if test="following-sibling::*[self::xsd:sequence 
                  | self::xsd:choice 
                  | self::xsd:all 
                  | self::xsd:element]">
      <xsl:choose>
        <xsl:when test="parent::xsd:sequence">
          <xsl:text>, </xsl:text>
        </xsl:when>
        <xsl:when test="parent::xsd:choice">
          <xsl:text> | </xsl:text>
        </xsl:when>
        <xsl:when test="parent::xsd:all">
          <xsl:text> &amp; </xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>