How can I get attribute values to not get converted from ' to ' or & to & or < to > ?

Discussion in 'XML' started by Sammy, May 6, 2005.

  1. Sammy

    Sammy Guest

    Hi, my mind is going crazy. I have tried everything I can think of to
    no
    avail.
    I have tried Disable Output Escaping.
    I tried to think of a way of enclosing the attribute data in a CDATA
    element. That did not parse.

    Here is my question:

    How can I get attribute values to not get converted from ' to '
    or & to & or < to > ?


    It seems like if I take the xsl:value-of an attribute and then output
    that
    value into another xml it get's converted.

    The problem is I am importing this stuff into a DB and I need to keep
    all
    those ' and other escapings..

    Also, is there a way to do something like this
    <![CDATA[
    <xsl:value-of select="@CATEGORY" />
    ]]>

    The problem with this is it gives me
    <xsl:value-of select="@CATEGORY" />

    where I want
    Men&apos;s Clothing

    Thanks any help would be appreciated...


    Here is my source xml, XSL, and outfile:

    Source XML:

    <import>
    <CAT>
    <row CATEGORY="Men&apos;s Clothing" />
    </CAT>
    </import>

    XSL:
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput indent="yes" method="xml" />
    <xsl:strip-space elements="*"/>

    <xsl:template name="frank_data">

    <xsl:param name="id" />


    <xsl:element name="frankgroup">
    <xsl:attribute name="identifier"><xsl:value-of select="$id"
    /></xsl:attribute>
    </xsl:element>

    </xsl:template>

    <xsl:template match="/">

    <xsl:element name="import">
    <xsl:apply-templates />
    </xsl:element>

    </xsl:template>

    <xsl:template match="CAT">
    <xsl:for-each select="*">
    <xsl:call-template name="frank_data">
    <xsl:with-param name="id" select="@CATEGORY" />
    </xsl:call-template>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
    Output:

    <import>
    <frankgroup
    id="Men's Clothing" />
    </import>
    Sammy, May 6, 2005
    #1
    1. Advertising


  2. > How can I get attribute values to not get converted from &apos; to '
    > or &amp; to & or &lt; to > ?


    The whole point about &lt; is that it means the same thing as > (not
    always in element content if the preceding two characters were ]] but in
    attribute values they always mean the same thing.
    If you go
    <foo bar="&gt;"/><foo bar=">"/> Then XSLT will see identical input from
    the two forms, and it may use either form on output. It has no way of
    knowing which form was used, any more than it has of knowing whther yiou
    used " or ' to surround the attribute value.

    > The problem is I am importing this stuff into a DB and I need to keep
    > all those &apos; and other escapings..


    If that import mechanism is not aware of XML conventions so is broken by
    XML quoting syntax they you will have to use the text output method of
    xslt and write out the literal tags that you want rather than allowing
    XSLT to linearise an element tree for you. But you lose most of the
    benefits of xslt doing that.

    so
    <xsl:eek:utput method="text"/>
    ....

    <xsl:template match="xyz">
    &lt;foo bar="<xsl:value-of select="@CATEGORY"/>" ..../&gt;
    ....



    Also, is there a way to do something like this
    <![CDATA[
    <xsl:value-of select="@CATEGORY" />
    ]]>


    Well you can do exactly that, as you observe, but then the
    <xsl:value-o
    is taken as character data not element markup.


    The problem with this is it gives me
    <xsl:value-of select="@CATEGORY" />


    where I want
    Men&apos;s Clothing

    As I say above If you mean that you had an input string of
    Men&apos;s Clothing
    and you need it to come out as
    Men&apos;s Clothing
    and not as
    Men's Clothing
    then basically you can't use XSLT as the XML parser will report these
    two things as the same. So there is no way XSLT can preserve the difference.


    David
    David Carlisle, May 6, 2005
    #2
    1. Advertising

  3. Sammy

    Sammy Guest

    Hmm, whell what if you want everythign to come out as


    &apos; then?


    I mean actually I don't care if it comes in as ' or &apos;

    I just want it to come out as &apos;
    Sammy, May 6, 2005
    #3
  4. In article <>,
    Sammy <> wrote:

    >Hmm, whell what if you want everythign to come out as
    >&apos; then?


    The answer from an XML point of view is that you shouldn't want that!

    If you need to enforce it for compatibility with some non-XML system
    (such as your database), then you need a special purpose tool to
    convert it from "generic XML" to "the subset of XML my application
    needs".

    -- Richard
    Richard Tobin, May 6, 2005
    #4
  5. Sammy

    Sammy Guest

    Are there any templates out there for converting all your data from

    ', <, >, &

    to

    &apos; &lt; &gt; &amp;

    ?
    Sammy, May 6, 2005
    #5
  6. Re: How can I get attribute values to not get converted from &apos;to ' or &amp; to & or &lt; to > ?

    Sammy wrote:

    > Hmm, whell what if you want everythign to come out as
    >
    >
    > &apos; then?


    XSLT gives you a result tree, you would then need to write your own
    serializer that follows the conventions you define.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 6, 2005
    #6
  7. Sammy

    Sammy Guest

    Oh, and I really like your FAQ by the way David...


    I've been reading it a lot over the last 2 weeks....
    Sammy, May 6, 2005
    #7
  8. Sammy

    Sammy Guest

    Yeah, but how do HTML peeps do this then?

    I mean they use XSL to create HTML all the time and it comes out as
    &apos; ??

    Am I missing something here?

    Those peeps who create HTML have to have it come out in &apos, and
    &nsps; and &amp;

    Or there HTML won't work...

    What do those peeps do?
    Sammy, May 6, 2005
    #8
  9. Sammy

    Sammy Guest

    It would seem like one stategy is to just have it output
    HTML?

    Does this work?

    If I change the <xsl:eek:utput element to say HTML

    will it output &apos;???
    Sammy, May 6, 2005
    #9
  10. Sammy

    Sammy Guest

    Thanks for all the quick responces by the way.

    I appreciate your help....
    Sammy, May 6, 2005
    #10
  11. "Sammy" <> writes:

    > Yeah, but how do HTML peeps do this then?
    >
    > I mean they use XSL to create HTML all the time and it comes out as
    > &apos; ??
    >
    > Am I missing something here?


    yes.

    > Those peeps who create HTML have to have it come out in &apos, and
    > &nsps; and &amp;
    >
    > Or there HTML won't work...


    It will work. look at
    <p>your&apos;s and your's</p>
    in an html browser of your choice. &apos; means the same thing as '
    The _only_ time you need to use &apos; is to get a ' in an ' delimited
    attribute value
    foo='your&apos;s'
    XSLT will do that automatically when it's needed.

    >
    > What do those peeps do?


    They let XSLT take care of the details.

    David
    David Carlisle, May 6, 2005
    #11
  12. "Sammy" <> writes:

    > Oh, and I really like your FAQ by the way David...
    >
    >
    > I've been reading it a lot over the last 2 weeks....


    different david
    David Carlisle, May 6, 2005
    #12
  13. In article <>,
    Sammy <> wrote:

    >It would seem like one stategy is to just have it output
    >HTML?
    >
    >Does this work?
    >
    >If I change the <xsl:eek:utput element to say HTML
    >
    >will it output &apos;???


    No, because ' is perfectly good in HTML as well as in XML.

    -- Richard
    Richard Tobin, May 6, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Mercola

    is &apos; (apostrophe) deprecated?

    James Mercola, Oct 4, 2004, in forum: HTML
    Replies:
    4
    Views:
    9,458
    sjk30
    Feb 7, 2011
  2. Kent Johnson

    No apos in htmlentitydefs

    Kent Johnson, Nov 28, 2005, in forum: Python
    Replies:
    1
    Views:
    359
    Fredrik Lundh
    Nov 28, 2005
  3. madan
    Replies:
    7
    Views:
    783
  4. Mike Schilling

    Re: SAX converts apostrophe to &apos;

    Mike Schilling, Sep 3, 2009, in forum: Java
    Replies:
    0
    Views:
    436
    Mike Schilling
    Sep 3, 2009
  5. Daniel Pitts

    Re: SAX converts apostrophe to &apos;

    Daniel Pitts, Sep 3, 2009, in forum: Java
    Replies:
    1
    Views:
    387
    Mike Schilling
    Sep 4, 2009
Loading...

Share This Page