1. Trang chủ
  2. » Công Nghệ Thông Tin

xslt cookbook phần 8 pptx

76 246 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Converting Visio VDX Documents To SVG
Tác giả John Breen
Trường học Not Available
Chuyên ngành Not Available
Thể loại Not Available
Năm xuất bản 2025
Thành phố Not Available
Định dạng
Số trang 76
Dung lượng 242,4 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

11.3 Generating XTM Topic Maps from UML Models via XMI Since UML was not explicitly designed to represent topic maps, this example works only if certain conventions are adopted.. Convent

Trang 1

11.1 Converting Visio VDX Documents to SVG

Table 11-1 Visio-to-SVG Mappings

Trang 2

<xsl:param name="pageNumber" select="1"/>

<xsl:param name="userScale" select="100"/>

Trang 4

The real meat of the conversion begins here Start by processing Visio stylesheet elements to convert them into equivalent Cascading Style Sheet directives Then convert all shapes into their SVG representation:

<xsl:apply -templates select=" / /v:StyleSheets"/> <xsl:apply -templates select="v:Shapes/v:Shape"/>

</xsl:call-template>

<xsl:text> } </xsl:text>

Trang 5

</xsl:if>

<xsl:apply-templates select="$ss/v:Char|$ss/v:Para" mode="style"/>

<xsl:call-template name="lookup -color">

<xsl:with-param name="c_el" select="."/>

Trang 6

<xsl:call-template name="lookup -color">

<xsl:with-param name="c_el" select="."/>

Trang 7

<! Ignore all other StyleSheet elements >

<xsl:template match="*[parent::v:StyleSheet]" 100"/>

priority="-Here is where shapes are mapped onto an SVG equivalent Notice how shapes can be associated with masters in a Visio document Think of a master as a template of a shape from which a shape

on a page can inherit attributes and behavior

Each Visio shape is, by default , translated as a <g> element since a Visio shape can contain both graphics and text Recall that the <g> element is SVG's way of specifying a group of graphical elements that can share stylistic traits

SvgElement is a Visio property that the user can attach to a Visio shape to specify special handling by this translator For example, svgElement can be set to any other SVG container element, such as <defs> This feature keeps certain shapes from being rendered, such as paths for animateMotion elements This way, the path can be referenced in the SVG file, but it will not be displayed

One of the main reasons for using svgElement is to indicate special shapes that are translated

to elements that have no correspondence in Visio, such as animate, animateMotion, and viewBox visio-master.xsl, discussed later, handles these elements:

<! = = = = = = = Shape = = = = = = = = =

= = = = >

Trang 8

<xsl:template match="v:Shape">

<xsl:variable name="master"

select="/v:VisioDocument//v:Masters[1]/

Trang 9

<! This is a mini mal implementation It

doesn't support

multiple links, subaddress, etc >

<a xlink:title="{v:Hyperlink/v:Description}" xlink:href="{v:Hyperlink/v:Address}">

Trang 10

<! This is to create the custom pattern >

<xsl:apply-templates s elect="v:Fill" mode="Shape"/>

<xsl:for-each select="v:Geom">

<xsl:apply -templates select="v:Ellipse"/>

<xsl:if test="v:MoveTo or v:LineTo">

Trang 11

<xsl:text>)</xsl:text>

</xsl:attribute>

</xsl:template>

Trang 12

Visio Geom elements are translated to paths Most of the mapping is straightforward, except for the handling of Non-Uniform Rational B-Splines (NURBS), which is delegated to a special set of

templates in visio-nurbs.xsl, which you can peruse in the full distribution:

<! Translate Geom element into path element >

</xsl:when>

<xsl:when test="name( ) = 'EllipticalArcTo'"> <! If we don't have access to trig functions, the

arc will just be represented by two line segments >

Trang 13

select="math:atan2(v:Y - $lastY, v:X - $lastX)

