使用xslt处理时“被翻译成”的问题

时间:2009-10-30 18:02:26

标签: xml xslt

我有一个.xslt将xml从一种形式转换为另一种形式(我没有使用和xsd所以我不会说从一个模式到另一个模式)。我的问题是原始文件的'编码'在翻译中丢失了。这导致了一个问题,因为在我的数据库中,使用撇号字符而不是单引号字符存储名称,当我使用此新数据执行更新时,名称不匹配,因为数据库在名称中有撇号,读取我翻译的xml文件的Java更新代码有单引号。

原始文件有各种卡片元素列表,如下所示(注意名称元素的值):

    <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>

这就是xslt模板匹配卡元素并创建我正在寻找的新表单:

    <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>

正如你所看到的那样,我并没有将所有部分转移到我想要的部分,而是将name元素转换为属性(我已经尝试将名称保留为元素但看到同样的问题)。注释掉的部分是我为获得我想要的输出而做的其他尝试(当前转换和那些注释掉的所有结果都使用单引号而不是撇号)。

以下是转换后的输出中卡元素的外观(注意name属性的不同值):

<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>

现在,当我的数据库更新代码(用Java编写)读入该XML时,Yawgmoth的Bargain在字符串中有一个单引号字符,但我创建的数据库的名称使用了撇号字符。不用说我的更新代码与卡名称不匹配。

在xsl转换中导致这种保真度丢失的原因是什么?

感谢,

伊恩

1 个答案:

答案 0 :(得分:2)

xml中的

&apos;使用预定义的实体引用',即unicode字符编号39(十六进制27),APOSTROPHE是',这是您输出中的内容。

您的问题,那就是xslt处理器。

我认为你得到一个单引号字符:右单引号(十六进制2019)。在哪种情况下,其他东西(你的数据库层?)正在“智能化”你的撇号字符。