El sentido de este post, que realmente son pruebas que hice cuando me puse hacer el post de Sustituir imagenes y texto en fichero svg de inkscape.
Un poco teoria de SVG generado Inkscape
Sabemos que un fichero svg su formato es XML, por lo en principio nuestro enfoque inicial es buscar información como sustituir contenido de un fichero XML.
XML es un lenguaje de marcado extensible, que fue creado para almacenar y transportar datos, legible tanto para humanos como para dispositivos.
En W3school hay un tutorial bastante completo de XML, dentro del cual podemos encontrar : XPath y Xquery
- XPath se puede utilizar para navegar a través de elementos y atributos en un documento XML.
- XQuery fue diseñado para consultar datos XML. Tiene el mismos sentido de uso que el SQL es para las bases de datos,
Lo queno encuentro es como poder sustituirlo, pero me imagino que XPath se puede realizar, hay que estudiarlo un poco.
Empezamos con XQUERY
Encontré el siguiente documento en internet, que pareció muy interesante, donde explica muy bien la funcionalidad de XQUERY.
Lo primero que hago es comprobar si tengo algún interprete para XQUERY en mi equipo en debian, me imagino que existe nativo en sistema operativo, ya que la intención es hacer peuqepo script en local, encontré xqilla, no lo tenía instalado , por lo que procedo a instalarlo.
apt install xqilla
Ahora creo mi primer fichero datos.xml, sencillo de prueba.
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Ahora creo mi primer fichero miquery.xml, con instrucciones de Xquery
doc("./datos.xml") /bookstore/book/title
El resultado es el siguiente.
▶ xqilla mixquery.xml
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
Bueno , parace que funciona, por lo que empezamos con un fichero svg. Me voy inkscape y creo algo sencillo, sin filigranas:
Como veis , tengo un capa y 5 objetos, sencillo. Lo guardo svg, el resultado fichero es:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Created with Inkscape (http://www.inkscape.org/) --> <svg width="210mm" height="297mm" viewBox="0 0 210 297" version="1.1" id="svg5" xml:space="preserve" inkscape:version="1.2.2 (b0a8486541, 2022-12-01)" sodipodi:docname="dibujo.svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"><sodipodi:namedview id="namedview7" pagecolor="#ffffff" bordercolor="#000000" borderopacity="0.25" inkscape:showpageshadow="2" inkscape:pageopacity="0.0" inkscape:pagecheckerboard="0" inkscape:deskcolor="#d1d1d1" inkscape:document-units="mm" showgrid="false" inkscape:zoom="0.4204826" inkscape:cx="489.91325" inkscape:cy="580.2856" inkscape:window-width="786" inkscape:window-height="724" inkscape:window-x="660" inkscape:window-y="25" inkscape:window-maximized="0" inkscape:current-layer="layer1" /><defs id="defs2" /><g inkscape:label="Capa 1" inkscape:groupmode="layer" id="layer1"><text xml:space="preserve" style="font-weight:bold;font-size:24.3801px;font-family:FontAwesome;-inkscape-font-specification:'FontAwesome Bold';text-align:center;text-anchor:middle;display:inline;opacity:1;fill:#9a0f27;fill-opacity:1;stroke:none;stroke-width:2.711;stroke-dasharray:none;stroke-opacity:1" x="100.35343" y="38.143005" id="text236" transform="scale(1.0406237,0.9609622)"><tspan sodipodi:role="line" id="tspan234" style="fill:#9a0f27;fill-opacity:1;stroke:none;stroke-width:2.711;stroke-dasharray:none;stroke-opacity:1" x="0" y="0">OFERTA</tspan></text><image width="131.02522" height="165.3152" preserveAspectRatio="none" xlink:href="everydayItalian.jpg" id="image2811" x="39.487389" y="41.914894" /><text xml:space="preserve" style="font-weight:bold;font-size:24.3801px;font-family:FontAwesome;-inkscape-font-specification:'FontAwesome Bold';text-align:center;text-anchor:middle;display:inline;fill:#9a0f27;fill-opacity:1;stroke:none;stroke-width:2.711;stroke-dasharray:none;stroke-opacity:1" x="100.35342" y="261.01852" id="text236-3" transform="scale(1.0406237,0.96096217)"><tspan sodipodi:role="line" id="tspan234-6" style="fill:#9a0f27;fill-opacity:1;stroke:none;stroke-width:2.711;stroke-dasharray:none;stroke-opacity:1" x="0" y="0">30,00</tspan></text><text xml:space="preserve" style="font-weight:bold;font-size:16.93333333px;font-family:FontAwesome;-inkscape-font-specification:'FontAwesome Bold';text-align:center;text-anchor:middle;display:inline;fill:#9a0f27;fill-opacity:1;stroke:none;stroke-width:2.711;stroke-dasharray:none;stroke-opacity:1" x="157.00522" y="261.01852" id="text236-3-7" transform="scale(1.0406237,0.96096217)"><tspan sodipodi:role="line" id="tspan234-6-5" style="fill:#9a0f27;fill-opacity:1;stroke:none;stroke-width:2.711;stroke-dasharray:none;stroke-opacity:1;font-size:16.93333333px" x="157.00522" y="261.01852">€</tspan></text></g></svg>
Ahora modifico el fichero miquery.xml, intento mostrar text
doc("./dibujo.svg") /svg/g/text
No Funciona, no se elmotivo.
Pruebo mas generico
doc("./dibujo.svg") /svg/g
Tampoco funciona :-(
Entonces ejecuto directamente:
▶ xqilla dibujo.svg ./dibujo.svg:1:6: error: The target of a processing instruction must not be 'XML' [err:XPST0003] <?xml version="1.0" encoding="UTF-8" standalone="no"?>
Por lo menos me da un error, por el que puedo buscar informacion.