- math:atan2(v:B - $lastY, v:A - $lastX)"/> <xsl:variable name="sweep">

Trang 14

<xsl:choose>

<xsl:when test="$angle &gt; 0

and math:abs($angle) &lt; 180">

<xsl:value-of select='0'/>

</xsl:when>

<xsl:when test="$angle &lt; 0

and math:abs($angle) &gt; 180">

Figure 11-1 Basic SVG shapes and text converted from Visio

Trang 15

The important lesson you should learn from this example goes beyond the details of Visio VDX , SVG, or any special tricks or techniques the author uses It is simply the fact that this complex transformation was the author's first experience with XSLT This says something very positive about the power of XSLT's transformational paradigm

11.1.4 See Also

The source code and some demonstrations are available on Source Forge at

http://sourceforge.net/projects/vdxtosvg/

Trang 16

11.2 Working with Excel XML Spreadsheets

11.2.1 Problem

You want to export data from Excel to XML, but not in the native format supported by Microsoft

11.2.2 Solution

If you have an Excel spreadsheet that looks like this:

Trang 17

<Cell>

<Data ss:Type="String">Price</Data> </Cell>

<Cell>

<Data ss:Type="String">Volume</Data> </Cell>

</Row>

<Row>

<Cell>

<Data ss:Type="Number">20010817</Data> </Cell>

<Cell>

<Data ss:Type="Number">61.88</Data> </Cell>

<Cell>

<Data ss:Type="Number">260163</Data> </Cell>

</Row>

<Row>

<Cell>

<Data ss:Type="Number">20010820</Data> </Cell>

<Cell>

<Data ss:Type="Number">62.7</Data> </Cell>

<Cell>

<Data ss:Type="Number">241859</Data> </Cell>

</Row>

<Row>

<Cell>

<Data ss:Type="Number">20010821</Data> </Cell>

<Cell>

<Data ss:Type="Number">60.78</Data> </Cell>

<Cell>

Trang 18

which is probably not what you had in mind!

This example conveniently maps an Excel XML file to a simpler XML file Many spreadsheets created in Excel have a structure in which the first row contains column names and subsequent rows contain data for those columns

One obvious mapping would convert the column names into element names and the remaining cells into element content The only missing pieces of information are the names of the top-level element and the element containing each row This stylesheet takes these names as parameters with some obvious defaults It converts some of the useful metadata into comments and throws away the Excel-specific stuff This section provides several other parameters that increase the generality of the conversion, such as which row contains the column names, where the data starts, and what to do about empty cells:

Trang 19

<! The name of the top level element >

<xsl:param name="topLevelName" select=" 'Table' "/>

<! The name of each row >

<xsl:param name="rowName" select=" 'Row' "/>

<! The namespace to use >

<xsl:param name="wsSub" select="'_'"/>

<! Determines which row contains the col names >

<xsl:param name="colNamesRow" select="1"/>

<! Determines which row the data begins >

<xsl:param name="dataRowStart " select="2"/>

<! If false then cells with null or whitespace only

content >

<! will be skipped >

<xsl:param name="includeEmpty" select="true( )"/>

<! If false then author and creation meta data will not

',''),':')"/>

</xsl:when>

<xsl:when test="translate($namespacePrefix,' ','')"> <xsl:value-of

select="concat(translate($namespacePrefix,' ',''),':')"/>

</xsl:when>

<xsl:otherwise/>

</xsl:choose>

Trang 21

<xsl:otherwise>

<xsl:value-of select="$COLS[$pos]/ss:Data"/> </xsl:otherwise>

Trang 22

At least one obvious additional extension could be made to this stylesheet: the handling of

multiple ss:Worksheet elements This handling could be done by specifying the worksheet number as a parameter:

<xsl:param name="WSNum" select="1"/>

<xsl:variable name="COLS"

select="/*/ss:Worksheet[$WSNum]/*/ss:Row[$colNamesRow]/ss:Cell"/>

