Avatar billede shemeri Praktikant
24. marts 2005 - 01:37 Der er 4 kommentarer og
2 løsninger

XSLT Transformation!!

Hej,

Jeg har en XSLT library der transform en xml til anden. Problemet er at jeg valgte xml som output og der får jeg alt for mange mellem-rum! Hvordan kan jeg slette al mellem-rum?

min xslt kode ser sådan ud:
----------------------------
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />

<xsl:template match='path'>
<xsl:element name='path'>
  <xsl:attribute name='d'>
  <xsl:apply-templates mode='in_path' />
  </xsl:attribute>
</xsl:element>
</xsl:template>

<xsl:template match='Move' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'>M</xsl:when>
  <xsl:otherwise>m</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="x" /><xsl:text>,</xsl:text><xsl:value-of select="y" />
</xsl:template>

<xsl:template match='Line' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'><xsl:text>L</xsl:text></xsl:when>
  <xsl:otherwise><xsl:text>l</xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select="x" /><xsl:text>,</xsl:text><xsl:value-of select="y" />
</xsl:template>

<xsl:template match='HLine' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'><xsl:text>H</xsl:text></xsl:when>
  <xsl:otherwise><xsl:text>h</xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select='x' />
</xsl:template>

<xsl:template match='VLine' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'><xsl:text>V</xsl:text></xsl:when>
  <xsl:otherwise><xsl:text>v</xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:value-of select='y' />
</xsl:template>

<xsl:template match='Cubic' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'><xsl:text>C</xsl:text></xsl:when>
  <xsl:otherwise><xsl:text>c</xsl:text></xsl:otherwise>
</xsl:choose>
  <xsl:value-of select='xc1' />
<xsl:choose>
<xsl:when test="number(yc1) &lt; number(0)"><xsl:value-of select='yc1' /></xsl:when>
<xsl:otherwise><xsl:text>,</xsl:text><xsl:value-of select='yc1' /></xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="number(xc2) &lt; number(0)"><xsl:value-of select='xc2' /></xsl:when>
<xsl:otherwise><xsl:text>,</xsl:text><xsl:value-of select='xc2' /></xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="yc2 &lt; 0"><xsl:value-of select='yc2' /></xsl:when>
<xsl:otherwise><xsl:text>,</xsl:text><xsl:value-of select='yc2' /></xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="x &lt; 0"><xsl:value-of select='x' /></xsl:when>
<xsl:otherwise><xsl:text>,</xsl:text><xsl:value-of select='x' /></xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="y &lt; 0"><xsl:value-of select='y' /></xsl:when>
<xsl:otherwise><xsl:text>,</xsl:text><xsl:value-of select='y' /></xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match='SCubic' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'>
  <xsl:text>S</xsl:text>
  </xsl:when>
  <xsl:otherwise>
  <xsl:text>s</xsl:text>
  </xsl:otherwise>
</xsl:choose>
<xsl:value-of select='xc2' /><xsl:text>,</xsl:text>
<xsl:value-of select='yc2' /><xsl:text>,</xsl:text>
<xsl:value-of select='x' /><xsl:text>,</xsl:text>
<xsl:value-of select='y' /><xsl:text>,</xsl:text> 
</xsl:template>

<xsl:template match='Quadratic' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'>
  <xsl:text>Q</xsl:text>
  </xsl:when>
  <xsl:otherwise>
  <xsl:text>q</xsl:text>
  </xsl:otherwise>
</xsl:choose>
<xsl:value-of select='xc' /><xsl:text>,</xsl:text>
<xsl:value-of select='yc' /><xsl:text>,</xsl:text>
<xsl:value-of select='x' /><xsl:text>,</xsl:text>
<xsl:value-of select='y' /><xsl:text>,</xsl:text> 
</xsl:template>


<xsl:template match='SQuadratic' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'>
  <xsl:text>T</xsl:text>
  </xsl:when>
  <xsl:otherwise>
  <xsl:text>t</xsl:text>
  </xsl:otherwise>
</xsl:choose>
<xsl:value-of select='x' /><xsl:text>,</xsl:text>
<xsl:value-of select='y' /><xsl:text>,</xsl:text> 
</xsl:template>


<xsl:template match='Arc' mode='in_path'>
<xsl:choose>
  <xsl:when test='@position="absolute"'>
  <xsl:text>A</xsl:text>
  </xsl:when>
  <xsl:otherwise>
  <xsl:text>a</xsl:text>
  </xsl:otherwise>
</xsl:choose>
<xsl:value-of select='rx' /><xsl:text>,</xsl:text>
<xsl:value-of select='ry' /><xsl:text>,</xsl:text>
<xsl:value-of select='rot' /><xsl:text>,</xsl:text>
<xsl:value-of select='lf' /><xsl:text>,</xsl:text>
<xsl:value-of select='sf' /><xsl:text>,</xsl:text>
<xsl:value-of select='x' /><xsl:text>,</xsl:text>
<xsl:value-of select='y' /><xsl:text>,</xsl:text> 
</xsl:template>

<xsl:template match='close' mode='in_path'>
<xsl:text>Z</xsl:text>
</xsl:template>

