|
Hello,
I am storing the database data in the XML file. Using the XSL file I want to retrieve the selected data from the XML file. I have tried using <xsl:choose>, <xsl:when>, <xsl:if>, but I am not getting the desired results. If I use <xsl:if>, the page is not rendered & the drop down control is displayed as a plain text. If I use <xsl:when>, the condition is not being tested.
Can anypne tell me exactly what could be the problem?
Thanks in advance
Hitesh
|
|
|
|
|
It would help a lot if you could provide an example of your XML and what you are trying to retrieve.
<tip>Write < when you want to show the < character.</tip>
|
|
|
|
|
Hi,
Thanks for the reply.
Following is the part of the code
<xsl:choose>
<xsl:when test=".[CardType=3]">
Credit Card Option1
<xsl:element name="select">
<xsl:attribute name="name">D2
<xsl:element name="option">
<xsl:attribute name="value"><xsl:value-of select="CardOptionParent1">
<xsl:when test=".[CardType='4']">
Credit Card Option1
<xsl:element name="select">
<xsl:attribute name="name">D2
<xsl:element name="option">
<xsl:attribute name="value"><xsl:value-of select="CardOptionParent2">
<xsl:otherwise>
Credit Card Option1
<xsl:element name="select">
<xsl:attribute name="name">D2
<xsl:for-each select="data/record/field1">
<xsl:element name="option">
<xsl:attribute name="value"><xsl:value-of select="CardLicenceOption">
<xsl:value-of select="CardLicenceOptionDescription">
CardType is the tag in my XML file. Based on the value of CardType selected, I want to display the values from the XML file.
Let me know if you can understand this. I wanted to attach my .xsl file but there is no way I can attach the file.
Thanks
Hitesh
|
|
|
|
|
I'm guessing your XML looks something like this:
<cards>
<card><CardType>1</CardType>
<data>
<record>
<field1>
<CardLicenceOption>1</CardLicenceOption>
<CardLicenceOptionDescription>Credit</CardLicenceOptionDescription>
</field1>
<field1>
<CardLicenceOption>2</CardLicenceOption>
<CardLicenceOptionDescription>Charge</CardLicenceOptionDescription>
</field1>
</record>
</data>
</card>
<card><CardType>3</CardType>
<CardOptionParent1>1</CardOptionParent1>
<CardOptionDescription>Cash</CardOptionDescription>
</card>
</cards>
It looks like you're using incorrect paths to access your XML elements. The following XSLT should work:
<xsl:for-each select="//card">
<tr><td width="50%">Credit Card Option1
<xsl:choose>
<xsl:when test="CardType=3">
<xsl:element name="select">
<xsl:attribute name="name">D2</xsl:attribute>
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="CardOptionParent1" />
</xsl:attribute>
<xsl:value-of select="CardOptionDescription" />
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:when test="CardType=4">
<xsl:element name="select">
<xsl:attribute name="name">D2</xsl:attribute>
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="CardOptionParent2" />
</xsl:attribute>
<xsl:value-of select="CardOptionDescription" />
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="select">
<xsl:attribute name="name">D2</xsl:attribute>
<xsl:for-each select="data/record/field1">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="CardLicenceOption"/>
</xsl:attribute>
<xsl:value-of select="CardLicenceOptionDescription" />
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</td></tr>
</xsl:for-each>
|
|
|
|
|
I'm interested in implementing my applications log files as XML. From what little I know of XML, DOM requires that you load the entire document before you can manipulate nodes. Fine for reading, but if I'm logging a lot of items, I don't want to have to open the file, read the entire document, add a node, then close again.
Does anyone have any experience doing XML logging? Is it worth the trouble? Got any examples or advice on non-DOM implementations?
|
|
|
|
|
I would not use XML files for logging. They are text files as you state, open and closing all of the time. Some database would be better. You can then always export the database to an XML file when needed for processing or archiving.
XML and related technologies are great but they are not pixie dust for all of the worlds troubles.
To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli
|
|
|
|
|
You could use one of two approaches if you do not plan to read the log file (only write to it):
- You could use the SAX interface to write entries to the log file
- You could write include XML tags in your log messages, making it possible to process the log later with an XML processor
The second option is, by far, the easiest to implement. I wrote an article for another Web site back in November 2001 that covers most of option #1 - here's the link (new window): Using the Microsoft XML Parser to Create XML Documents
Essam - Author, JScript .NET Programming
...and a bunch of articles around the Web
|
|
|
|
|
here's what I did, but we didn't run this production yet
1) construct one line of valid xml
2) write it to text file , flush buffer
This produced a text file that resembled xml, but missing the top-level node. To display it in a web page, I pre-pended a top-level node ( < TOP > ) , and appended a closing node name
( < / TOP > ), and this worked.
I think one possible advantage is that it is super fast and simple to write a line of text, and then you can take care of the leading and trailing niceties later when you display the file.
Also, this approach ignores the DOM entirely. It is only straight C text files at the time of logging.
|
|
|
|
|
We have done something similar, and for the first time files, if you check their existance before opening them, you can write out the xml header for a new file. You can also write out any trailer information, and if it is static, then when you open the file, you can re-position the file pointer (using fseek) to be that number of characters back from the end, write out your new data, and rewrite the static (ie, fixed/known length) trailer. It's a little clumsy, but it helps to know that you can re-position the file pointer.
Dave
"You can say that again." -- Dept. of Redundancy Dept.
|
|
|
|
|
This has been driving me crazy! I am trying to use XSL to output a nice pretty XHTML page from my XML feed. And of course, all nice pretty XHTML pages include a !DOCTYPE declaration. But whenever I try and simply stick it in my <xsl:template> tag, it gives me this error:
<br />
<br />
Cannot have a DOCTYPE declaration outside of a prolog. Error processing resource 'file:///C:/Inetpub/wwwroot/PageWriter/Template.xsl'. Line 13, Position 13<br />
<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br />
------------^<br />
I've tried a variety of things, such as splitting up the !DOCTYPE into various pieces and putting them in <xsl:text> elements, but that didn't work.
Anybody know the answer? This must be a fairly common question, not all XHTML and HTML outputs can be sloppily tossed out w/o a !DOCTYPE (i.e. like they are in all examples I can find).
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
Domenic,
I am not all that familuar with XHTML but can you post a little more of the transformation you are using and the output it creates. I at least do not see enough to comment on.
To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli
|
|
|
|
|
Well, in total its huge (10KB hand typed! I'm so proud of myself ), but here are the relevant parts:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:XHTML="http://www.w3.org/1999/xhtml"
xmlns:MHC="/PageWriter/TemplateSchema.xsd">
<xsl:template match="/">
<xsl:processing-instruction name="xml-stylesheet">
<xsl:text>version="1.0" encoding="utf-8"</xsl:text>
</xsl:processing-instruction>
<!-- This is commented out because it gives me the evil error, how can I simply output this? -->
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<!-- Irrelevant stuff here -->
</head>
<body>
<!-- Irrelevant stuff here -->
</boy>
</html>
</xsl:template>
</xsl:stylesheet>
Basically I just want to get that commented out part into my output, and because it has the sequence -Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
Have you tried to replace the <! character sequence with the string <! - that should trick the parser enough to work around the problem. Of course, change the closing > symbol at the end of the DOCTYPE to the string >
Essam - Author, JScript .NET Programming
...and a bunch of articles around the Web
|
|
|
|
|
Yep. Didn't work, just outputted the actual text <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> at the top of the XHTML output document when viewed in the browser (IE6).
Any other suggestions?
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
Domenic [CPUA 0x1337] wrote:
Any other suggestions?
Yes - I forgot to mention that you need to enclose the modified DOCTYPE declaration within an xsl:text element, as follows (assuming that the XSL's namespace prefix is xsl):
<xsl:text disable-output-escaping="yes">
<!DOCTYPE html...>
</xsl:text>
That should work - famous last words...
Essam - Author, JScript .NET Programming
...and a bunch of articles around the Web
|
|
|
|
|
As I said on the below thread, this works great, thanks a ton! I do have another question tho, but I would refer you to the below thread, as I don't know how long I can keep this multi-threading going!
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
The problem is your commented out line was being taken as instructions to the transformation processor not as output. Try Essam's suggestion as it is the simplest. And drop a message back. My concern is it may stay as the encoded text in the output. But just because I have never done it that way so what. You may need to force this line to be text output. Look at the processing instruction:
<xsl:text>Most any data here.
There are limits to this also but it is what I have used most often. I also use the output instructions to not have any text output that is not explicit such as this line:
<xsl:output method="text" encoding="utf-8" media-type="model/vrml" indent="no" cdata-section-elements="Script">
I do this so as to not pickup extra linefeeds etc.
To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli
|
|
|
|
|
I think there's quite a bit of XML data your missing from your output, remember to substitute < and > in the right places.
Besides that, replacing it with < and > didn't work for the !DOCTYPE. It just outputted the actual text of <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Any other ideas?
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
Yes I did not check the display message as is (no HTML).
I was just adding the xsl:text tag that essam mentions above. There are also option to not add all of the text (line feeds etc.) to the output.
My Sample lines were approximately:
<xsl:output method="text" encoding="utf-8" media-type="model/vrml" indent="no" cdata-section-elements="Script"/>
<!-- ****** X3D: convert the X3D tag to VRML 3.0 header ****** -->
<xsl:template match="X3D">
<xsl:text>#VRML V2.0 utf8 </xsl:text>
<xsl:text># [X3D] VRML V3.0 utf8 </xsl:text>
<xsl:apply-templates select="Scene"/>
</xsl:template>
This is the begining of my translation file for converting X3D models into VRML97. Lets see if I get it right this time.
There are a number of sites with info here.
One example is the w3schools site
http://www.w3schools.com/xsl/el_text.asp
and zvon
http://www.zvon.org/xxl/XSLTreference/W3C/xslt.html#element-text
both on the partners links on CP.
To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli
|
|
|
|
|
This works great! Thank you so much!
But now I've got another question... Should I do the same thing for my <?xml version="1.0" encoding="utf-8"?> that I want at the very top of the output document, or will this be done for me? From what I've seen using XSL Tester to view my XHTML output, this is done for me, but it gives me an encoding="UTF-16" , which I don't think is a valid encoding.
Can I somehow change the way this is generated automatically if it is, or is this simply a quirk in XSL Tester and I should do the same thing Idid with my !DOCTYPE, by putting it in that special <xsl:tex> element?
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
I assume that you are using one of the MSXML implementations. To set the encoding see the xsl:output transformation element (at the begining of the transformation), You should be able to set utf-8 with it. UTF-16 is a valid encoding. Most of my work right now is with the Apache line of implementations so there may be some variations. I have shown that DOM and SAX transformations do not always give the same results. I do have some test applications with the MSXML v3 and v4 so I can do some experiments if needed.
<xsl:output method="text" encoding="utf-8"/>
To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli
|
|
|
|
|
I am using the MSXSL that comes with IE6 or the .NET Runtime, whichever is newer (I have both installed).
So XSL will generate an <?xml...?> declaration automagically, which can be controlled by the <xsl:output> element?
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
I can say that the xml declaration is not allways output. With the output line shown earlier it should never be output. Just add it with the xsl:text line if needed. The output line will set if encoding is utf-8.
To be conscious that you are ignorant of the facts is a great step towards Knowledge. Benjamin Disraeli
|
|
|
|
|
So, my XSL should like like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0">
<xsl:output encoding="utf-8" />
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
</xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<!-- Stuff here -->
</html>
</xsl:template>
</xsl:stylesheet>
And this will output:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<!-- The stuff that was where the comment was goes here -->
</html>
If you're not absolutely sure, could you write me quick program or give me the JScript code (not .NET) to just output the text? Whenever I do a view source in IE, it just gives me the original XML.
I would do that myself, but I'm stuck on this Windows 98 until I get a new hard drive
Thank you!
-Domenic Denicola- [CPUA 0x1337]
MadHamster Creations
"I was born human. But this was an accident of fate - a condition merely of time and place. I believe it's something we have the power to change..."
|
|
|
|
|
There are two ways to output the DOCTYPE.
One is using the OUTPUT method in XSL, like so:
>xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="DTD/xhtml1-transitional.dtd" indent="yes" /<
The other, is easier but probably not such a good idea. It uses the XSL:TEXT and CDATA section methods:
>xsl:text disable-output-escaping="yes"<>![CDATA[>!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"<]]<>/xsl:text<
This one also baffled me for awhile until I found the OUTPUt method, happy hunting.
regards,
Paul Watson
Bluegrass
Cape Town, South Africa
The greatest thing you'll ever learn is just to love, and to be loved in return - Moulin Rouge
|
|
|
|