0 users online. Create an account or sign in to join them.Users

Author:
iwyg
Version:
1.2
Release Date:
29 Feb 2012
Category:
Output

Description

Converts a nodeset to string, especially useful for json conversions.

===================================================================================

Copyright 2011, Thomas Appel, http://thomas-appel.com, mail(at)thomas-appel.com dual licensed under MIT and GPL license http://dev.thomas-appel.com/licenses/mit.txt http://dev.thomas-appel.com/licenses/gpl.txt

Github repository: https://github.com/iwyg/xml-nodeset-to-string

===================================================================================

Example usage:

(convert a exsl nodeset to string: )


<xsl:variable name="somelink">
    <a href="{url}" class="some-class"><xsl:value-of select="name"/></a>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($somelink)/* | exsl:node-set($some-link)/text()" mode="nodetostring"/>

(convert xml noset to string: )


<xsl:apply-templates select="node | node[text()" mode="nodetostring"/>

XSLT

View Raw
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
	Converts a nodeset to string, especially useful for json conversions.

	===================================================================================

	Copyright 2011, Thomas Appel, http://thomas-appel.com, mail(at)thomas-appel.com
	dual licensed under MIT and GPL license
	http://dev.thomas-appel.com/licenses/mit.txt
	http://dev.thomas-appel.com/licenses/gpl.txt

	===================================================================================

	Example usage:

	(convert a exsl nodeset to string: )
	___

	<xsl:variable name="somelink">
		<a href="{url}" class="some-class"><xsl:value-of select="name"/></a>
	</xsl:variable>
	<xsl:apply-templates select="exsl:node-set($somelink)/* | exsl:node-set($some-link)/text()"/>
	___

	(convert xml noset to string: )
	___

	<xsl:apply-templates select="node | node[text()]"/>

-->
	<xsl:variable name="q">
		<xsl:text>"</xsl:text>
	</xsl:variable>
	<xsl:variable name="empty"/>


	<xsl:template match="*" mode="selfclosetag">
		<xsl:text>&lt;</xsl:text>
		<xsl:value-of select="name()"/>
		<xsl:apply-templates select="@*" mode="attribs"/>
		<xsl:text>/&gt;</xsl:text>
	</xsl:template>

	<xsl:template match="*" mode="opentag">
		<xsl:text>&lt;</xsl:text>
		<xsl:value-of select="name()"/>
		<xsl:apply-templates select="@*" mode="attribs"/>
		<xsl:text>&gt;</xsl:text>
	</xsl:template>

	<xsl:template match="*" mode="closetag">
		<xsl:text>&lt;/</xsl:text>
		<xsl:value-of select="name()"/>
		<xsl:text>&gt;</xsl:text>
	</xsl:template>

	<xsl:template match="* | text()" mode="nodetostring">
		<xsl:choose>
			<xsl:when test="boolean(name())">
				<xsl:choose>
					<!--
						 if element is not empty
					-->
					<xsl:when test="normalize-space(.) != $empty or *">
						<xsl:apply-templates select="." mode="opentag"/>
							<xsl:apply-templates select="* | text()" mode="nodetostring"/>
						<xsl:apply-templates select="." mode="closetag"/>
					</xsl:when>
					<!--
						 assuming emty tags are self closing, e.g. <img/>, <source/>, <input/>
					-->
					<xsl:otherwise>
						<xsl:apply-templates select="." mode="selfclosetag"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="@*" mode="attribs">
		<xsl:if test="position() = 1">
			<xsl:text> </xsl:text>
		</xsl:if>
		<xsl:value-of select="concat(name(), '=', $q, ., $q)"/>
		<xsl:if test="position() != last()">
			<xsl:text> </xsl:text>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>

Discuss this XSLT Utility

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.1
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details