2014-04-18 17 views
5

Ho un file XML che memorizza i dati. Sto usando un XSL per generare file HTML da quel file XML. Quando provo a farlo ottengo l'errore Illegal HTML character: decimal 150Come gestire i caratteri HTML non validi in XSL

Non sono autorizzato a modificare il file XML. Devo mappare quell'uno e molti altri personaggi illegali a un personaggio legale (può essere qualsiasi) in XSL. Quindi deve fare quella mappatura in modo generico, non solo per un tipo di personaggio.

risposta

7

è possibile definire una mappa dei caratteri che mappa i caratteri non sono ammessi a un permesso, per esempio uno spazio:

<xsl:output indent="yes" method="html" use-character-maps="m1"/> 

<xsl:character-map name="m1"> 
    <xsl:output-character character="&#150;" string=" "/> 
</xsl:character-map> 

In alternativa, utilizzare un modello di sostituzione di tutti i caratteri non validi, secondo http://www.w3.org/TR/xslt-xquery-serialization/#HTML_CHARDATA questi sono il controllo caratteri # x7f- # X9F in modo da utilizzare

<xsl:template match="text()"> 
    <xsl:value-of select="replace(., '[&#x007F;-&#x009F;]', ' ')"/> 
</xsl:template> 

dovrebbe assicurarsi che tali personaggi di nodi di testo nel documento di input sono sostituiti da uno spazi.

Come alternativa, è possibile prendere in considerazione l'output di XHTML con elementi negli spazi dei nomi XHTML e nel metodo di output xhtml.

Sulla base della lista di caratteri, una mappa completa carattere mappatura di tutti i caratteri di controllo illegali per uno spazio è

<xsl:character-map 
        name="no-control-characters"> 
    <xsl:output-character character="&#127;" string=" "/> 
    <xsl:output-character character="&#128;" string=" "/> 
    <xsl:output-character character="&#129;" string=" "/> 
    <xsl:output-character character="&#130;" string=" "/> 
    <xsl:output-character character="&#131;" string=" "/> 
    <xsl:output-character character="&#132;" string=" "/> 
    <xsl:output-character character="&#133;" string=" "/> 
    <xsl:output-character character="&#134;" string=" "/> 
    <xsl:output-character character="&#135;" string=" "/> 
    <xsl:output-character character="&#136;" string=" "/> 
    <xsl:output-character character="&#137;" string=" "/> 
    <xsl:output-character character="&#138;" string=" "/> 
    <xsl:output-character character="&#139;" string=" "/> 
    <xsl:output-character character="&#140;" string=" "/> 
    <xsl:output-character character="&#141;" string=" "/> 
    <xsl:output-character character="&#142;" string=" "/> 
    <xsl:output-character character="&#143;" string=" "/> 
    <xsl:output-character character="&#144;" string=" "/> 
    <xsl:output-character character="&#145;" string=" "/> 
    <xsl:output-character character="&#146;" string=" "/> 
    <xsl:output-character character="&#147;" string=" "/> 
    <xsl:output-character character="&#148;" string=" "/> 
    <xsl:output-character character="&#149;" string=" "/> 
    <xsl:output-character character="&#150;" string=" "/> 
    <xsl:output-character character="&#151;" string=" "/> 
    <xsl:output-character character="&#152;" string=" "/> 
    <xsl:output-character character="&#153;" string=" "/> 
    <xsl:output-character character="&#154;" string=" "/> 
    <xsl:output-character character="&#155;" string=" "/> 
    <xsl:output-character character="&#156;" string=" "/> 
    <xsl:output-character character="&#157;" string=" "/> 
    <xsl:output-character character="&#158;" string=" "/> 
    <xsl:output-character character="&#159;" string=" "/> 
</xsl:character-map> 

ho generato quella lista con XSLT 2.0 e sassone, utilizzando

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" 
    exclude-result-prefixes="xs axsl"> 

<xsl:param name="start" as="xs:integer" select="127"/> 
<xsl:param name="end" as="xs:integer" select="159"/> 

<xsl:param name="replacement" as="xs:string" select="' '"/> 

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/> 

<xsl:output method="xml" indent="yes" use-character-maps="character-reference"/> 

<xsl:character-map name="character-reference"> 
    <xsl:output-character character="«" string="&amp;"/> 
</xsl:character-map> 

<xsl:template name="main"> 
    <axsl:character-map name="no-control-characters"> 
    <xsl:for-each select="$start to $end"> 
     <axsl:output-character character="«#{.};" string="{$replacement}"/> 
    </xsl:for-each> 
    </axsl:character-map> 
</xsl:template> 

</xsl:stylesheet> 
+0

Grazie, ha risolto il problema per 150 decimali. Ma come posso mappare più di 1 tipo? C'è un modo generico? – US1925

+0

Puoi inserire tutti gli elementi 'xsl: output-character' nella' xsl: character-map' di cui hai bisogno. –

Problemi correlati