Jump to content

Abstract XML!


dooberry

Recommended Posts

OK, I'm pushing back my boundaries now.Up to this point I have worked with an existing xml file and a bunch of xsl files which all transform it in different ways.I was wondering (and I'm sure that someone must have done it!) - I can create an object in memory that is a blank XML document a new instance of the XMLDOM object.If I want to change it, I can change it - no problem because I just use the object methods.Can I still use transformnode to change the presentation of it?Do I need to add processing instructions to it (including the xml version instruction)?If anyone can offer advice I'd appreciate it.Hey, while I'm taking this approach to questioning - I know that we have the wonderful W3 schools tutorials but - Has anyone thought of putting an FAQ section on the forums because I keep seeing the same questions being asked by different members and it seems a shame to have to trawl through masses of topics to find the answer to what may be a simple question that isn't covered by the tutorials.Doo (Halle) Berry - yeah you would! :)

Link to comment
Share on other sites

If by "change the presentation of it" you mean something like a dynamical change of presentation, depending on the currently changed content... perhaps an XSLT with conditional statements?Well, if you mean that you won't have existing XML file, but would "emulate" one with JavaScript/VBScript well... hm... considering the fact that the JavaScript that runs XSLT inside XHTML doesn't requre the XML to have processing instructions, I would say that you don't need to create processing instructions, but could also do that.As for this FAQ thing... If I ever create a website of mine, FAQ about XSLT is THE first thing I'm going to include. Until then, the most frequently asked things, such as "how to create attributes based on XML data?" were already suggested to W3Schools' staff to add to the tutorial. When are they going to be added is another thing.

Link to comment
Share on other sites

Yeah, I think thats what I mean.I was going to use a global script variable to hold the XML document in memory, then I was just going to use the transformNode() method combined with an XSL file name to transform the data.Here's some rough code to give the idea:

<script language="javascript" >var rootdocvar basenoderootdoc = new ActiveXObject("Microsoft.XMLDOM")rootdoc.async = falsebasenode = rootdoc.createElement("mynewelement")rootdoc.documentElement.appendChild(basenode)function displaypage(xslparam){  var xsldoc  xsldoc = new ActiveXObject("Microsoft.XMLDOM")  xsldoc.async = "false"  xsldoc.load(xslparam)  document.write rootdoc.transformNode(xsldoc)}</script>

I assumed that this would hold the data of the XML document until the document is saved and then it could be re-initialised once the data is stored.Dooberry

Link to comment
Share on other sites

Yay!! :)This worked. Not only did it work, but it's also allowed me to halve the amount of code that I need to write because I don't have to save the document until all the data has been submitted.Reworking the code was fun to and for a client side application this is probably a better way of doing the job.Woohoo!

Link to comment
Share on other sites

Gosh Darn it!!!I've hit a barrier because I'm traversing documents.I'm creating nodes as I go to minimise the amount of memory used by the client when holding the document (which is the real part of the problem). What I need to do is look at another document which holds default values for a set of nodes, but substitute the values with those contained by the document in memory if they exists - here's an illustration of what I would do if both documents were saved:

<xsl:for-each select="document('externalitems.xml')//externalitem" ><xsl:variable name="id" select="id" /><xsl:if test="exists(document('newdata.xml')//information/items[id=$id])" >  <xsl:value-of select="document('newdata.xml')//information/items[id=$id]/data" ></xsl:if><xsl:value-of select="itemdata" /></xsl:for-each>

What I would like to do is use <xsl:if> to replace the <itemdata> values with the values held by my document in memory (it does not exist on the client disk at this point!). Is this even possible without saving the document that I need to check?

HOLY ROCK Batman!! What is it Robin? It's a rock with a hole in it!
Link to comment
Share on other sites

You seem to put your applications to a very weird level, that I seem to be unable to catch on with. I wonder why don't you try more traditional methods and then go wild with them. Anyway...If I understand correctly, you have two XMLs, each holding different valus of the same element, right? Depending on a certain criteria, you want to load one of them, correct? Soooo.... how about using when/otherwise instead?Something like this:

<xsl:choose><xsl:when test="exists(document('newdata.xml')//information/items[id=$id])"><xsl:value-of select="document('newdata.xml')//information/items[id=$id]/data" ></xsl:when><xsl:otherwise><xsl:value-of select="itemdata" /></xsl:otherwise>

?

Link to comment
Share on other sites

Yeah, that looks like it might do the trick.As far as your other comment goes, you're right - I'm weird and my communication skills aren't brilliant! :)Thanks for your help.Doobs.

Link to comment
Share on other sites

Hmm, your code has given me and idea - If I use a template that matches the node in "memory" and then use a template which matches the external document node, I can use the <xsl:when> element to apply the appropriate template as required.When I've sorted it I'll post the code.

Link to comment
Share on other sites

Ok, I've got a more basic problem.The exists XPath function is not working. I'm using the following namespace declaration:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">

But I get one of 2 errors, If is use "exists(nodename)" I get the error message 'exists is not a valid XPath function'.If I use "fn:exists(nodename)" I get the error message 'http://www.w3.org/2005/02/xpath-functions does not contain any functions'.Where am I going wrong?Dooberry

Link to comment
Share on other sites

As far as I can see in this namespace, XPath 2.0 functions are still candidate reccomendation. That's probably why it's not working as it should be.By the way, the namespace you provded doesn't work for me and neither does W3Schools one. I would advice that you avoid using such not very well implemented functions.A much easier way to chech if something exists is to simply type it's name without any additional info:

<xsl:if test="nodename">

That is of couse if you're using it with conditionals. Any other use would really requre that function.

Link to comment
Share on other sites

I think I was trying too hard!!I used that principle with <xsl:choose> instead of <xsl:if> and I can now pick between a default value and a user entry - that's all I was trying to do.Thanks for your help.Dooberry decides to buy a book called 'XSL for Dummies'. :)

Link to comment
Share on other sites

  • 2 weeks later...

I've used this to another end as well now.Instead of "jumping" between nodes based on a user entry, I can also use this to identify where key values have not been entered:

<xsl:choose><xsl:when test="item_qty">  <xsl:input id="qty">  <xsl:attribute name="value">  <xsl:value-of select="item_qty" />  <xsl:attribute/>  </xsl:input></xsl:when><xsl:otherwise>You have not entered the quantity.</xsl:otherwise></xsl:choose>

This is a useful way to eliminate schema (because I don't have the facilities to use them!).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...