<xsl:template match="ss:Workbook">

<xsl:element name="{concat($nsp,translate($topLevelName, '&#x20;&#x9;&#xA;',$wsSub))}"

A more ambitious solution handles each Worksheet in a multiple Worksheet document as

a separate element in the resulting document This setup means that the column names can no longer be handled as a global variable:

<xsl:template match="ss:Workbook">

<xsl:element name="{concat($nsp,translate($topLevelName, '&#x20;&#x9;&#xA;',$wsSub))}"

namespace="{$namespace}">

Trang 24

11.3 Generating XTM Topic Maps from UML Models via XMI

Since UML was not explicitly designed to represent topic maps, this example works only if certain

conventions are adopted Most conventions revolve around the use of specific UML stereotypes

A stereotype is a UML extension mechanism that lets you associate a symbol with any UML classifier that designates that classifier as having user-defined semantics To implement Topic Maps in UML, refer to the stereotypes listed in Table 11-2

Table 11-2 Conventions for topic mapping in UML

Stereotype UML context Meaning

Topic Class Represents a topic It is the default role of a class in our conventions,

so this stereotype is optional

topic -occurrence association

Base Name Class

Designates that the class models an alternate topic base name and therefore implies a scope The scope elements are associated with the baseName class via associations with stereotype scope

Occurrence Association Designates that the association points to a resource that is an

occurrence of the topic

Scope Association

Indicates the association that specifies the scope of the topic map characteristic The nature of the scope depends on the stereotype of the target class (topicRef, resourceRef, or

a topic

In addition to these stereotypes, the following UML-to-Topic Map mappings are used:

UML class

Is a Models Topic Map topic, unless a non-topic stereotype is present The class name

is used as the topic base name If the class has an attribute called ID, its default value is

Trang 25

used as the topic ID If no such ID is present, then the class name is also used as the topic

ID

UML association

Models a Topic Map association unless a stereotype specifies otherwise The UML association name is the Topic Map association name The UML role specifiers are the Topic Map role specifiers

UML instantiates association

Models the Topic Map instanceOf relationship

Unadorned UML generalization (inheritance) association

Used as a shortcut to specify the canonical superclass-subclass association where the ends are assigned the roles superclass and subclass automatically This same relationship links classes with the baseName stereotype to the topic classes for which they represent an alternate scoped name Generalization with the stereotype variant also specifies topic variants

If you use Rational Rose and it cannot save models as XMI, you should download the Rose add-in at

http://www.rational.com/support/downloadcenter/addins/rose/index.jsp I tested only this example with Rose, but other UML tools such as

TogetherSoft's Together Control Center also support XMI, and this example will probably work with these tools as well

The stylesheet that transforms XMI to XTM is shown here As was the case when you considered transforming XMI in Recipe 10.8, use several entities to make the long element names of XMI manageable:

= = = = = = = = = >

<! Some generic kind of UML element >

Trang 26

<!ENTITY ELEM "&FC;.Namespace.ownedElement">

<! The association as a whole >

<!ENTITY ASSOC "&FC;.Association">

<! The connection part of the association >

<!ENTITY CONN "&ASSOC;.connection">

<! The ends of an association >

<!ENTITY END "&ASSOC;End">

<!ENTITY CONNEND "&CONN;/&END;">

<!ENTITY ENDTYPE "&END;.type">

<! A UML class >

<!ENTITY CLASS "&FC;.Class">

<! The name of some UML entity >

<!ENTITY NAME "&FC;.ModelElement.name">

<! A UML sterotype >

<!ENTITY STEREOTYPE "&ME;.stereotype/&FX;.Stereotype">

<!The place where UML documentation is stored in XMI

<!ENTITY SUPERTYPE "&FC;.Generalization.supertype">

<!ENTITY SUBTYPE "&FC;.Generalization.subtype">

<!ENTITY SUPPLIER "&FC;.Dependency.supplier">

<!ENTITY CLIENT "&FC;.Dependency.client">

<!ENTITY ATTR "&CLASS;ifier.feature/&FC;.Attribute">

<! Used for pointing at standard XTM PSIs >

<!ENTITY TM.ORG "http://www.topicmaps.org/xtm/index.html"> ]>

