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

S

Sammy

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

David Carlisle

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
 
S

Sammy

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

Richard Tobin

Sammy said:
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
 
S

Sammy

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

', <, >, &

to

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

?
 
M

Martin Honnen

Sammy said:
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.
 
S

Sammy

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


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

Sammy

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

Sammy

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;???
 
D

David Carlisle

Sammy said:
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
 
R

Richard Tobin

Sammy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top