By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:output 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>

Jul 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a

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

Jul 20 '05 #2

P: n/a
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;

Jul 20 '05 #3

P: n/a
Are there any templates out there for converting all your data from

', <, >, &

to

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

?

Jul 20 '05 #4

P: n/a
In article <11*********************@z14g2000cwz.googlegroups. com>,
Sammy <fr********@gmail.com> 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
Jul 20 '05 #5

P: n/a


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/
Jul 20 '05 #6

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

Jul 20 '05 #7

P: n/a
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?

Jul 20 '05 #8

P: n/a
It would seem like one stategy is to just have it output
HTML?

Does this work?

If I change the <xsl:output element to say HTML

will it output &apos;???

Jul 20 '05 #9

P: n/a
Thanks for all the quick responces by the way.

I appreciate your help....

Jul 20 '05 #10

P: n/a
"Sammy" <fr********@gmail.com> 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
Jul 20 '05 #11

P: n/a
"Sammy" <fr********@gmail.com> 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
Jul 20 '05 #12

P: n/a
In article <11*********************@g14g2000cwa.googlegroups. com>,
Sammy <fr********@gmail.com> wrote:
It would seem like one stategy is to just have it output
HTML?

Does this work?

If I change the <xsl:output element to say HTML

will it output &apos;???


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

-- Richard

Jul 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.