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

xslt cookbook phần 8 pptx

76 244 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

11.1 Converting Visio VDX Documents to SVG 11.1.1 Problem You want to convert Microsoft Visio XML files (VDX) into more portable SVG files. 11.1.2 Solution John Breen implemented the following solution. He maps the major Visio elements to SVG as shown in Table 11-1. Table 11-1. Visio-to-SVG Mappings Visio element SVG element VisioDocument/Colors/ColorEntry color value VisioDocument/Stylesheets/StyleSheet CSS Style VisioDocument/Pages/Page Svg Page/Shapes/Shape G Shapes/Shape/@NameU @id Shapes/Shape/XForm transform XForm/PinX and XForm/PinX translate( ) Shapes/Shape/Fill/FillForegnd @fill (with lookup) Shapes/Shape/Geom Path Shape/Geom/MoveTo @d "M" Shape/Geom/LineTo @d "L" Shape/Geom/NoFill(0) @d "z" This section goes over only the main stylesheet and select portions of the included ones. The entire source code with examples is available at http://sourceforge.net/projects/vdxtosvg/: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="urn:schemas-microsoft-com:office:visio" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:math="java.lang.Math" xmlns:jDouble="java.lang.Double" xmlns:saxon="http://icl.com/saxon" exclude-result-prefixes="v math saxon jDouble" xmlns="http://www.w3.org/2000/svg" version="1.0"> <xsl:output method="xml" version="1.0" omit-xml-declaration="no" media-type="image/svg+xml" encoding="iso-8859-1" indent="yes" cdata-section-elements="style" doctype-public="-//W3C//DTD SVG 1.0//EN" doctype-system="http://www.w3.org/TR/2001/REC-SVG- 20010904/DTD/svg10.dtd" /> The stylesheet uses a parameter pageNumber for specifying what page should be extracted from the VDX, and the parameter userScale specifies by which amount to scale Visio units to user units: <xsl:param name="pageNumber" select="1"/> <xsl:param name="userScale" select="100"/> <! = = = = Variables (ie, Constants) = = = = = = = = = = = = = = > <! Color map > <xsl:variable name="Colors" select="//v:Colors[position( )=1]/v:ColorEntry"/> <! Page being processed > <xsl:variable name="Page" select="/v:VisioDocument/v:Pages/v:Page[number($pageNumber)] "/> <! Template Masters > <xsl:variable name="Masters" select="//v:Masters[position( )=1]/v:Master"/> <! viewBox Master > <xsl:variable name="viewBoxMaster" select="$Masters[@NameU='viewBox']"/> <! Ratio of font height to width (fudge factor) > <xsl:variable name="fontRatio" select="2"/> <! Pi (SVG uses degrees, Visio uses radians) > <xsl:variable name="pi" select="3.14159265358979323846264338327"/> The stylesheet is decomposed into several components that are included here. Portions of these modules are discussed later in this section. The stylesheet implements some extensions in JavaScript; however, if your XSLT processor does not support JavaScript, you can still use this code. Some text might not format nicely, however: <! Included files > <xsl:include href="visio-style.xsl"/> <xsl:include href="visio-text.xsl"/> <xsl:include href="visio-masters.xsl"/> <xsl:include href="visio-nurbs.xsl"/> <! Scripts > <xsl:template name="required-scripts"> <script xlink:href="wordwrap.js" type="text/ecmascript"/> </xsl:template> <xsl:template match="/v:VisioDocument"> <xsl:apply-templates select="$Page"/> </xsl:template> A Visio page is mapped onto an SVG graphic. Information from the Visio document determines how best to lay out the graphic in a view box: <! = = = = = = = = Page = = = = = = = = = = = = = = = = = = = > <xsl:template match="v:Page"> <xsl:message> <xsl:value-of select="@NameU"/> </xsl:message> <svg id="{@NameU}"> <xsl:attribute name="xml:space"> <xsl:value-of select="'preserve'"/> </xsl:attribute> <xsl:choose> <! Use viewBox with name 'default' if present > <xsl:when test="//v:Shape[@Master=$viewBoxMaster/@ID and @NameU='default'][1]"> <xsl:for-each select="//v:Shape[@Master=$viewBoxMaster/@ID and @NameU='default']"> <xsl:attribute name="viewBox"> <xsl:value-of select="concat( v:XForm/v:PinX*$userScale, ' ', - v:XForm/v:PinY*$userScale, ' ', v:XForm/v:Width*$userScale, ' ', v:XForm/v:Height*$userScale)"/> </xsl:attribute> </xsl:for-each> </xsl:when> <! Otherwise, center on sheet > <xsl:otherwise> <xsl:attribute name="viewBox"> <xsl:value-of select="concat('0 ', - v:PageSheet/v:PageProps/v:PageHeight *$userScale, ' ', v:PageSheet/v:PageProps/v:PageWidth *$userScale, ' ', v:PageSheet/v:PageProps/v:PageHeight *$userScale)"/> </xsl:attribute> </xsl:otherwise> </xsl:choose> <xsl:call-template name="required-scripts"/> <xsl:call-template name="predefined-pattern-fgnds"/> <xsl:call-template name="predefined-markers"/> 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"/> </svg> </xsl:template> <! = = = = = = = = StyleSheets = = = = = = = = = > <xsl:template match="v:StyleSheets"> <defs> <xsl:for-each select="v:StyleSheet"> <! Line style > <style id="ss-line-{@ID}" type="text/css"> <xsl:text>*.ss-line-</xsl:text><xsl:value-of select="@ID"/> <xsl:text> { </xsl:text> <xsl:call-template name="recursive-line-style"> <xsl:with-param name="ss" select="."/> </xsl:call-template> <xsl:text> }</xsl:text> </style> <! Fill style > <style id="ss-fill-{@ID}" type="text/css"> <xsl:text>*.ss-fill-</xsl:text><xsl:value-of select="@ID"/> <xsl:text> { </xsl:text> <xsl:call-template name="recursive-fill-style"> <xsl:with-param name="ss" select="."/> </xsl:call-template> <xsl:text> }</xsl:text> </style> <! Text style > <style id="ss-text-{@ID}" type="text/css"> <xsl:text>*.ss-text-</xsl:text><xsl:value-of select="@ID"/> <xsl:text> { </xsl:text> <xsl:call-template name="recursive-text-style"> <xsl:with-param name="ss" select="."/> </xsl:call-template> <xsl:text> } </xsl:text> </style> </xsl:for-each> </defs> </xsl:template> <! Recurse through StyleSheet inheritance > <xsl:template name="recursive-line-style"> <xsl:param name="ss"/> <xsl:if test="$ss/@LineStyle"> <xsl:call-template name="recursive-line-style"> <xsl:with-param name="ss" select="$ss/ /v:StyleSheet[@ID=$ss/@LineStyle]"/> </xsl:call-template> </xsl:if> <xsl:apply-templates select="$ss/v:Line" mode="style"/> </xsl:template> <xsl:template name="recursive-fill-style"> <xsl:param name="ss"/> <xsl:if test="$ss/@FillStyle"> <xsl:call-template name="recursive-fill-style"> <xsl:with-param name="ss" select="$ss/ /v:StyleSheet[@ID=$ss/@FillStyle]"/> </xsl:call-template> </xsl:if> <xsl:apply-templates select="$ss/v:Fill" mode="style"/> </xsl:template> <xsl:template name="recursive-text-style"> <xsl:param name="ss"/> <xsl:if test="$ss/@TextStyle"> <xsl:call-template name="recursive-text-style"> <xsl:with-param name="ss" select="$ss/ /v:StyleSheet[@ID=$ss/@TextStyle]"/> </xsl:call-template> </xsl:if> <xsl:apply-templates select="$ss/v:Char|$ss/v:Para" mode="style"/> </xsl:template> <! This template returns a string for the line style > <xsl:template match="v:Line" mode="style"> <xsl:for-each select="v:LineWeight"> <xsl:text>stroke-width:</xsl:text> <xsl:value-of select=". * $userScale"/><xsl:text>;</xsl:text> </xsl:for-each> <xsl:for-each select="v:LineColor"> <xsl:choose> <xsl:when test=" /v:LinePattern > 0"> <xsl:text>stroke:</xsl:text> <xsl:call-template name="lookup-color"> <xsl:with-param name="c_el" select="."/> </xsl:call-template> </xsl:when> <xsl:when test=" /v:LinePattern = 0"> <xsl:text>stroke:none</xsl:text> </xsl:when> </xsl:choose> <xsl:text>;</xsl:text> </xsl:for-each> <xsl:for-each select="v:EndArrow"> <xsl:choose> <xsl:when test=". = 0"> <xsl:value-of select="string('marker- end:none;')"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="concat('marker- end:url(#EndArrow-', ., '-', /v:EndArrowSize, ');')"/> </xsl:otherwise> </xsl:choose> </xsl:for-each> <xsl:apply-templates select="v:LinePattern[. &gt; 1]" mode="style"/> </xsl:template> <! This template returns a string for the fill style > <xsl:template match="v:Fill" mode="style"> <xsl:for-each select="v:FillForegnd"> <xsl:choose> <xsl:when test=" /v:FillPattern = 1"> <xsl:text>fill:</xsl:text> <xsl:call-template name="lookup-color"> <xsl:with-param name="c_el" select="."/> </xsl:call-template> </xsl:when> <xsl:when test=" /v:FillPattern = 0"> <xsl:text>fill:none</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>fill:url(#</xsl:text> <xsl:value-of select="generate-id( / )"/> <xsl:text>-pat)</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:text>;</xsl:text> </xsl:for-each> </xsl:template> <! This template returns a string for the text style > <xsl:template match="v:Char|v:Para" mode="style"> <xsl:for-each select="v:Color"> <! I don't think Visio handles filled characters > <xsl:text>stroke:none</xsl:text> <xsl:text>;fill:</xsl:text> <xsl:call-template name="lookup-color"> <xsl:with-param name="c_el" select="."/> </xsl:call-template> <xsl:text>;</xsl:text> </xsl:for-each> <xsl:for-each select="v:Size"> <xsl:text>font-size:</xsl:text> <xsl:value-of select=". * $userScale"/><xsl:text>;</xsl:text> </xsl:for-each> <xsl:for-each select="v:HorzAlign"> <xsl:text>text-anchor:</xsl:text> <xsl:choose> <xsl:when test="(. = 0) or (. = 3)"> <xsl:text>start</xsl:text> </xsl:when> <xsl:when test=". = 1"> <xsl:text>middle</xsl:text> </xsl:when> <xsl:when test=". = 2"> <xsl:text>end</xsl:text> </xsl:when> </xsl:choose> <xsl:text>;</xsl:text> </xsl:for-each> </xsl:template> <! Ignore all other StyleSheet elements > <xsl:template match="*[parent::v:StyleSheet]" priority="- 100"/> 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 = = = = = = = = = = = = = > <xsl:template match="v:Shape"> <xsl:variable name="master" select="/v:VisioDocument//v:Masters[1]/ v:Master[@ID=current( )/@Master]"/> <xsl:variable name="svgElement"> <xsl:choose> <! Check for special svgElement property in shape > <xsl:when test="./v:Prop/v:Label[.='svgElement']"> <xsl:value-of select="./v:Prop/v:Label[.='svgElement']/ /v:Value"/> </xsl:when> <! and in master > <xsl:when test="@Master and $master//v:Prop/v:Label[.='svgElement']"> <xsl:value-of select="$master//v:Prop/v:Label[.='svgElement']/ /v:Value"/ > </xsl:when> <! The simple case maps a shape onto a svg (g)roup > <xsl:otherwise> <xsl:value-of select="'g'"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:choose> <xsl:when test="@Master and string($svgElement) and contains($specialMasters, $svgElement)"> <xsl:call-template name="choose-special-master"> <xsl:with-param name="master" select="$master"/> <xsl:with-param name="masterElement" select="$svgElement"/> </xsl:call-template> </xsl:when> <xsl:when test="($svgElement = 'defs') or ($svgElement = 'g') or ($svgElement = 'symbol')"> <xsl:choose> <xsl:when test="v:Hyperlink"> <! Surround shape with 'a' element > <! This is a minimal implementation. It doesn't support multiple links, subaddress, etc. > <a xlink:title="{v:Hyperlink/v:Description}" xlink:href="{v:Hyperlink/v:Address}"> <xsl:if test="v:Hyperlink/v:NewWindow"> <xsl:attribute name="show"> <xsl:value-of select="new"/> </xsl:attribute> </xsl:if> <xsl:element name="{$svgElement}"> <xsl:call-template name="userShape"/> </xsl:element> </a> </xsl:when> <xsl:otherwise> <xsl:element name="{$svgElement}"> <xsl:call-template name="userShape"/> </xsl:element> </xsl:otherwise> </xsl:choose> </xsl:when> </xsl:choose> </xsl:template> Here the normal shapes created by the user are mapped to SVG, as specified in Table 11-1: <! This does the processing for normal 'user' shapes > <xsl:template name="userShape"> <xsl:variable name="master" select="/v:VisioDocument/v:Masters /v:Master[(@ID=current( )/@Master) and (current( )/@Type != 'Group')] /v:Shapes/v:Shape | /v:VisioDocument/v:Masters /v:Master[@ID=current( ) /ancestor::v:Shape[@Master]/@Master] //v:Shape[@ID=current( )/@MasterShape] | ."/> <xsl:call-template name="setIdAttribute"/> <xsl:attribute name="class"> <xsl:for-each select="($master[@LineStyle])[last( )]"> <xsl:text> ss-line-</xsl:text> <xsl:value-of select="@LineStyle"/> </xsl:for-each> <xsl:for-each select="($master[@FillStyle])[last( )]"> <xsl:text> ss-fill-</xsl:text> <xsl:value-of select="@FillStyle"/> </xsl:for-each> </xsl:attribute> <xsl:attribute name="style"> <xsl:for-each select="$master"> <xsl:apply-templates select="./v:Line" mode="style"/> <xsl:apply-templates select="./v:Fill" mode="style"/> </xsl:for-each> </xsl:attribute> <xsl:for-each select="v:XForm"> <xsl:call-template name="transformAttribute"> </xsl:call-template> </xsl:for-each> <! This is to create the custom pattern > <xsl:apply-templates select="v:Fill" mode="Shape"/> <xsl:for-each select="v:Geom"> <xsl:apply-templates select="v:Ellipse"/> <xsl:if test="v:MoveTo or v:LineTo"> <xsl:call-template name="pathElement"/> </xsl:if> </xsl:for-each> <xsl:for-each select="($master/v:Text)[last( )]"> <xsl:apply-templates select="."/> </xsl:for-each> <xsl:apply-templates select="v:Shapes/v:Shape"/> <! Add elements from properties > <xsl:for-each select="v:Prop"> <xsl:choose> <xsl:when test="starts-with(v:Label, 'svg- element')"> <! This is sort of ugly - it may disappear some day > <xsl:value-of disable-output-escaping="yes" select="v:Value"/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:template> <xsl:template match="v:Ellipse"> <! This is a somewhat limited translation. It assumes that the axes are parallel to the x & y axes, and the lower- left corner of the bounding box is at the origin (which appears to be the way Visio draws them by default). > <ellipse id="ellipse-{generate- id(ancestor::v:Shape[1])}" cx="{v:X*$userScale}" cy="{-v:Y*$userScale}" rx="{v:X*$userScale}" ry="{v:Y*$userScale}"/> </xsl:template> [...]... 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: Date 2001 081 7 2001 082 0 2001 082 1 2001 082 2 Price 61 .88 62.7 60. 78 60.66 Volume 260163 24 185 9 233 989 387 444 Then the Excel XML version looks like this: 2001 081 7 61 .88 260163 2001 082 0 62.7 24 185 9 2001 082 1 60. 78... REC-html40"> Salvatore R Mangano Salvatore R Mangano 2002- 08- 18T00:43:49Z 2002- 08- 18T02:19:21Z Descriptix 10.3501 ... ss:Type="Number">2001 082 1 60. 78 233 989 2001 082 2 60.66 387 444 ... 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: 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/ 11.2 Working... translate( ) rotate( ) Visio Geom elements are translated . spreadsheet that looks like this: Date Price Volume 2001 081 7 61 .88 260163 2001 082 0 62.7 24 185 9 2001 082 1 60. 78 233 989 2001 082 2 60.66 387 444 Then the Excel XML version looks like this: <?xml. <LastAuthor>Salvatore R. Mangano</LastAuthor> <Created>2002- 08- 18T00:43:49Z</Created> <LastSaved>2002- 08- 18T02:19:21Z</LastSaved> <Company>Descriptix</Company>. Visio uses radians) > <xsl:variable name="pi" select="3.141592653 589 7932 384 62643 383 27"/> The stylesheet is decomposed into several components that are included

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

Xem thêm: xslt cookbook phần 8 pptx

TỪ KHÓA LIÊN QUAN