--------------------------------
min source xml fil ser sådan ud:
---------------------------------
<path>
<d>
<Move position="absolute">
<x>0</x><y>312</y>
</Move>
<Cubic position="absolute">
<xc1>40</xc1><yc1>48</yc1><xc2>120</xc2><yc2>-32</yc2><x>160</x><y>-6</y>
</Cubic>
<Cubic position="absolute">
<xc1>0</xc1><yc1>0</yc1><xc2>5</xc2><yc2>4</yc2><x>10</x><y>-3</y>
</Cubic>
<Cubic position="absolute">
<xc1>10</xc1><yc1>-103</yc1><xc2>50</xc2><yc2>-83</yc2><x>90</x><y>-42</y>
</Cubic>
<Cubic position="absolute">
<xc1>0</xc1><yc1>0</yc1><xc2>20</xc2><yc2>12</yc2><x>30</x><y>7</y>
</Cubic>
<Cubic position="absolute">
<xc1>-2</xc1><yc1>12</yc1><xc2>-18</xc2><yc2>17</yc2><x>-40</x><y>17</y>
</Cubic>
<Cubic position="absolute">
<xc1>-55</xc1><yc1>-2</yc1><xc2>-40</xc2><yc2>25</yc2><x>-20</x><y>35</y>
</Cubic>
<Cubic position="absolute">
<xc1>30</xc1><yc1>20</yc1><xc2>35</xc2><yc2>65</yc2><x>-30</x><y>71</y>
</Cubic>
<Cubic position="relative">
<xc1>-50</xc1><yc1>4</yc1><xc2>-170</xc2><yc2>4</yc2><x>-200</x><y>-79</y>
</Cubic>

<close />
</d>
</path>


---------------------------------------------
min xml output ser sådan ud med al de mellem-rum
---------------------------------------------
<path d="


M 0 , 312

C 40 , 48 , 120 -32 , 160 -6
C 0 , 0 , 5 , 4 , 10 -3
C 10 -103 , 50 -83 , 90 -42
C 0 , 0 , 20 , 12 , 30 , 7
C -2 , 12 -18 , 17 -40 , 17
C -55 -2 -40 , 25 -20 , 35
C 30 , 20 , 35 , 65 -30 , 71
c -50 , 4 -170 , 4 -200 -79

Z

"/>

--------------------------------------
xml output skal helst ser sådan ud istedet
--------------------------------------
<path d="M 0,312 C 40,48,120-32,160-6 C 0,0,5,4,10-3 C 10-103,50-83,90-42 C 0,0,20,12,30,7 C-2,12-18,17-40,17 C -55,-2,-40,25,-20,35 C 30,20, 35,65,-30,71 c -50,4,-170,4,-200,-79 Z"/>

altså alt på én linie

er der nogen der kan hjælpe :-)

tak
Avatar billede softspot Forsker
24. marts 2005 - 07:23 #1
Prøv evt. at sætte indent="no"

<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
Avatar billede shemeri Praktikant
24. marts 2005 - 16:01 #2
Nej det virker ikke :(
Avatar billede janegil Nybegynder
29. marts 2005 - 15:37 #3
Jeg har en gang 'løst' problemet ved å fjerne linjeksift fra XSLT-dokumentet.

Det gir ikke vakker XSLT-kode.
Avatar billede Slettet bruger
30. marts 2005 - 00:09 #4
Jeg tog en "cut 'n' paste" fra ovenstående til Cooktop 2.5 (bruger MSXSML) og fik en nydelig og pæn d attribute (manglede nogle mellemrum mellem bogstaverne og de efterfølgende tal, men altså uden de mange linjeskift...)

/CS
Avatar billede shemeri Praktikant
30. marts 2005 - 16:59 #5
jeg har prøvet med saxon og sax api i java og Stylus Studio XML Professional Edition og flere. Stylus gav ovenstående resultat. Jeg har prøvet med saxon hvor al mellemrum blev oversættet til &#xA; og gav følgene resultat:-

D:\test>java -jar saxon8.jar test.xml XML_to_path.xsl
Warning: Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor
<path d="&#xA;&#xA;&#xA;M10,10&#xA;&#xA;l20,10&#xA;&#xA;l20,30&#xA;&#xA;M40,40&#
xA;&#xA;l55,35&#xA;&#xA;C40,48,120,-32,160,-6&#xA;&#xA;c0,0,5,4,10,-3&#xA;&#xA;C
10,-103,50,-83,90,-42&#xA;&#xA;c0,0,20,12,30,7&#xA;&#xA;V76&#xA;&#xA;v90&#xA;&#x
A;H76&#xA;&#xA;h90&#xA;&#xA;s77,91,92,28&#xA;&#xA;S77,91,79,50&#xA;&#xA;q77,32,1
1,77&#xA;&#xA;Q77,32,11,77&#xA;&#xA;t21,97&#xA;&#xA;T31,67&#xA;&#xA;a78,88,10,13
,19,23.9,9.89&#xA;&#xA;A78,88,10,13,19,23.9,9.89&#xA;&#xA;Z&#xA;&#xA;"/>

jeg har prøvet med sax api i java interface og gave følgende resultat hvor al mellemrum blev oversættet til &#10;

<path d="&#10;&#10;&#10;M10,10&#10;10;&#10;l20,10&#10;&#10;l20,30&#10;&#10;M40,40
&#10;&#10;l55,35&#10;&#10;C40,48,120,-32,160,-6&#10;&#10;c0,0,5,4,10,-3
&#10;&#10;C10,-103,50,-83,90,-42&#10;&#10;c0,0,20,12,30,7&#10;&#10;V76&#10;&#10;
v90&#10;&#10;H76&#10;&#10;h90&#10;&#10;s77,91,92,28&#10;&#10;S77,91,79,50&#10;&#10;
q77,32,11,77&#10;&#10;Q77,32,11,77&#10;&#10;t21,97&#10;&#10;T31,67&#10;&#10;a78,88,
10,13,19,23.9,9.89&#10;&#10;A78,88,10,13,19,23.9,9.89&#10;&#10;Z&#10;&#10;"/>

hjælp!! :-(
Avatar billede shemeri Praktikant
01. april 2005 - 19:34 #6
Jeg fandt en løsning!

SIMPLY jeg tilføj strip-space til min XSLT
<xsl:strip-space element="*" />

;-)

tak allesammen for hjælpen.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester