我有一个XML文件(FileMaker fm14布局fmxmlsnippet),其中包含HexData标记中的SVG文件,如下所示:
<HexData>3C73766720786D6C6E733D22687474703A2F2F7777772E77332E6F72672F323030302F7376672220786D6C6E733A786C696E6B3D22687474703A2F2F7777772E77332E6F72672F313939392F786C696E6B2220786D6C3A73706163653D227072657365727665222077696474683D22313030707822206865696768743D223436707822207374796C653D2273686170652D72656E646572696E673A67656F6D6574726963507265636973696F6E3B20746578742D72656E646572696E673A67656F6D6574726963507265636973696F6E3B20696D6167652D72656E646572696E673A6F7074696D697A655175616C6974793B2066696C6C2D72756C653A6576656E6F64643B20636C69702D72756C653A6576656E6F6464222076696577426F783D2230203020382E383931343520342E3035363539223E0D0A3C6720636C6173733D22666D5F66696C6C223E0A203C646566733E0A20203C7374796C6520747970653D22746578742F637373223E0A2020200A202020202E66696C30207B66696C6C3A233041304230437D0A2020200A20203C2F7374796C653E0A203C2F646566733E0A203C672069643D224C617965725F78303032305F31223E0A20203C6D657461646174612069643D22436F72656C436F727049445F30436F72656C2D4C61796572222F3E0A20203C7061746820636C6173733D2266696C302220643D224D352E303432343820312E303032323363302E3930393132372C2D302E35373033313620322E30353835362C2D302E39393436353520332E33373236382C2D302E37393537343820302E34353831342C2D302E33363037343520302E37303732342C302E35383434333420302E3138303238342C302E343235373532202D302E3936393639372C2D302E30363832393636202D312E38393233372C302E313636353038202D322E37313135332C312E3036383233202D302E303534333332362C302E30363831393732202D302E303933313939392C302E3131323635202D302E3132363632352C302E313437323638202D302E3231333230322C2D302E333237383238202D302E3435313437332C2D302E363039363639202D302E3731343831322C2D302E383435357A6D2D342E3836393238202D302E37373032333863302E3139313632312C2D302E33323233383620322E31373835362C2D302E32393632353720322E33313739332C3020302E3137313138382C302E353135353320302E3138313639372C312E313831303720302E3130323039372C312E3933303733202D302E303139353832372C302E33323530373920302E32303930342C302E33333234383620302E3630373439342C302E31323235353220302E3137323737382C2D302E3038363436363320302E3631323936392C2D302E33353433333220312E30373635372C2D302E37333139353620302E3138313232332C2D302E313437363120302E3337373633352C2D302E32393532353320302E3538383138372C2D302E34333632393420302E3237363232322C302E32373032373220302E3531373533392C302E35363538313220302E3731383434342C302E383930363539202D302E303839383939332C302E30383433363839202D302E3231363931312C302E323336333934202D302E3530303432392C302E353432373835202D302E3636343438372C302E373138303931202D322E303533372C312E3535333939202D332E31343230352C312E353034202D302E3830313035382C2D302E30333637393231202D312E35303835352C2D302E323733393831202D312E37363832352C2D312E3333373333202D302E3236373636372C2D312E3039353936202D302E3139313233352C2D312E393131373520302C2D322E34383531357A6D312E3136393131202D302E31323238333963302E343238372C3020302E3737363335332C302E3033353039323120302E3737363335332C302E3037383337343920302C302E30343332383238202D302E3334373635332C302E30373833373439202D302E3737363335332C302E30373833373439202D302E3432383638392C30202D302E3737363334322C2D302E30333530393231202D302E3737363334322C2D302E3037383337343920302C2D302E3034333238323820302E3334373635332C2D302E3037383337343920302E3737363334322C2D302E303738333734397A222F3E0A203C2F673E0A3C2F673E0D0A3C2F7376673E</HexData>
(周围XML的其余部分并不重要)
我需要从HexData中解码SVG文件
在其他语言中,这可以通过一个循环解决:
循环很容易在XSLT 1.0中实现,作为递归命名模板,读取每两个十六进制数字 - 但它结束:XSLT 1.0缺少char()函数!
我的第一个字符转换解决方案 - 简单地将十六进制字符转换为十六进制实体 - 有点半成功......
以下命名模板:
<xsl:template name="HexData.toString">
<xsl:param name="HexData"/>
<!-- -->
<xsl:if test="string-length($HexData)>1">
<!-- Output char as hex entity -->
<xsl:value-of select="'&#x'" disable-output-escaping="yes"/>
<xsl:value-of select="translate(substring($HexData,1,2),'abcdef','ABCDEF')"/>
<xsl:value-of select="';'"/>
<!-- recurse -->
<xsl:call-template name="HexData.toString">
<xsl:with-param name="HexData" select="substring($HexData,3)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
... 几乎会产生正确的结果:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="100px" height="46px" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 8.89145 4.05659">
<g class="fm_fill">
 <defs>
  <style type="text/css">
   
    .fil0 {fill:#0A0B0C}
   
  </style>
 </defs>
 <g id="Layer_x0020_1">
  <metadata id="CorelCorpID_0Corel-Layer"/>
  <path class="fil0" d="M5.04248 1.00223c0.909127,-0.570316 2.05856,-0.994655 3.37268,-0.795748 0.45814,-0.360745 0.70724,0.584434 0.180284,0.425752 -0.969697,-0.0682966 -1.89237,0.166508 -2.71153,1.06823 -0.0543326,0.0681972 -0.0931999,0.11265 -0.126625,0.147268 -0.213202,-0.327828 -0.451473,-0.609669 -0.714812,-0.8455zm-4.86928 -0.770238c0.191621,-0.322386 2.17856,-0.296257 2.31793,0 0.171188,0.51553 0.181697,1.18107 0.102097,1.93073 -0.0195827,0.325079 0.20904,0.332486 0.607494,0.122552 0.172778,-0.0864663 0.612969,-0.354332 1.07657,-0.731956 0.181223,-0.14761 0.377635,-0.295253 0.588187,-0.436294 0.276222,0.270272 0.517539,0.565812 0.718444,0.890659 -0.0898993,0.0843689 -0.216911,0.236394 -0.500429,0.542785 -0.664487,0.718091 -2.0537,1.55399 -3.14205,1.504 -0.801058,-0.0367921 -1.50855,-0.273981 -1.76825,-1.33733 -0.267667,-1.09596 -0.191235,-1.91175 0,-2.48515zm1.16911 -0.122839c0.4287,0 0.776353,0.0350921 0.776353,0.0783749 0,0.0432828 -0.347653,0.0783749 -0.776353,0.0783749 -0.428689,0 -0.776342,-0.0350921 -0.776342,-0.0783749 0,-0.0432828 0.347653,-0.0783749 0.776342,-0.0783749z"/>
 </g>
</g>
</svg><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="100px" height="46px" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 8.89145 4.05659">
<g class="fm_fill">
 <defs>
  <style type="text/css">
   
    .fil0 {fill:#0A0B0C}
   
  </style>
 </defs>
 <g id="Layer_x0020_1">
  <metadata id="CorelCorpID_0Corel-Layer"/>
  <path class="fil0" d="M5.04248 1.00223c0.909127,-0.570316 2.05856,-0.994655 3.37268,-0.795748 0.45814,-0.360745 0.70724,0.584434 0.180284,0.425752 -0.969697,-0.0682966 -1.89237,0.166508 -2.71153,1.06823 -0.0543326,0.0681972 -0.0931999,0.11265 -0.126625,0.147268 -0.213202,-0.327828 -0.451473,-0.609669 -0.714812,-0.8455zm-4.86928 -0.770238c0.191621,-0.322386 2.17856,-0.296257 2.31793,0 0.171188,0.51553 0.181697,1.18107 0.102097,1.93073 -0.0195827,0.325079 0.20904,0.332486 0.607494,0.122552 0.172778,-0.0864663 0.612969,-0.354332 1.07657,-0.731956 0.181223,-0.14761 0.377635,-0.295253 0.588187,-0.436294 0.276222,0.270272 0.517539,0.565812 0.718444,0.890659 -0.0898993,0.0843689 -0.216911,0.236394 -0.500429,0.542785 -0.664487,0.718091 -2.0537,1.55399 -3.14205,1.504 -0.801058,-0.0367921 -1.50855,-0.273981 -1.76825,-1.33733 -0.267667,-1.09596 -0.191235,-1.91175 0,-2.48515zm1.16911 -0.122839c0.4287,0 0.776353,0.0350921 0.776353,0.0783749 0,0.0432828 -0.347653,0.0783749 -0.776353,0.0783749 -0.428689,0 -0.776342,-0.0350921 -0.776342,-0.0783749 0,-0.0432828 0.347653,-0.0783749 0.776342,-0.0783749z"/>
 </g>
</g>
</svg>
嗯,很近但没有雪茄! :d
&#34; Ceci n&#39; est pas une pipe!&#34;
但是,如果您在浏览器中查看上述字符串,则会看到以下内容:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="100px" height="46px" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd" viewBox="0 0 8.89145 4.05659">
<defs>
<style type="text/css">
.fil0 {fill:#0A0B0C}
</style>
</defs>
<g id="Layer_x0020_1">
<metadata id="CorelCorpID_0Corel-Layer"/>
<path class="fil0" d="M5.04248 1.00223c0.909127,-0.570316 2.05856,-0.994655 3.37268,-0.795748 0.45814,-0.360745 0.70724,0.584434 0.180284,0.425752 -0.969697,-0.0682966 -1.89237,0.166508 -2.71153,1.06823 -0.0543326,0.0681972 -0.0931999,0.11265 -0.126625,0.147268 -0.213202,-0.327828 -0.451473,-0.609669 -0.714812,-0.8455zm-4.86928 -0.770238c0.191621,-0.322386 2.17856,-0.296257 2.31793,0 0.171188,0.51553 0.181697,1.18107 0.102097,1.93073 -0.0195827,0.325079 0.20904,0.332486 0.607494,0.122552 0.172778,-0.0864663 0.612969,-0.354332 1.07657,-0.731956 0.181223,-0.14761 0.377635,-0.295253 0.588187,-0.436294 0.276222,0.270272 0.517539,0.565812 0.718444,0.890659 -0.0898993,0.0843689 -0.216911,0.236394 -0.500429,0.542785 -0.664487,0.718091 -2.0537,1.55399 -3.14205,1.504 -0.801058,-0.0367921 -1.50855,-0.273981 -1.76825,-1.33733 -0.267667,-1.09596 -0.191235,-1.91175 0,-2.48515zm1.16911 -0.122839c0.4287,0 0.776353,0.0350921 0.776353,0.0783749 0,0.0432828 -0.347653,0.0783749 -0.776353,0.0783749 -0.428689,0 -0.776342,-0.0350921 -0.776342,-0.0783749 0,-0.0432828 0.347653,-0.0783749 0.776342,-0.0783749z"/>
</g>
</svg>
如果您复制那个字符串并再次在浏览器中查看它,您将获得我想要提取的SVG图像:
那么如何在单个XSLT 1.0转换中从HexData到SVG XML?
具体做法是: