开发者

XML Schema / XML Doc Structure - Written Correctly

New to XML, written an XML document and auto generated a Schema using Visual Studio. I started writing my own schema originally. My question would be is there any flaws or possible improvements I can implement? I've got type, requirement and occurrence validation, just curious to hear what you experienced guys say.

XML

<?xml version="1.0" encoding="utf-8" ?>
<university>
<lesson id="ms434">
  <subject>Biology</subject>
  <maintopic name="Human Biology">
    <subtopic>Enlarge Hearts</subtopic>
    <subtopic>Heart Valves</subtopic>
  </maintopic>
  <content>
    <sentance>Very long sentance one</sentance>
    <sentance>Very long sentance two</sentance>
    <sentance>Very long sentance three</sentance>
  </content>
</lesson>
</university>

Schema

<?xml version="1.0" encoding="utf-8"?>

<xsl:template match="@* | node()">
  <html>
    <body>
      <h1>Professional Training Facilities</h1>
 开发者_如何学Python     <p>
        <strong>University: </strong>
        <xsl:for-each select="university/lesson">          
        <xsl:value-of select="subject"/>
      </p>
      <br/>
      <p>
        <strong>Main Topic: </strong>
        <xsl:value-of select="maintopic=name"/>
      </p>
      <br/>
      <p>
        <strong>Sub Topics: </strong>
        <xsl:for-each select="maintopic">
          <p>
            <xsl:value-of select="subtopic"/>
          </p>
        </xsl:for-each>
      </p>
      <p></p>
      <strong>Content:</strong>
      <xsl:for-each select="content">
        <p>
          <xsl:value-of select="sentance"/>
        </p>
      </xsl:for-each>
      </xsl:for-each> 
      <br/>
    </body>
  </html>
</xsl:template>

When I'd almost completed the Schema manually, I'm sure I had a lot less code, is the auto generation excessive?

Edit: foreach error for first statement, working on a fix, that's not the problem btw.


A schema is a description of a class of documents. Any schema generated from a single document is a guess. For example, if all your lesson elements have a length attribute that is an integer, the tool might guess that it will always be an integer, and give it a type of xs:integer. But you might want to be more precise, and make the type an integer in the range 30 to 60. Or the tool might be more restrictive than you want: perhaps it guesses that the ID is always 5 characters long, when that's just an accident of your sample data. So whenever you use a schema-generation tool like this, you will need to check the output and change it to describe a class of documents rather than just your sample.

I don't know the VS tool, but many such tools have options for the style in which the schema is generated, e.g. local element declarations vs global element declarations. The different outputs might well be equivalent, but some of them may allow greater reuse of components or be easier to modify. It's worth experimenting with different options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