how can I do this ?

J

Julio

Hi, I have problem with namespaces and attributes.
I want to get this:

<grammar xmlns="http://www.w3.org/2001/06/grammar"
xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
I have the following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" indent="yes" omit-xml-declaration="yes" />

<xsl:template match="/">
<xsl:element name="grammar">
<xsl:attribute name="xmlns">http://www.w3.org/2001/06/grammar</xsl:attribute>
<xsl:attribute name="sapi"
namespace="xmlns">http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions</xsl:attribute>
</xsl:element>
</xsl:template>


but I can't get what I want, it don't work. Can anyone help me out?
Thanks in advance.
Julio
 
P

Pieter Vandepitte

Strange, it _does_ work for me... but i appended
</xsl:stylesheet>
to your stylesheet :)

w.kind regards
Pitter
 
P

Patrick TJ McPhee

% I want to get this:
%
% <grammar xmlns="http://www.w3.org/2001/06/grammar"
% xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
% >

% I have the following XSLT:
%
% <?xml version="1.0" encoding="UTF-8"?>
% <xsl:stylesheet version="1.0"
% xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
% <xsl:eek:utput method="xml" indent="yes" omit-xml-declaration="yes" />
%
% <xsl:template match="/">
% <xsl:element name="grammar">
% <xsl:attribute
% name="xmlns">http://www.w3.org/2001/06/grammar</xsl:attribute>

Don't treat the name space declaration as an ordinary attribute. Just
declare the name space, and it should be emitted as needed. For instance,
if you put

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2001/06/grammar"
xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions">

at the top of your style sheet, you can put

<grammar>
<sapi:excuse-my-ignorance/>
</grammar>

In your template, and it will come out with the appropriate name space
declarations.
 
J

Julio Kriger

Hi, I'm using XmlSpy 2004 and I don't what I want.
Which xslt engine are you using?
Cheers!
 
J

Julio Kriger

Hi, I supose if I try what you say it will work. However, I only need
the attribute
'xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
' to say to the grammar compiler to use some extension to build the
binary file of the grammar.
Let me show what I need to get, you will see that I don't need a tag
<sapi:whatever ... />.

<grammar xmlns="http://www.w3.org/2001/06/grammar"
xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
xml:lang="en-US" tag-format="semantics-ms/1.0" version="1.0"
mode="voice">
<rule id="root" scope="public">
<tag>$.Result={}</tag>
<one-of>
<item>
<item>Boston Fortune</item>
<tag>$.Result.FirstName=Boston</tag>
<tag>$.Result.LastName=Fortune</tag>
<tag>$.Result.Extension=123</tag>
<tag>$.Result.Department=Development</tag>
</item>
<item>
<item>Londo Deamon</item>
<tag>$.Result.FirstName=Londo</tag>
<tag>$.Result.LastName=Deamon</tag>
<tag>$.Result.Extension=456</tag>
<tag>$.Result.Department=Development</tag>
</item>
<item>
<item>Oxford Cowboy</item>
<tag>$.Result.FirstName=Oxford</tag>
<tag>$.Result.LastName=Cowboy</tag>
<tag>$.Result.Extension=789</tag>
<tag>$.Result.Department=Research</tag>
</item>
</one-of>
</rule>
</grammar>


Now I have it working with the following xslt, but I beleive it's a
workaround and not a good solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
xmlns="http://www.w3.org/2001/06/grammar">
<xsl:eek:utput method="xml" indent="yes" omit-xml-declaration="yes" />

<xsl:template match="/">
<grammar
xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
xml:lang="en-US" tag-format="semantics-ms/1.0" version="1.0"
mode="voice" xmlns="http://www.w3.org/2001/06/grammar">
<!--
<xsl:element name="grammar">
<xsl:attribute
name="xmlns">http://www.w3.org/2001/06/grammar</xsl:attribute>
<xsl:attribute
name="xmlns:sapi">http://schemas.microsoft.com/Speech/2002/06/SRGSExtens
ions</xsl:attribute>

