p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   I can't figure this out. (http://p2p.wrox.com/showthread.php?t=66790)

aaronmuslim March 20th, 2008 10:44 AM

I can't figure this out.
 
I can't figure out how to create new xml elements to hold values of
another xml documents elements with XSLT. When I choose the output method of xml none of the < or > entities get converted to < >. So should I change the output method to "text" and just open by declaring the xml document myself? I'm sure there is some simple solution here that I just can't find a referencing example of.

Heres what my newb thoughts are composed of right now:
Code:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="xml"/>

  <xsl:template match="/Root">
    &lt;NEWXMLELEMENT &gt;
      <xsl:value-of select="."/>
    &lt;/NEWXMLELEMENT&gt;
  </xsl:template>

</xsl:stylesheet>

I really want to know how to do this with XSLT properly and not have to revert to my old java methods...

Martin Honnen March 20th, 2008 10:50 AM

I don't understand what you want to achieve.
You might want to post a sample of your input and then a sample of the output you want to create with your stylesheet.
Generally if you want to create elements with your stylesheet then you have two options, literal result elements e.g.
Code:

<xsl:template match="foo">
  <bar>
    <xsl:apply-templates/>
  </bar>
</xsl:template>

and constructing elements with xsl:element e.g.
Code:

<xsl:template match="foo">
  <xsl:element name="bar">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>


mhkay March 20th, 2008 10:54 AM

The way to create a new element is like this:

  <xsl:template match="/Root">
    <NEWXMLELEMENT>
      <xsl:value-of select="."/>
    </NEWXMLELEMENT>
  </xsl:template>

You need to understand a very basic concept in XSLT: a stylesheet doesn't generate a file containing lexical markup, it generates a tree containing nodes, which is then passed to a serializer to be serialized. The NEWXMLELEMENT element node in this stylesheet gets copied to create a NEWXMLELEMENT node in your result tree, and the serializer takes this element node and produces the angle bracket markup. If you try to generate markup (start and end tags) by hand, it becomes the content of a text node, which the serializer processes by escaping any special characters it finds.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

aaronmuslim March 20th, 2008 11:11 AM

Okay, I don't doubt I sound incredibly confused to every experienced xslt programmer trust me it sucks to be down here at this level of xslt.

What I'm saying is that there is some kind of conflict between your first example and my xslt processor. It genererates a error when I try to compile that using <xsl:output method="xml"/>

I'm here at work right now in a Federal building and so the computer security is outrageous and I don't have the Xalan installed here to tell you exactly the error, but I'm working on it ....


aaronmuslim March 20th, 2008 11:13 AM

mhkay & martin, thank you for expalining this to me. If you only knew how much I value ur input guys.... thanks.


All times are GMT -4. The time now is 04:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.