Jump to content

Can I alter the xml tree that I am working on and continue working on it with my xsl?


groads2

Recommended Posts

I have a really large xml document that I need to display in an HTML form. I am using xsl to transform into an html. Is it possible to alter the xml that is being transformed with the xsl and continue transforming it after it has been altered? I am really thinking of removing or changing some of the namespacing. Thanks.

Link to comment
Share on other sites

You can write several stylesheets where one transforms the result of another. Or with one stylesheets (and XSLT 2.0), you can apply more than one transformation step by using variables and modes e.g.

<xsl:variable name="temp">  <xsl:apply-templates mode="step1"/></xsl:variable> <!-- now write templates for the mode step1 and the XML elements you want to transform --> <xsl:template match="temp2">  <xsl:apply-templates select="$temp1/node()"  mode="step2"/></xsl:template> <!-- now here write templates for mode step2 processing the elements created in variable temp and step1 --><xsl:template match="/">  <!-- create final output here -->  <xsl:apply-templates select="$temp2/node()"/></xsl:template> 

In theory you might no need mode if all the different formats use different elements or elements in different namespaces but for clarity I would use them to cleanly separate the processing steps.

Link to comment
Share on other sites

Thanks Martin. I am new to xsl transformation and I am just not understanding parts of the xsl transformation. When I read that something returns the node, I expect the whole node including tags such as <car>Ford</car> not just the value of "Ford". So my delima continues. I have a the following xml, xsl and result below. I really want my far to have the xml with no name spaces. So How can I get the nodes such as <??>???</??> into my variable? Thanks. Also am I missing some fundamental aspect of the transformation process or is it just a lack of experience? Thanks again. my test xml:

<?xml version="1.0" encoding="utf-8"?><n1:treasure xmlns:n1="myNamespace"><piles><moreStuff>  <theStuff>gary</theStuff>  <theStuff>troy</theStuff></moreStuff><n1:otherStuff>  <junk>aaa</junk>  <junk>bbb</junk></n1:otherStuff></piles><misc>  <item1>item one</item1>  <item2>item two</item2></misc></n1:treasure>

my current (after many tests) XSLT:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:n1="myNamespace"><xsl:output method="html"/>  <xsl:variable name="jnker">    <xsl:for-each select="*">	  <xsl:element name="{local-name()}">	    <xsl:copy-of select="."/>	  </xsl:element>    </xsl:for-each></xsl:variable> <xsl:template match="n1:treasure">  <html>  <head>   <script language="javascript">	  $(document).ready(function(){	    var vjunder = "<xsl:value-of select="$jnker"/>";	  });	 </script>  </head>  <body>    <div id="here">Here I am</div>  </body></html></xsl:template>   </xsl:stylesheet>

The results that I constantly get, with the variable not having xml but a string of values.

<html xmlns:n1="myNamespace">  <head>    <META http-equiv="Content-Type" content="text/html; charset=utf-16"><script language="javascript">	  $(document).ready(function(){	    var vjunder = "  gary  troy  aaa  bbb  item one  item two";});	 </script></head>  <body>    <div id="here">Here I am</div>  </body></html>

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...