2016-08-24 2 views
0

Ich versuche Flash-Text-Format in HTML-basierten Text zu konvertieren.Wie ersetze ich XML für gemeinsame Knoten mit XSLT durch unterschiedliche Datenstrukturen?

XML-Quelle.

<TEXTFORMAT LEADING="2"> 
    <P ALIGN="RIGHT"> 
     <FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">left tefxt </FONT> 
    </P> 
</TEXTFORMAT> 
<TEXTFORMAT LEADING="2"> 
    <P ALIGN="JUSTIFY"> 
     <FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">@#dgsdg 
      <FONT FACE="Gabriola">sdfgdfg</FONT> dsfg df 
      <FONT SIZE="16">gdsfg</FONT>sd sd 
      <I>fg df</I> gsdg sdgfgsd gdfg </FONT> 
    </P> 
</TEXTFORMAT> 
<TEXTFORMAT LEADING="2"> 
    <P ALIGN="JUSTIFY"> 
     <FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">fdsgd sdfg </FONT> 
    </P> 
</TEXTFORMAT> 
<TEXTFORMAT LEADING="2"> 
    <P ALIGN="LEFT"> 
     <FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0"> reter erret erret wertwer tert</FONT> 
    </P> 
</TEXTFORMAT> 
<TEXTFORMAT LEADING="2"> 
    <P ALIGN="LEFT"> 
     <FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">ertyryrt</FONT> 
    </P> 
</TEXTFORMAT> 

muss ich alle Elemente der Schrift umwandeln Zugriff

<FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">@#dgsdg 
        <FONT FACE="Gabriola">sdfgdfg</FONT> dsfg df 
        <FONT SIZE="16">gdsfg</FONT>sd sd 
        <I>fg df</I> gsdg sdgfgsd gdfg </FONT> 

in einer Struktur wie dieser

<span style="font-family:Lato; font-size:12px; color:#4B4B4B;"> 
    @#dgsdg<span style="font-family:Gabriola;">sdfgdfg</span> dsfg df 
    <span style="font-size:16px;">gdsfg</span>sd sd 
       <i>fg df</i> gsdg sdgfgsd gdfg 
</span> 

Die obigen Datenstrukturen eine Menge variieren (die Schriftblöcke).

Wie kann ich alle Tags ersetzen und den Stil hinzufügen? Ist es möglich, den untergeordneten Knoten von untergeordneten Knoten zu durchlaufen?

Mein XSLT

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="div"> 
    <xsl:strip-space elements="*"/> 
     <div> 
      <xsl:for-each select="TEXTFORMAT"> 
       <xsl:if test="P"> 
        <span> 
         <xsl:attribute name="style"> 
          <xsl:value-of select="'align:'" /><xsl:value-of select="P/@ALIGN" />; 
         </xsl:attribute> 
         <xsl:for-each select="P/FONT"> 
          <span> 
           <xsl:attribute name="style"> 
            <xsl:value-of select="'font-family:'" /><xsl:value-of select="@FACE" />; 
            <xsl:value-of select="'font-size:'" /><xsl:value-of select="@SIZE" />; 
            <xsl:value-of select="'color:'" /><xsl:value-of select="@COLOR" />; 
           </xsl:attribute> 
          </span> 
         </xsl:for-each> 
        </span> 
        <br/> 
       </xsl:if> 

     </div> 
    </xsl:template> 

</xsl:stylesheet> 

HINWEIS: Es besteht die Möglichkeit von n Anzahl von Tags innerhalb eines Tags

+0

Geben Sie Ihre versuchte XSLT ein. –

+0

@RudramuniTP aktualisiert – tymspy

Antwort

1

Sie vielleicht so etwas wie (Anmerkung, die ich für einen <root> Tag zu Ihrer Eingabe xml hinzugefügt haben machen Sie es gültig)

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="1.0"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes" method="html"/> 

    <!-- identity template --> 

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

    <xsl:template match="root"> 
     <div> 
      <xsl:apply-templates/> 
     </div> 
    </xsl:template> 

    <xsl:template match="FONT"> 
     <span> 
      <xsl:attribute name="style"> 
       <!-- collect attributes --> 
       <xsl:variable name="styles"> 
        <xsl:if test="@FACE"> 
         <xsl:value-of select="concat('font-family:', @FACE)"/> 
         <xsl:text>; </xsl:text> 
        </xsl:if> 
        <xsl:if test="@SIZE"> 
         <xsl:value-of select="concat('font-size:', @SIZE, 'px')"/> 
         <xsl:text>; </xsl:text> 
        </xsl:if> 
        <xsl:if test="@COLOR"> 
         <xsl:value-of select="concat('color:', @COLOR)"/> 
         <xsl:text>;</xsl:text> 
        </xsl:if> 
       </xsl:variable> 
       <!-- delete trailing spaces --> 
       <xsl:value-of select="normalize-space($styles)"/> 
      </xsl:attribute> 
      <xsl:apply-templates/> 
     </span> 
    </xsl:template> 

    <!-- remove unwanted attributes --> 
    <xsl:template match="@LETTERSPACING|@KERNING"/> 

    <xsl:template match="I"> 
     <i><xsl:apply-templates/></i> 
    </xsl:template> 

</xsl:stylesheet> 
Verwandte Themen