-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdf-xml-to-nquads.xsl
142 lines (129 loc) · 4.97 KB
/
rdf-xml-to-nquads.xsl
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?xml version="1.1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<xsl:param name="graph"/>
<xsl:template match="/">
<c:body content-type="application/n-quads" xmlns:c="http://www.w3.org/ns/xproc-step" >
<xsl:apply-templates/>
</c:body>
</xsl:template>
<xsl:template match="rdf:RDF">
<xsl:apply-templates/>
</xsl:template>
<xsl:function name="rdf:node-identifier">
<xsl:param name="node"/>
<xsl:value-of select="
if ($node/@rdf:about) then
concat('<', resolve-uri($node/@rdf:about, ($node/ancestor-or-self::*/@xml:base)[1]), '>')
else if ($node/@rdf:ID) then
concat('<', resolve-uri(concat('#', $node/@rdf:ID), ($node/ancestor-or-self::*/@xml:base)[1]), '>')
else if ($node/@rdf:nodeID) then
concat('_:', $node/@rdf:nodeID)
else
concat('_:', generate-id($node))
"/>
</xsl:function>
<xsl:function name="rdf:literal">
<xsl:param name="text"/>
<xsl:param name="lang"/>
<xsl:param name="datatype"/>
<xsl:variable name="result">
<xsl:text>"</xsl:text>
<xsl:analyze-string select="$text" regex="(")|(\\)|(\n)|(\r)">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(1)">\"</xsl:when>
<xsl:when test="regex-group(2)">\\</xsl:when>
<xsl:when test="regex-group(3)">\n</xsl:when>
<xsl:when test="regex-group(4)">\r</xsl:when>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
<xsl:text>"</xsl:text>
<xsl:choose>
<xsl:when test="$datatype">^^<<xsl:value-of select="$datatype"/>></xsl:when>
<xsl:otherwise>
<xsl:if test="$lang">@<xsl:value-of select="$lang"/></xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="string($result)"/>
</xsl:function>
<!-- match a node element -->
<xsl:template match="*">
<xsl:variable name="subject" select="rdf:node-identifier(.)"/>
<!-- rdf:type property value specified by node element QName -->
<xsl:if test="not(self::rdf:Description)">
<!-- element name specifies a class -->
<xsl:variable name="class" select="concat('<', namespace-uri(.), local-name(.), '>')"/>
<xsl:call-template name="triple">
<xsl:with-param name="subject" select="$subject"/>
<xsl:with-param name="predicate" select="'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>'"/>
<xsl:with-param name="object" select="$class"/>
</xsl:call-template>
</xsl:if>
<!-- other properties specified by property elements -->
<xsl:for-each select="*">
<xsl:variable name="predicate" select="concat('<', namespace-uri(.), local-name(.), '>')"/>
<!-- property value may be specified by an element or an rdf:resource attribute or by text with an optional datatype attribute -->
<xsl:choose>
<xsl:when test="*">
<xsl:call-template name="triple">
<xsl:with-param name="subject" select="$subject"/>
<xsl:with-param name="predicate" select="$predicate"/>
<xsl:with-param name="object" select="rdf:node-identifier(*)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="@rdf:resource">
<xsl:call-template name="triple">
<xsl:with-param name="subject" select="$subject"/>
<xsl:with-param name="predicate" select="$predicate"/>
<xsl:with-param name="object" select="concat('<', resolve-uri(@rdf:resource, (ancestor-or-self::*/@xml:base)[1]), '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="triple">
<xsl:with-param name="subject" select="$subject"/>
<xsl:with-param name="predicate" select="$predicate"/>
<xsl:with-param name="object" select="rdf:literal(string(.), ancestor-or-self::*/@xml:lang, @rdf:datatype)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!-- other string properties may be specified by property attributes -->
<xsl:for-each select="@*">
<xsl:if test="namespace-uri(.) != 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ">
<xsl:variable name="predicate" select="concat('<', namespace-uri(.), local-name(.), '>')"/>
<xsl:call-template name="triple">
<xsl:with-param name="subject" select="$subject"/>
<xsl:with-param name="predicate" select="$predicate"/>
<xsl:with-param name="object" select="rdf:literal(., ancestor-or-self::*/@xml:lang, ())"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
<!-- recurse -->
<xsl:apply-templates select="*/*"/>
</xsl:template>
<xsl:template name="triple">
<xsl:param name="subject"/>
<xsl:param name="predicate"/>
<xsl:param name="object"/>
<xsl:variable name="graph" select="concat('<', $graph, '>')"/>
<xsl:value-of select="
concat(
$subject,
' ',
$predicate,
' ',
$object,
' ',
$graph,
'.',
codepoints-to-string(10)
)
"/>
</xsl:template>
</xsl:stylesheet>