<xsl:param name="termOnErr" select="true( )"/>

<! Index classes by their name >

<xsl:key name="classKey" match="&CLASS;" use="@xmi.id"/>

Trang 27

<! Index Stereoptypes by both name and xmi.id >

You can convert a XMI UML model to a topic map in two passes First, import topics from

classes, and then import XTM associations from UML associations:

<xsl:template match="/">

<xtm:topicMap>

<xsl:apply -templates mode="topics"/>

<xsl:apply -templates mode="associations"/>

</xtm:topicMap>

</xsl:template>

The only classes that should be translated into topic are the ones without a stereotype or with a

topic stereotype The other classes in the model are representations to which a topic can refer, such

as subject indicators and resources:

<! = = = = = = = = = = = = = = = = = =

= = = = = >

<! UML Classes to TOPICS Translation >

Trang 28

<! = = = = = = = = = = = = = = = = = =

= = = = = >

<xsl:template match="&ELEM;/&CLASS;" mode="topics">

<! Topics are modeled as classes whose >

<! stereotype is either empty or 'topic' >

<xsl:with-param name="class" select="."/>

<xsl:with-param name="prefix" select="''"/>

</xsl:call-template>

</xsl:variable>

<xtm:topic id="{$top icId}">

<! This for-each is solely to change context to the optional >

<! Core.Attribute attribute named

Trang 29

<xsl:when test="&ATTR;/&NAME; = 'id' ">

Trang 30

an abuse of the concept Nevertheless, it is effective and allows a visual cue in the UML diagram, rather than relying solely on stereotype tags:

<! Alternate base n ames are found by traversing UML > <! generalization relationships and looking for baseName >

<! Variants are found by traversing UML >

<! generalization relationships and looking for baseName >

select="key('classKey',$subtypeXmiId)"/> <xsl:if test="$variantClass/&STEREOTYPE;/@xmi.idref =

$VARIANT_ID">

Trang 31

<! Gets a variant's parameters from >

<! the attibutes of the variant class >

Trang 32

Topic Map occurrences are modeled as associations to classes containing resource references or data Since inline resource data can be too large to fit nicely as an attribute value, this example

allows the attribute description to be used as an alternate container of resource data:

<! Topic map occurances are modeled as associations to > <! classes containing resource references or data >

<xsl:template match="&ELEM;/&CLASS;" mode="getOccurances"> <xsl:variable name="xmiId" select="@xmi.id"/>

<! Search over the associations this class

Trang 33

XTM instanceOf relationships are modeled as UML dependency associations, also called

instantiates This representation of instanceOf is quite natural:

<! This template finds if a topic class has any

instanceOf >

<! associations >

<xsl:template match="&ELEM;/&CLASS;" mode="getInstanceOf"> <xsl:param name="classId"/>

Trang 34

<! We loop of dependency relations and determine > <! how the instance is represented >

Topic with no id!

Trang 35

<xsl:value-of select="$instanceClass/&NAME;"/> <xsl:text> is a </xsl:text>

Trang 36

<xsl:when test="$stereotypeId = $RESOURCE_ID">

<xsl:variable name="resourceId">

<xsl:call-template name="getResourceIdentity"> <xsl:with-param name="class" select="$class"/> </xsl:call-template>

<xsl:template match="&ASSOC;" mode="associations">

<! Only named UML associations are topic map

Trang 37

select="key('classKey',$superClassId)"/>

<! If a generalization relation exists from a topic to

Ngày đăng: 14/08/2014, 01:20

TỪ KHÓA LIÊN QUAN