Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 07:22
Show Gist options
  • Save anonymous/4335037 to your computer and use it in GitHub Desktop.
Save anonymous/4335037 to your computer and use it in GitHub Desktop.
URL verification with XSLT
<?xml version="1.0" encoding="UTF-8"?>
<root>
<url>See http://www.uis.no&lt;/url>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://fsws.usit.no/schemas/studinfo"
xmlns:studinfo="http://fsws.usit.no/schemas/studinfo">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="url" minOccurs="0" type="xs:anyURI"/>
<xs:element name="url-text" minOccurs="0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="url">
<xsl:call-template name="dirty_URL">
<xsl:with-param name="add-to-element" select="'url-text'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="dirty_URL">
<xsl:param name="add-to-element"/>
<xsl:variable name="url">
<xsl:apply-templates select="text()" mode="serialize"/>
</xsl:variable>
<xsl:variable name="new_url">
<xsl:value-of select="iri-to-uri($url)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$url = $new_url">
<xsl:copy>
<xsl:value-of select="$new_url"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$add-to-element}">
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="serialize">
<xsl:text>&lt;</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>&gt;</xsl:text>
<xsl:apply-templates mode="serialize"/>
<xsl:text>&lt;/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>&gt;</xsl:text>
</xsl:template>
<xsl:template match="text()" mode="serialize">
<xsl:value-of select="." xml:space="preserve"/>
</xsl:template>
</xsl:stylesheet>
@maddingo
Copy link

There is a minor error in dirty.xml the third line should be not have the lt entity. But I couldn't figure out how to update this gist. Checkout my gist at https://gist.github.com/4335076 for revised version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment