tags:

views:

71

answers:

2

I'm trying to transform one xml file to output another xml file, and I need the attribute "account" to be output identically to how it appears below. I have a bunch of these values in the file, most are not working.

For values of account like 0x0406 it is output as 0.0.06. But for values like 0x002d it leaves them alone and they come through the way I want.

Any ideas?

Initial XML:

<?xml version="1.0" encoding="UTF-8"?>
...
<foo account="0x0406" other-stuff="blah" something-name="blah again"/>
...

This is my xslt template:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:user="http://some-internal-thing/user"&gt;
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
...
<xsl:attribute name="account"><xsl:value-of select="@account"/></xsl:attribute>
...
A: 

Can you try to change the encoding ? For example "ASCII" instead of "UTF-8".

Pierre
People still use ASCII? In this day and age?
Matti Virkkunen
Or just remove the encoding from the source XML's declaration altogether and let the XSLT engine detect. Maybe your source file is NOT UTF-8.
Jacob
I couldn't try ASCII, I have unicode characters in my xml file and I get errors when trying ASCII.
wonderfulthunk
+2  A: 

The described problem cannot be reproduced.

I have run the following transformation with 10 different xslt processors (Msxml3, Msxml4, Msxml6, .NET XslTransform, .NET XslCompiledTransform, AltovaXml(for XSLT 1.01.0), Saxon6.5.4, Saxon 9.1.07, Saxon 9.1.07.NET and XML-SPY-XSLT2.0) and they all produce the same correct result.

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:user="http://some-internal-thing/user"&gt;
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

 <xsl:template match="/*">
   <xsl:copy>
    <xsl:attribute name="account"><xsl:value-of select="@account"/></xsl:attribute>
   </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when this transformation is performed on the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<foo account="0x0406" other-stuff="blah" something-name="blah again"/>

the expected, correct result is produced:

<foo account="0x0406" />

In case your XSLT processor is not one of these and you really get a wrong result, this is a bug and should be reported to the vendors.

Dimitre Novatchev
Wow, 10 different XSLT processors - I wish I had that kind of time on my hands. Thanks for the confirmation, it made me look at everything else besides XSLT, and It turns out I had sed touching the XML file earlier in my process and replacing the literal string 0.0.0, which of course wasn't quoted, and so it was replacing that regex. I hate inheriting code.
wonderfulthunk
@wonderfulthunk: I am glad that this helped. Believe me or not, but this took me just 2-3 minutes. I am using an XSLT IDE that allows to perform any transformation with any XSLT processor (already installed) in just one or two clicks.
Dimitre Novatchev