Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How can I get attribute values to not get converted from ' to ' or & to & or < to > ?

Reply
Thread Tools

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

 
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

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

<xsl:template name="frank_data">

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

 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      05-06-2005


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

 
Reply With Quote
 
 
 
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
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;

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-06-2005
In article <(E-Mail Removed). com>,
Sammy <(E-Mail Removed)> 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
 
Reply With Quote
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
Are there any templates out there for converting all your data from

', <, >, &

to

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

?

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      05-06-2005


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/
 
Reply With Quote
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
Oh, and I really like your FAQ by the way David...


I've been reading it a lot over the last 2 weeks....

 
Reply With Quote
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
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?

 
Reply With Quote
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
It would seem like one stategy is to just have it output
HTML?

Does this work?

If I change the <xslutput element to say HTML

will it output &apos;???

 
Reply With Quote
 
Sammy
Guest
Posts: n/a
 
      05-06-2005
Thanks for all the quick responces by the way.

I appreciate your help....

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
str to stdvec of converted values J_R_L VHDL 0 06-13-2007 02:56 AM
JNDI: Delete only one attribute when there are several different values for the same attribute bsporb@gmail.com Java 3 05-02-2007 05:41 AM
How can I make a class that can be converted into an int? Matthew Wilson Python 4 10-02-2006 03:17 PM
Control properties with entity references declared in ASPX get converted to character values jesl@lundbeck.com ASP .Net 2 02-15-2006 08:31 AM
'The expression can not be converted to type' error A. Kong VHDL 4 10-10-2004 07:37 AM



Advertisments