[Dive4elements-commits] [PATCH] Add XSL transformation that replaces <dc:attribute> with their shortened inline notation

Wald Commits scm-commit at wald.intevation.org
Thu Mar 28 15:12:30 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1364479946 -3600
# Node ID 43bf4976dd24d986821f93fcf8b2aae7bfc8173f
# Parent  d459a885c51fb44240de367ffd2a60f3a4ffc052
Add XSL transformation that replaces <dc:attribute> with their shortened inline notation.

diff -r d459a885c51f -r 43bf4976dd24 flys-artifacts/contrib/inline-dc-attribute.xsl
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/contrib/inline-dc-attribute.xsl	Thu Mar 28 15:12:26 2013 +0100
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    inline-dc-attribute.xsl
+    =======================
+    Transforms datacage templates from:
+
+        <foo>
+          <dc:element name="bar" value="${baz}"/>
+          <dc:element name="bla" value="${blub}-${urgs}"/>
+        </foo>
+
+    to:
+
+        <foo bar="{$bar} bla="{$blub}-{$urgs}/>
+-->
+<xsl:stylesheet version="1.0"
+  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+  xmlns:dc="http://www.intevation.org/2011/Datacage">
+
+  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
+
+  <xsl:template name="string-replace-all">
+    <xsl:param name="text"/>
+    <xsl:param name="replace"/>
+    <xsl:param name="by"/>
+    <xsl:choose>
+      <xsl:when test="contains($text, $replace)">
+        <xsl:value-of select="substring-before($text,$replace)"/>
+        <xsl:value-of select="$by"/>
+        <xsl:call-template name="string-replace-all">
+          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
+          <xsl:with-param name="replace" select="$replace"/>
+          <xsl:with-param name="by" select="$by"/>
+        </xsl:call-template>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:value-of select="$text"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template
+    match="node()[count(dc:attribute) > 0 and namespace-uri() != 'http://www.intevation.org/2011/Datacage']">
+    <xsl:copy>
+      <xsl:for-each select="./dc:attribute">
+        <xsl:attribute name="{@name}">
+          <xsl:call-template name="string-replace-all">
+            <xsl:with-param name="text" select="@value"/>
+            <xsl:with-param name="replace">${</xsl:with-param>
+            <xsl:with-param name="by">{$</xsl:with-param>
+          </xsl:call-template>
+        </xsl:attribute>
+      </xsl:for-each>
+      <xsl:apply-templates select="@*|node()" mode="ignore-text"/>
+    </xsl:copy>
+  </xsl:template>
+
+  <xsl:template match="dc:attribute|text()" mode="ignore-text"/>
+  <xsl:template match="@*|node()" mode="ignore-text">
+    <xsl:copy>
+      <xsl:apply-templates select="@*|node()"/>
+    </xsl:copy>
+  </xsl:template>
+
+  <xsl:template match="@*|node()">
+    <xsl:copy>
+      <xsl:apply-templates select="@*|node()"/>
+    </xsl:copy>
+  </xsl:template>
+
+</xsl:stylesheet>


More information about the Dive4elements-commits mailing list