<xsl:attribute name="xml:lang">en-US</xsl:attribute>
<xsl:attribute name="tag-format">semantics-ms/1.0</xsl:attribute>
<xsl:attribute name="version">1.0</xsl:attribute>
<xsl:attribute name="mode">voice</xsl:attribute>
-->
<xsl:element name="rule">
<xsl:attribute name="id">root</xsl:attribute>
<xsl:attribute name="scope">public</xsl:attribute>

<xsl:call-template name="tag">
<xsl:with-param name="name">$.Result</xsl:with-param>
<xsl:with-param name="value">{}</xsl:with-param>
</xsl:call-template>

<xsl:element name="one-of">
<xsl:for-each select="//User">
<xsl:call-template name="user" />
</xsl:for-each>
</xsl:element>
</xsl:element>
<!--
</xsl:element>
-->
</grammar>
</xsl:template>

<xsl:template name="tag">
<xsl:param name="name" />
<xsl:param name="value" />

<xsl:element name="tag">
<xsl:value-of select="$name" />
<xsl:text>=</xsl:text>
<xsl:value-of select="$value" />
</xsl:element>
</xsl:template>

<xsl:template name="user">
<xsl:element name="item">
<xsl:element name="item">
<xsl:value-of select="./FirstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="./LastName" />
</xsl:element>

<xsl:call-template name="tag">
<xsl:with-param name="name">$.Result.FirstName</xsl:with-param>
<xsl:with-param name="value"><xsl:value-of select="./FirstName"
/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="tag">
<xsl:with-param name="name">$.Result.LastName</xsl:with-param>
<xsl:with-param name="value"><xsl:value-of select="./LastName"
/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="tag">
<xsl:with-param name="name">$.Result.Extension</xsl:with-param>
<xsl:with-param name="value"><xsl:value-of select="./Extension"
/></xsl:with-param>
</xsl:call-template>

<xsl:call-template name="tag">
<xsl:with-param name="name">$.Result.Department</xsl:with-param>
<xsl:with-param name="value"><xsl:value-of select="./Department"
/></xsl:with-param>
</xsl:call-template>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

As you can see, there are some part that are commented becuase they
don't work.

Cheers!
 
P

Patrick TJ McPhee

% Hi, I supose if I try what you say it will work. However, I only need
% the attribute
% 'xmlns:sapi="http://schemas.microsoft.com/Speech/2002/06/SRGSExtensions"
% ' to say to the grammar compiler to use some extension to build the
% binary file of the grammar.

This sounds like a misuse of the name space declaration.

% Now I have it working with the following xslt, but I beleive it's a
% workaround and not a good solution:

I'm not sure what your problem is. Do you mean you would rather have
<xsl:element name='grammar'>
...

rather than
<grammar ...>

?

Blech. In my opinion, you should never use xsl:element or xsl:attribute
unless you need to generate the names dynamically. It makes the
stylesheet about a hundred times harder to read when you do.

If it's not that, then what's the problem?
 
J

Julio Kriger

Hi,
I would rather user <xsl:element name='grammar'> than <grammar ...>
I beleive it's more properly done this way (<xsl:element
name='grammar'>) than the other way (<grammar ...>).
Maybe it's a really misuse of namespace declaration. I don't know.
I have created a C# program that create this kind of grammar files, with
the use of createelement and createattibute of the xmldocument object. I
thouht it will be same with XSLT.
Cheers!
 
P

Patrick TJ McPhee

% I would rather user <xsl:element name='grammar'> than <grammar ...>
% I beleive it's more properly done this way (<xsl:element
% name='grammar'>) than the other way (<grammar ...>).

Just get that idea out of your head. I mean, do it if you want, but
don't imagine it's more proper. To me, it's harder to read, harder
to maintain and just generally worse in every way imaginable.

Having said that, if you control the semantics behind this use of
xmlns:whatever, then change the way you do things. Create an attribute
in your own name space which tells your code to do whatever it is
you need to do, rather than depending on a particular name space
declaration being present.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top