代码之家  ›  专栏  ›  技术社区  ›  Ian Leslie

'在用xlst处理时被转换为'

  •  0
  • Ian Leslie  · 技术社区  · 15 年前

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

    原始文件包含各种类似的卡片元素列表(注意name元素的值):

        <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属性的不同值):

    <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中读取时,YougMyTH的协议在字符串中有一个单引号字符,但是我创建的数据库使用撇号字符。不用说,我的更新代码与卡名不匹配。

    什么导致了XSL转换中的保真度损失?

    谢谢,

    伊恩

    1 回复  |  直到 15 年前
        1
  •  2
  •   CB Bailey    15 年前

    &apos; 在XML中,为'which is the unicode character number 39(hex 27),apostrophe which is ' 这就是你的输出。

    那么,您的问题不在XSLT处理器上。

    我想你会得到一个单引号字符:右单引号(Hex 2019)。在这种情况下,还需要其他东西(您的数据库层?)使你的撇号字符“变聪明”。