Jump to content

Copy-of In To A Variable


turtle
 Share

Recommended Posts

when i do<xsl:copy-of select="." />i get a bunch of nodes and their inner text, but when i do<xsl:variable name="asdf"><xsl:copy-of select="." /></xsl:variable><xsl:value-of select=$asdf />i only get the inner text; why is that? how do i get the whole partial xml in to a variable?

Link to comment
Share on other sites

xsl:value-of translates whatever you give to it to a string.Try to copy-of the variable instead.

Link to comment
Share on other sites

i see - tho' the problem still exists as i kinda want to string it (but the whole xml, not just the inner text), since i want to call normalize-space on it

Link to comment
Share on other sites

i see - tho' the problem still exists as i kinda want to string it (but the whole xml, not just the inner text), since i want to call normalize-space on it
Errr... I'm not sure how/if you can do that in XSLT. By the time the XML reaches XSLT, it's parsed, and you instead want it raw (unparsed).Consider doing post processing of the XSLT result - do everything else, but those nodes you want to output and normalize-space on, do so with whatever your environment is (e.g. if in PHP - do it with DOM).
Link to comment
Share on other sites

trouble is, the environment varies - the xsl is for transforming a data xml from system A in to another format so system B can read it, but the systems A and B can be F and G the next daycould it be possible to for-each the partial xml and re-construct it by manually adding the inequality signs and calling name() etc? i'm not quite sure how to close the tags tho', since it's quite hierarchical...

Link to comment
Share on other sites

trouble is, the environment varies - the xsl is for transforming a data xml from system A in to another format so system B can read it, but the systems A and B can be F and G the next daycould it be possible to for-each the partial xml and re-construct it by manually adding the inequality signs and calling name() etc? i'm not quite sure how to close the tags tho', since it's quite hierarchical...
Yeah. That's one way I guess. But not with for-each. You'll need the power of templates to do arbitrary hierarchies. Something like this can do it:
<xsl:template match="*"><<xsl:value-of select="name()" />><xsl:apply-templates/></<xsl:value-of select="name()" />></xsl:template>

Then, instead of creating a variable with a copy-ed value, apply the templates at that point:

<xsl:template match="/"><!--Do everything else...--><xsl:apply-templates/><!--Do everything else...--></xsl:template>

Link to comment
Share on other sites

*edit* did get it to work with this (doing the initial element where i call this template)

<xsl:template name="initial">		<xsl:for-each select="./*">			<xsl:element name="{name()}">				<xsl:choose>					<xsl:when test="./*">						<xsl:call-template name="initial" />					</xsl:when>					<xsl:otherwise>						<xsl:value-of select="."/> <!-- don't have any attributes -->					</xsl:otherwise>				</xsl:choose>			</xsl:element>		</xsl:for-each>  </xsl:template>

Edited by turtle
Link to comment
Share on other sites

the copy-of performs a deep copy and creates a result tree fragment, not a stringtry this approach (I use MSXSL , so adjust as needed)

		<table>			<column>				<val>a</val>				<val>b</val>			</column>			<column>				<val>c</val>				<val>d</val>			</column>			<column>				<val>e</val>				<val>f</val>			</column>		</table>

	<xsl:stylesheet			xmlns:xsl="http://www.w3.org/1999/XSL/Transform"			xmlns:msxsl="urn:schemas-microsoft-com:xslt"			version="1.0">			<xsl:output method="html" />			<xsl:template match="/">				<xsl:variable name="col1">					<xsl:copy-of select="//column[1]" />				</xsl:variable>				<xsl:for-each select="msxsl:node-set($col1)//val">					<xsl:value-of select="name()"/>:					<xsl:value-of select="."/><br/>				</xsl:for-each>			</xsl:template>		</xsl:stylesheet>

results:val:aval:b

Edited by aalbetski
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
 Share

×
×
  • Create New...