My favorites
▼
|
Sign in
lindenb
Pierre Lindenbaum's library
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
xsl
/
xml2dom.xsl
‹r250
r508
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?xml version='1.0' ?>
<xsl:stylesheet
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns="http://www.w3.org/1999/xhtml"
version='1.0'
>
<!--
Author:
Pierre Lindenbaum PhD
plindenbaum@yahoo.fr
Motivation:
This stylesheet transform xml/DOM to javascript statements
-->
<xsl:output method="text" />
<xsl:variable name="datadoc">true</xsl:variable>
<xsl:variable name="NodeType">var</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<xsl:if test=".."><xsl:if test="$datadoc='false' or ($datadoc='true' and count(../child::*) = 0)">
<xsl:value-of select="translate(local-name(..),'-','_')"/>_<xsl:value-of select="generate-id(..)"/>.appendChild(document.createTextNode("<xsl:call-template name="escape"><xsl:with-param name="s"><xsl:value-of select="."/></xsl:with-param></xsl:call-template>"));<xsl:text>
</xsl:text>
</xsl:if></xsl:if>
</xsl:template>
<xsl:template match="node()">
<xsl:variable name="owner"><xsl:value-of select="translate(local-name(.),'-','_')"/>_<xsl:value-of select="generate-id(.)"/></xsl:variable>
<xsl:value-of select="$NodeType"/><xsl:text> </xsl:text><xsl:value-of select="$owner"/>= <xsl:choose>
<xsl:when test="namespace-uri(.)=''">document.createElement("<xsl:value-of select="name(.)"/>");</xsl:when>
<xsl:otherwise>document.createElementNS(<xsl:call-template name="namespace"><xsl:with-param name="uri"><xsl:value-of select="namespace-uri(.)"/></xsl:with-param></xsl:call-template>,"<xsl:value-of select="name(.)"/>");</xsl:otherwise>
</xsl:choose><xsl:text>
</xsl:text>
<xsl:if test="..">
<xsl:value-of select="translate(local-name(..),'-','_')"/>_<xsl:value-of select="generate-id(..)"/>.appendChild(<xsl:value-of select="$owner"/>);
</xsl:if>
<xsl:for-each select="@*">
<xsl:value-of select="$owner"/>.<xsl:choose>
<xsl:when test="namespace-uri(.)=''">setAttribute("<xsl:value-of select="name(.)"/>","<xsl:call-template name="escape"><xsl:with-param name="s"><xsl:value-of select="."/></xsl:with-param></xsl:call-template>");</xsl:when>
<xsl:otherwise>setAttributeNS(<xsl:call-template name="namespace"><xsl:with-param name="uri"><xsl:value-of select="namespace-uri(.)"/></xsl:with-param></xsl:call-template>,"<xsl:value-of select="name(.)"/>","<xsl:call-template name="escape"><xsl:with-param name="s"><xsl:value-of select="."/></xsl:with-param></xsl:call-template>");</xsl:otherwise>
</xsl:choose><xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:apply-templates select="*|text()"/>
</xsl:template>
<xsl:template name="namespace">
<xsl:param name="uri"/>
<xsl:choose>
<xsl:when test="$uri='http://www.w3.org/1999/XSL/Transform'">XSL.NS</xsl:when>
<xsl:when test="$uri='http://www.w3.org/1999/xhtml'">HTML.NS</xsl:when>
<xsl:when test="$uri='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'">XUL.NS</xsl:when>
<xsl:when test="$uri='http://www.w3.org/2000/svg'">SVG.NS</xsl:when>
<xsl:when test="$uri='http://www.w3.org/1999/xlink'">XLINK.NS</xsl:when>
<xsl:when test="$uri='http://www.w3.org/1999/02/22-rdf-syntax-ns#'">RDF.NS</xsl:when>
<xsl:when test="$uri='http://www.w3.org/2000/01/rdf-schema#'">RDFS.NS</xsl:when>
<xsl:otherwise>"<xsl:value-of select="$uri"/>"</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="escape">
<xsl:param name="s"/><xsl:variable name="c"><xsl:value-of select="substring($s,1,1)"/></xsl:variable>
<xsl:choose>
<xsl:when test="$c='
'">\n</xsl:when>
<xsl:when test='$c="'"'>\'</xsl:when>
<xsl:when test="$c='"'">\"</xsl:when>
<xsl:otherwise><xsl:value-of select="$c"/></xsl:otherwise>
</xsl:choose><xsl:if test="string-length($s) >1"><xsl:call-template name="escape">
<xsl:with-param name="s"><xsl:value-of select="substring($s,2,string-length($s)-1)"/></xsl:with-param>
</xsl:call-template></xsl:if>
</xsl:template>
</xsl:stylesheet>
Show details
Hide details
Change log
r251
by plindenbaum on Apr 10, 2009
Diff
cont
Go to:
...denb/tinytools/Consequences.java
...g/lindenb/sw/vocabulary/OWL.java
...src/java/org/lindenb/util/C.java
...rg/lindenb/util/StringUtils.java
...rg/lindenb/xml/XMLUtilities.java
/trunk/src/xsl/xml2dom.xsl
Project members,
sign in
to write a code review
Older revisions
r250
by plindenbaum on Apr 10, 2009
Diff
xml2dom
All revisions of this file
File info
Size: 3833 bytes, 85 lines
View raw file
Powered by
Google Project Hosting