Hvis du nu skal indlæse XML'en i en database der kun kan indeholde noder med eksempelvis 4000 tegn, så kan du via XSLT få klippet det overflødige.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="text()"> <xsl:variable name="numlenght"> <xsl:value-of select="string-length(.)" /> </xsl:variable> <xsl:choose> <xsl:when test="string-length(.)<4000"> <xsl:value-of select="."/> </xsl:when> <xsl:when test="string-length(.)>4000"> <xsl:value-of select="substring(.,1,4000)"/> </xsl:when> </xsl:choose> </xsl:template> </xsl:stylesheet>
I Groovy kunne man eksempelvis gøre således:
import javax.xml.stream.*
import javax.xml.transform.*
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource
static final String XSLT = '''
... som skrevet ovenover ...
'''
def xml = new File(filename).getText('utf-8')
def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(new StringReader(XSLT)))
StringWriter writer = new StringWriter(xml.size())
transformer.transform(new StreamSource(new StringReader(xbrl)), new StreamResult(writer))
def cuttetXML = writer.toString()