我是 XSLT 新手。我有一个 XML 文件,想使用 XSLT 将其转换为 RDF/XML。实际上,我找到了一个 XSLT 样式表,在它和 XML 文件之间建立链接,结果在浏览器中仅显示“文本”而不是 XML 文件。我的问题是:我想获得 RDF/XML 格式的转换结果,但不幸的是我得到的结果是纯文本。
XML 文件
<xml>
<?xml-stylesheet type="text/xsl" href="qu.xsl"?>
<person>
<name>Joe</name>
<website url="www.example1.com">contact1</website >
<vote>20</vote>
</person>
<person>
<name>Anna</name>
<website url="www.example2.com">contact2</website>
<vote>80</vote>
</person>
</xml>
Run Code Online (Sandbox Code Playgroud)
XSLT 样式表是
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:foaf="http://xmlns.com/foaf/spec/"
xmlns:foo="http://example.com/foo#">
<xsl:template match="/">
<rdf:RDF>
<rdf:Description rdf:about="http://www.example.com/xml">
<xsl:apply-templates/>
</rdf:Description>
</rdf:RDF>
</xsl:template>
<xsl:template match="person">
<xsl:variable name="critic"><xsl:value-of select="name"/></xsl:variable>
<xsl:variable name="criticWebsite"><xsl:value-of select="website/@url"/</xsl:variable>
<foo:hasCritic>
<rdf:Description rdf:about="http://www.example.com/critic/{$critic}">
<foaf:name><xsl:value-of select="name"/></foaf:name>
<foaf:homepage>
<rdf:Description rdf:about="http://{$criticWebsite}">
<rdfs:label><xsl:value-of select="website"/></rdfs:label>
</rdf:Description>
</foaf:homepage>
</rdf:Description>
</foo:hasCritic>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
但结果是: Joe contact1 20 Anna contact2 80
您的 XSLT(修复了 附近的拼写错误criticWebsite xsl:value-of):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:foaf="http://xmlns.com/foaf/spec/"
xmlns:foo="http://example.com/foo#">
<xsl:template match="/">
<rdf:RDF>
<rdf:Description rdf:about="http://www.example.com/xml">
<xsl:apply-templates/>
</rdf:Description>
</rdf:RDF>
</xsl:template>
<xsl:template match="person">
<xsl:variable name="critic"><xsl:value-of select="name"/></xsl:variable>
<xsl:variable name="criticWebsite"><xsl:value-of select="website/@url"/></xsl:variable>
<foo:hasCritic>
<rdf:Description rdf:about="http://www.example.com/critic/{$critic}">
<foaf:name><xsl:value-of select="name"/></foaf:name>
<foaf:homepage>
<rdf:Description rdf:about="http://{$criticWebsite}">
<rdfs:label><xsl:value-of select="website"/></rdfs:label>
</rdf:Description>
</foaf:homepage>
</rdf:Description>
</foo:hasCritic>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用于您的输入 XML 文件(声明xml-stylesheet立即移至 XML 声明下方,并且 XSLT 文件的路径设为绝对路径):
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="file:///c:/path/to/XSLT/qu.xsl"?>
<xml>
<person>
<name>Joe</name>
<website url="www.example1.com">contact1</website >
<vote>20</vote>
</person>
<person>
<name>Anna</name>
<website url="www.example2.com">contact2</website>
<vote>80</vote>
</person>
</xml>
Run Code Online (Sandbox Code Playgroud)
生成此 RDF 文档:
<?xml version="1.0" encoding="UTF-8"?><rdf:RDF xmlns:html="http://www.w3.org/1999/xhtml" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:foaf="http://xmlns.com/foaf/spec/" xmlns:foo="http://example.com/foo#"><rdf:Description rdf:about="http://www.example.com/xml">
<foo:hasCritic><rdf:Description rdf:about="http://www.example.com/critic/Joe"><foaf:name>Joe</foaf:name><foaf:homepage><rdf:Description rdf:about="http://www.example1.com"><rdfs:label>contact1</rdfs:label></rdf:Description></foaf:homepage></rdf:Description></foo:hasCritic>
<foo:hasCritic><rdf:Description rdf:about="http://www.example.com/critic/Anna"><foaf:name>Anna</foaf:name><foaf:homepage><rdf:Description rdf:about="http://www.example2.com"><rdfs:label>contact2</rdfs:label></rdf:Description></foaf:homepage></rdf:Description></foo:hasCritic>
</rdf:Description></rdf:RDF>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4701 次 |
| 最近记录: |