1

XML

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0" width="1000" height="1000" id="svg5496">
  <defs id="defs5498">
    <font-face units-per-em="1024" id="font-face3935" font-family="SVGFont 1"/>
    <missing-glyph d="M0,0h1000v1024h-1000z" id="missing-glyph3937"/>
    <glyph unicode="一" glyph-name="null" horiz-adv-x="256" d="M21 112H234V93H21V112Z"/>
    <glyph unicode="丁" glyph-name="null" horiz-adv-x="256" d="M16 196H239V177H144V5Q144 -9 138 -15T113 -21Q98 -21 84 -19L80 1Q95 -2 110 -2Q118 -2 121 0T124 10V177H16V196Z"/>
    <glyph unicode="丂" glyph-name="null" horiz-adv-x="256" d="M18 193H237V173H93L82 118H217Q217 31 209 5T163 -22Q141 -22 117 -18L111 2Q139 -2 162 -2Q182 -2 188 14T195 98H57L71 173H18V193Z"/>
    <glyph unicode="七" glyph-name="null" horiz-adv-x="256" d="M241 142L243 123L110 108V15Q110 9 115 7T160 5Q201 5 209 10Q215 14 218 45L239 39Q235 1 226 -7T160 -15Q109 -15 103 -13Q89 -8 89 11V106L16 98L13 117L89 125V201H110V128L241 142Z"/>
  </defs>
  <path style="font-size:800px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;font-family:Droid Sans Fallback;-inkscape-font-specification:Droid Sans Fallback" d="" id="tpl-path"/>
</svg>

XML ( $block)

参数block为以下文档:

<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" > <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
<defs >
<font id="Hanzi-Pinyin-ruby-font" horiz-adv-x="254" ><font-face
    font-family="Droid Sans Fallback"
    units-per-em="256"
    panose-1="2 11 5 2 0 0 0 0 0 1"
    ascent="267"
    descent="-68"
    alphabetic="0" />
<missing-glyph horiz-adv-x="256" d="M75 183H181V0H75V183ZM88 13H167V170H88V13Z" />
<glyph unicode="&#x4e00;" glyph-name="null" horiz-adv-x="256" d="M21 112H234V93H21V112Z" />
<glyph unicode="&#x4e01;" glyph-name="null" horiz-adv-x="256" d="M16 196H239V177H144V5Q144 -9 138 -15T113 -21Q98 -21 84 -19L80 1Q95 -2 110 -2Q118 -2 121 0T124 10V177H16V196Z" />
<glyph unicode="&#x4e02;" glyph-name="null" horiz-adv-x="256" d="M18 193H237V173H93L82 118H217Q217 31 209 5T163 -22Q141 -22 117 -18L111 2Q139 -2 162 -2Q182 -2 188 14T195 98H57L71 173H18V193Z" />
<glyph unicode="&#x4e03;" glyph-name="null" horiz-adv-x="256" d="M241 142L243 123L110 108V15Q110 9 115 7T160 5Q201 5 209 10Q215 14 218 45L239 39Q235 1 226 -7T160 -15Q109 -15 103 -13Q89 -8 89 11V106L16 98L13 117L89 125V201H110V128L241 142Z" />
</font>
</defs>
</svg>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<!--
@description
  Place hanzi and pinyin in the correct element
@upstream: true
-->
<xsl:stylesheet version="2.0"
  xmlns="http://www.w3.org/2000/svg"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:cc="http://creativecommons.org/ns#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"

  exclude-result-prefixes="#default dc cc rdf svg xlink xsl xs"
>

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<!-- ++++++++++++++++++++++++++ PARAM +++++++++++++++++++++++++ -->
<xsl:variable  name="emptyString" select="''" />
<xsl:param   name="hanzi" select="'hanzi'" />
<xsl:param   name="unicode" select="'unicode'" />
<xsl:param   name="pinyin" select="'pinyin'" />

<xsl:param   name="block" select="'NO-TEMPLATE'" />
<xsl:param   name="blockDoc" select="document($block)" />


