The XML format is widely known as an exchange format for information. As a response to that, the SystemWeaver Script Language is equipped with the capability of generating such formats. This material will introduce the XML generation capabilities of SystemWeaver. 


Prerequisites


Exercises

The answers to all exercises in this article can be found on the Script Training>Answers ribbon tab of the database linked above:   


Note: This article does not cover the complete set of functionality.


XML Editor

XML formats are created in the XML Editor. To access it, select an item in the tree, click on the View drop-down list, select XMLs and select Sandbox:

 


The XML generation capabilities of SystemWeaver are composed of the following constructs:

  • <XDocument>
  • <XElement>
  • <XAttribute>
  • <Text>
  • <Description>
  • <XDefaultNamespace>


As with all configurable items, when you are satisfied with your script or to check on its progress, switch from the Definition to the View tab to see the results.


<XDocument>

The <XDocument> tag describes the root element of the XML document generated. It has an Attribute ”name=” that sets the name of the root element.


Example

<XDocument name="RootElement"> 
</XDocument>


<XComment>


The <XComment> tag is optional and can be used, together with <Text> to include comments in the XML output. The text value may include SystemWeaver Field Codes.


Example

<XDocument name="TopTag">
	<XComment>
		<Text>My comment</Text>
	</XComment>
</XDocument>



<XElement>


The <XElement> tag is used to define XML Elements within the <XDocument> tag. It has an Attribute ”name=” that sets the name of the element. <XElement> tags can be nested inside each other.


Note: XML element names may not contain blank spaces.


Example

<XDocument name="RootElement"> 
  <XElement name="Specification">
     <ForEachPart type="XESX">                     
       <DefObj>
         <XElement name="#{Name}”/> 
       </DefObj> 
     </ForEachPart>
  </XElement>
</XDocument>



Bildresultat för exercise icon freeExercise 1 - XML Elements

  1. Open Item x0400000000000292.
  2. Open the XML Editor.
  3. For Vehicle Architecture, define an XML with XElements that replicate the software structure of the tree. 

Expected result: 


<XAttribute>

The <XAttribute> tag is used to define attributes of XML elements. It has an attribute ”name” that sets the name of the attribute and an attribute ”value” that defines the actual attribute value. One <XElement> can have many attributes.


Example

<XDocument name="RootElement">  
  <XElement name="Specification">
    <ForEachPart type="XESX"> 
      <DefObj>
        <XElement name="#{Name}">
          <XAttribute name="handle" value="#{Handle}"/>
          <XAttribute name="version" value="#{Version}"/>
        </XElement>
      </DefObj> 
    </ForEachPart>
  </XElement>
</XDocument>



Bildresultat för exercise icon freeExercise 2 - Attributes

  1. Open Item x0400000000000292.
  2. Open the XML Editor.
  3. Using Exercise 1 as a starting point, add an attribute on every XElement for the handle, i.e., the unique ID of a SystemWeaver object, for every item. 

Expected result: 


<Text>

The <Text> tag is used to define text inside an <XElement>. 


Example

<XDocument name="RootElement"> 
  <XElement name="Specification">
    <Text>#{Name} : #{Version}</Text>
  </XElement>
</XDocument>



Bildresultat för exercise icon freeExercise 3 - Text

  1. Open Item x0400000000000292.
  2. Open the XML Editor.
  3. Using Exercise 2 as a starting point, add an XElement on components and requirements containing the name of the item using a text tag.  

Expected result: 


<Description>

The <Description> tag is used to print the content of the Description field into an <XElement>. The format attribute has 3 options:

  • ”plain” exports the Description as plain text. 
  • ”xhtml” exports the Description in XHTML format version 1.0.
  • "reqif" exports the Description in XHTML format version 1.1.


Tip: When choosing one of the XHTML formats, the ”prefix” attribute can be used to specify which XML namespace prefix to use for the generated XHTML. Note that this namespace prefix must have been declared in the file already.


Note: <Description format="reqif"/> is deprecated. For improved XHTML generation use <Extension name="xhtmlDescription"/>.

Example

<XDocument name="RootElement"> 
  <XElement name="Requirement">
   <Description format="plain"/>
  </XElement>
</XDocument>



Note: This is not the tag used for Description in reports.


Bildresultat för exercise icon freeExercise 4 - Description Field


  1. Open Item x0400000000000292.
  2. Open the XML Editor.
  3. Using Exercise 3 as a starting point, add an XElement on requirements containing the Description in plain text.  

Expected result (screenshot purposefully cut-off at bottom):


<XDefaultNamespace>

The <XDefaultNamespace> tag adds a default name space to the XDocument element. 


Example

<XDocument name="RootElement"> 
  <XDefaultNamespace uri="ANameSpace"/>
</XDocument>



The purpose of using an URI is to give the namespace a unique name. However, companies often use the namespace as a pointer to a web page containing namespace information.


What's Next?

You can extend your knowledge by practicing the numerous scripting options described in the SystemWeaver Script Language Reference section in the Help.