我有以下源XML:
<?xml version="1.0" encoding="utf-8" ?>
<svg width="800" height="600" xml:id="pg1">
<g class="foreground">
<text transform="translate(163,90)
rotate(0.000,219.664,8.938)
scale(1.000,1.000)"
RotationPoint="(320.000000,240.000000)"
xml:id="anno1"
visible="1" editwidth="439.33" editheight="17.88"
forcewidth="0" forceheight="0" language_direction="1"
textdirection="0" theme_anno_style="0">
<tspan justification="left"
line-spacing="1.00" prepara-spacing="1.00"
bullet="0">
<tspan>
<tspan fill="#000000" font-size="16.000"
font-family="Arial"
char-transform="0.00 1.00 0.00 0.00 0.00 1.00"
textLength="439.33" y="14.48" x="0.00">
It is not a book you can curl up at night with</tspan>
</tspan>
</tspan>
</text>
</g>
</svg>
我想将其转换为:
<svg:page id="page0.svg">
<svg:rect id="page0.svg_BG_Rect" fill="#ffffff" x="0" y="0" width="800.00" height="600.00"/>
<svg:textarea fill="none" font-weight="normal" font-stretch="normal" font-style="normal" font-family="" font-size="0.00" x="163.00" y="90.00" width="483.26" height="19.66" transform="translate(0,0) rotate(0,320,240)" id="anno1">
<svg:tspan fill="#000000" font-weight="normal" font-stretch="normal" font-style="normal" font-family="Arial" font-size="16.00" id="para1" text-align="start">
<svg:tspan fill="#000000" font-weight="normal" font-stretch="normal" font-style="normal" font-family="Arial" font-size="16.00">
<svg:tspan fill="#000000" font-weight="normal" font-stretch="normal" font-style="normal" font-family="Arial" font-size="16.00">
<![CDATA[ It is not a book you can curl up at night with]]>
</svg:tspan>
</svg:tspan>
<svg:tbreak/>
</svg:tspan>
</svg:textarea>
</svg:page>
我被告知最好使用XSLT完成,但我看到的示例/教程显示简单的1对1转换,可能可以使用文本搜索/替换(好的,轻微夸张)。
以上只是一个例子,还有更多,但我真的可以使用入门XSLT给我一个开始。
答案 0 :(得分:0)
从您的示例中可能有点模糊,因为某些属性值没有明确的来源,需要以某种方式进行硬编码或计算。
那就是说,我认为以下解决方案为您提供了您所追求的结构。 (请注意,它缺少更精细的细节,例如数字格式。)
给出以下输入XML:
<?xml version="1.0" encoding="utf-8" ?>
<svg width="800" height="600" xml:id="pg1">
<g class="foreground">
<text transform="translate(163,90)
rotate(0.000,219.664,8.938)
scale(1.000,1.000)"
RotationPoint="(320.000000,240.000000)"
xml:id="anno1"
visible="1" editwidth="439.33" editheight="17.88"
forcewidth="0" forceheight="0" language_direction="1"
textdirection="0" theme_anno_style="0">
<tspan justification="left"
line-spacing="1.00" prepara-spacing="1.00"
bullet="0">
<tspan>
<tspan fill="#000000" font-size="16.000"
font-family="Arial"
char-transform="0.00 1.00 0.00 0.00 0.00 1.00"
textLength="439.33" y="14.48" x="0.00">
It is not a book you can curl up at night with
</tspan>
</tspan>
</tspan>
</text>
</g>
</svg>
这个XSL样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:svg="http://www.w3.org/2000/svg">
<xsl:output method="xml" version="1.0" indent="yes" cdata-section-elements="svg:tspan" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- Get the fill attribute for svg:tspan from the innermost tspan element
(i.e. the tspan element with no tspan children). -->
<xsl:variable name="fill">
<xsl:value-of select="//tspan[not(tspan)]/@fill"/>
</xsl:variable>
<!-- Get the font-family attribute for svg:tspan from the innermost tspan element. -->
<xsl:variable name="font-family">
<xsl:value-of select="//tspan[not(tspan)]/@font-family"/>
</xsl:variable>
<!-- Get the font-size attribute for svg:tspan from the innermost tspan element. -->
<xsl:variable name="font-size">
<xsl:value-of select="//tspan[not(tspan)]/@font-size"/>
</xsl:variable>
<!-- Create the transform attribute by parsing the RotationPoint attribute of
the text element. -->
<xsl:variable name="transform">
<xsl:text>translate(0,0) rotate(0,</xsl:text>
<xsl:call-template name="substring-between">
<xsl:with-param name="substr" select="//text/@RotationPoint"/>
<xsl:with-param name="after" select=" '(' "/>
<xsl:with-param name="before" select=" ',' "/>
</xsl:call-template>
<xsl:text>,</xsl:text>
<xsl:call-template name="substring-between">
<xsl:with-param name="substr" select="//text/@RotationPoint"/>
<xsl:with-param name="after" select=" ',' "/>
<xsl:with-param name="before" select=" ')' "/>
</xsl:call-template>
<xsl:text>)</xsl:text>
</xsl:variable>
<!-- A helper function to find the string between two strings. -->
<xsl:template name="substring-between">
<xsl:param name="substr"/>
<xsl:param name="after"/>
<xsl:param name="before"/>
<xsl:value-of select="substring-after(substring-before($substr, $before), $after)"/>
</xsl:template>
<!-- Create the svg:page and svg:rect elements. Attributes hard-coded when it's not clear where they come from. -->
<xsl:template match="svg">
<svg:page xmlns:svg="http://www.w3.org/2000/svg" id="page0.svg">
<svg:rect id="page0.svg_BG_Rect" fill="#ffffff" x="0" y="0" width="{@width}" height="{@height}">
<xsl:apply-templates/>
</svg:rect>
</svg:page>
</xsl:template>
<!-- Create the svg:textarea element. Attributes hardcoded when it's not clear where they come from. -->
<xsl:template match="text">
<svg:textarea fill="none" font-weight="normal" font-stretch="normal" font-style="normal" font-family="" font-size="0.00" x="163.00" y="90.00" width="{@editwidth}" height="{@editheight}" transform="{$transform}" id="{@xml:id}">
<xsl:apply-templates/>
</svg:textarea>
</xsl:template>
<!-- Change the tspan elements to svg:tspan elements, using attributes from the innermost tspan element (which were put into variables earlier). Only the outermost tspan element has the id and text-align attributes. -->
<xsl:template match="tspan">
<svg:tspan fill="{$fill}" font-weight="normal" font-stretch="normal" font-style="normal" font-family="{$font-family}" font-size="{$font-size}">
<xsl:if test="not(parent::tspan)">
<xsl:attribute name="id">para1</xsl:attribute>
<xsl:attribute name="text-align">start</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</svg:tspan>
</xsl:template>
</xsl:stylesheet>
输出此XML:
<svg:page xmlns:svg="http://www.w3.org/2000/svg" id="page0.svg">
<svg:rect id="page0.svg_BG_Rect"
fill="#ffffff"
x="0"
y="0"
width="800"
height="600">
<svg:textarea fill="none"
font-weight="normal"
font-stretch="normal"
font-style="normal"
font-family=""
font-size="0.00"
x="163.00"
y="90.00"
width="439.33"
height="17.88"
transform="translate(0,0) rotate(0,320.000000,240.000000)"
id="anno1">
<svg:tspan fill="#000000"
font-weight="normal"
font-stretch="normal"
font-style="normal"
font-family="Arial"
font-size="16.000"
id="para1"
text-align="start">
<svg:tspan fill="#000000"
font-weight="normal"
font-stretch="normal"
font-style="normal"
font-family="Arial"
font-size="16.000">
<svg:tspan fill="#000000"
font-weight="normal"
font-stretch="normal"
font-style="normal"
font-family="Arial"
font-size="16.000"><![CDATA[
It is not a book you can curl up at night with
]]></svg:tspan>
</svg:tspan>
</svg:tspan>
</svg:textarea>
</svg:rect>
</svg:page>