<!-- ++++++++++++++++++++++++++ TEMPLATE +++++++++++++++++++++++++ -->
<xsl:template match="/">
  <xsl:message>   WIP: <xsl:value-of select="$block" /></xsl:message>
  <xsl:apply-templates />
</xsl:template>


<!-- block holding glyph information -->
<xsl:template match="svg:font">
  <xsl:message>count: <xsl:value-of select="count($blockDoc/svg:svg/svg:defs/svg:font/svg:glyph)" /></xsl:message>
  <xsl:apply-templates select="child::*" />
  <xsl:apply-templates mode="from-block"
    select="$blockDoc/svg:svg/svg:defs/svg:font/svg:glyph" />
</xsl:template>


<!-- the character name -->
<xsl:template match="svg:glyph" mode="from-block">
  <!-- <xsl:message>  copying: <xsl:value-of select="@unicode" /></xsl:message> -->

  <xsl:copy-of select="." />
  <!-- <xsl:apply-templates /> -->
</xsl:template>


<!-- the character name -->
<xsl:template match="@glyph-name">
  <xsl:variable  name="unicode" select="codepoints-to-string(.)" />
  <xsl:variable  name="hanzi" select="'hanzi'" />
  <xsl:variable  name="pinyin" select="'pinyin'" />

    <xsl:attribute name="glyph-name">
      <xsl:value-of select="concat($unicode, ': ', $hanzi, ' /', $pinyin,'/')" />
    </xsl:attribute>
</xsl:template>


<!-- the glyph -->
<xsl:template match="@unicode">
    <xsl:variable  name="unicode" select="." />
    <xsl:variable  name="hanzi" select="codepoints-to-string($unicode)" />

    <xsl:attribute name="unicode">
      <xsl:value-of select="$hanzi" />
    </xsl:attribute>
</xsl:template>


<xsl:template match="*[@id='hanzi-glyph']/text()">
  <xsl:value-of select="$hanzi" />
</xsl:template>


<xsl:template match="*[@id='pinyin-text']/text()">
  <xsl:value-of select="$pinyin" />
</xsl:template>


<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

错误

Validation error at xsl:copy on line 92 of merge-svg.xsl:
  FORG0001: Cannot convert string "\u4e02" to an integer
  at xsl:apply-templates (file:/mnt/data/projects/Hanzi-Pinyin-Font/resources/xslt/merge-svg.xsl#93)
     processing /svg/defs[1]/glyph[1]
  at xsl:apply-templates (file:/mnt/data/projects/Hanzi-Pinyin-Font/resources/xslt/merge-svg.xsl#93)
     processing /svg/defs[1]
  at xsl:apply-templates (file:/mnt/data/projects/Hanzi-Pinyin-Font/resources/xslt/merge-svg.xsl#36)
     processing /svg
Transformation failed: Run-time errors were reported

问题

我正在使用Saxon-HE 9.5.1.2J以下命令:

java -jar bin/saxon9.jar \
    -o:./.tmp/batik-unified.svg.tmp \
    -xsl:./resources/xslt/merge-svg.xsl \
    block=./.tmp/batik-CJK_Unified_Ideographs.svg \
    ./.tmp/batik-unified.svg

我该如何解决这个问题?

4

1 回答 1

2

使用concatandtranslate函数以及disable-output-escaping(如果需要,在 XSLT 1.0 模式下)属性将其从 JavaScript 样式代码点(“\u4E02”)转换为 XML 实体(“丂”):

例如,这个 XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
<foo>bar</foo>
</root>

使用此样式表:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml">

<xsl:template match="/">
<xsl:variable name="unicode" select="'\u4E02'"/>
<xsl:variable name="hanzi" select="concat(translate(translate( $unicode, '\u', '&amp;#' ),'E','e'),';')"/>

<xsl:value-of select="concat(substring-before($hanzi,'#'),'#x',substring-after($hanzi,'#'))" disable-output-escaping="yes"/>

</xsl:template>
</xsl:stylesheet>

参考

于 2014-10-03T01:21:37.840 回答