tags:

views:

197

answers:

1

I have a .xslt that translates xml from one form to another (I'm not using and xsd so I will not say from one schema to another). My problem is that the original file's encoding of ' is lost in translation. This causes me a problem because in my database the names are stored using the apostrophe character not the single quote character and when I'm performing an update with this new data the names don't match because the database has apostrophes in the names and the Java update code which reads my translated xml file has single quotes.

The original file has various lists of card elements that look like this (note value of the name element):

    <card>
        <cost>4BB</cost>
        <color>Black</color>
        <expansion-set>
            <rarity>R</rarity>
            <abbreviation>UD</abbreviation>
        </expansion-set>
        <type>Enchantment</type>
        <ruling>Vintage tournaments (see Rule 901) have restricted this
            card since 1999/10/01.</ruling>
        <ruling>Legacy tournaments (see Rule 902) have banned this card
            since 1999/10/01.</ruling>
        <ruling>Extended tournaments (see Rule 903) have banned this
            card since 1999/08/01.</ruling>
        <note>Note - Also see Skip, Rule G19.11.</note>
        <text>Text(UD): Skip your draw step. ; Pay 1 life: Draw a card.</text>
        <name>Yawgmoth&apos;s Bargain</name>
    </card>

This what the xslt template matches card elements and create the new form I am looking for:

    <xsl:template match="card">
     <card name="{name}">
      <!-- Add the card name -->
<!--      <xsl:attribute name="name">
       <xsl:value-of select="name"/>
      </xsl:attribute>
      <cardname><xsl:value-of select="name"/></cardname>  -->
      <!-- Add the card's rulings -->
      <xsl:apply-templates select="ruling"/>
      <!-- Add the card's notes -->
      <xsl:apply-templates select="note"/>
     </card>
    </xsl:template>

As you can see I am not transferring over everything just the parts I want and I'm turning the name element into an attribute (I have tried keeping the name as an element but see the same problem). The commented out parts are other attempts I have made to get the output I want (the current transform and those commented out all result in the same use of the single quote not the apostrophe).

Here is what a card element looks like in the translated output (note the different value of the name attribute):

<card name="Yawgmoth's Bargain">
<ruling ruleref="901">Vintage tournaments (see Rule 901) have restricted this card since 1999/10/01.</ruling>
<ruling ruleref="902">Legacy tournaments (see Rule 902) have banned this card since 1999/10/01.</ruling>
<ruling ruleref="903">Extended tournaments (see Rule 903) have banned this card since 1999/08/01.</ruling>
<note ruleref="G19.11">Note - Also see Skip, Rule G19.11.</note>
</card>

Now when my database update code (written in Java) reads in that XML, Yawgmoth's Bargain has a single quote character in the string but the database I have created has the name using an apostrophe character. Needless to say my update code does not match the card names.

What is causing this loss of fidelity in the xsl transform?

thanks,

Ian

+2  A: 

&apos; in xml uses the predefined entity reference for ' which is the unicode character number 39 (hex 27), APOSTROPHE which is ' which is what you have in your output.

Your problem, then isn't with the xslt processor.

I presume you are getting one of the single quote characters: RIGHT SINGLE QUOTATION MARK (hex 2019). In which case something else (your database layer?) is 'smartifying' your apostrophe character.

Charles Bailey
Yep, that is it. In this case the "something else" was my own code. On this project I reused some utility classes to escape the strings for SQL. That code has an option to, as you put it, "smartify" the single quotes into right single quotes. Reuse is good right;-)Now that I know what is really going on I'll just adjust my update code to perform the same smartificaiton and all will be well.
Ian Leslie