Conditional Character map?

Here should go questions about transforming XML with XSLT and FOP.
sderrick
Posts: 269
Joined: Sat Jul 10, 2010 4:03 pm

Conditional Character map?

Post by sderrick »

I need to generate output for different media targets from the same xml source.

One of the targets is mobi pocket for the Kindle, with 1980's era markup capability. I have 10-15 unicode characters in the xml, like en-dashes, em-dashes, thin spaces, etc. that the kindle doesn't have support for in it's font set.

I would like to be able to conditionally use a character map in the same xsl script, that would leave the characters alone for html or epub, but replace them if generating for the Kindle.

Is this possible?

thanks,

Scott
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Conditional Character map?

Post by adrian »

Hello,

xsl:character-map allows a "use-when" attribute(Conditional Element Inclusion) but unfortunately its value is evaluated statically so you can't use a parameter or variable to trigger it.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
sderrick
Posts: 269
Joined: Sat Jul 10, 2010 4:03 pm

Replace unicode with html entitys; was

Post by sderrick »

Instead of using character-map I am trying to use the translate function.

Using this works well

translate(.,"&#x2d;,'-')

but this fails

translate(.,'&#xae;,'&reg;')

with

F [Saxon-HE 9.3.0.4] The entity "reg" was not declared, but was referenced in the attribute "select" of the element "xsl:value-of".

How can I get the translate function to output &reg; if it sees &#xae;?

Scott
sderrick
Posts: 269
Joined: Sat Jul 10, 2010 4:03 pm

Re: Conditional Character map?

Post by sderrick »

Hmmm...

Using translate() is going so well..

I tried declaring this

<!DOCTYPE stylesheet [
<!ENTITY reg
"<xsl:text disable-output-escaping='yes'> '&reg;' </xsl:text>">
]>

To allow me to use the &reg; entity, but that causes another error at the point of the &reg; ..

F [Saxon-HE 9.3.0.4] The value of attribute "select" associated with an element type "xsl:value-of" must not contain the '<' character.

?????
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Conditional Character map?

Post by adrian »

Hello,

If you want to use the translate function you need a 1 to 1 mapping of the characters in the second and third argument strings(1 character in the second argument is replaced with the corresponding character from the third argument). But if I understand correctly you want to replace a character with an entity(1 to many): ® -> &reg; so you're going to have to use the replace function instead.

e.g.

Code: Select all

<xsl:value-of select="replace(.,'&#xae;','&reg;')" disable-output-escaping="yes"/>

PS: If you declare an entity and use an XML/XSL snippet as its value, when the stylesheet is compiled the parser will substitute the entity reference with its value so you will obtain an invalid stylesheet(an xml:text element inside the value of the select attribute. Hence the error...

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
sderrick
Posts: 269
Joined: Sat Jul 10, 2010 4:03 pm

Re: Conditional Character map?

Post by sderrick »

I bailed on the utility of having it in a single stylesheet and went with a kludge of having my character-maps in separate style sheets.

Not a neat and tidy as I'm used to when coding in a real programming language but it does work.

thanks for all the input.

Scott
Post Reply