Created
April 8, 2013 05:52
-
-
Save hbsnow/5334515 to your computer and use it in GitHub Desktop.
XSLTでabbr要素によってマークアップされた略語に、自動的にtitle属性を別ファイルのXMLを使用して挿入する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<rdf:RDF xml:lang="en" | |
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | |
xmlns:skos="http://www.w3.org/2004/02/skos/core#"> | |
<skos:Concept rdf:about="http://example.com/glossary/html"> | |
<skos:prefLabel>HyperText Markup Language</skos:prefLabel> | |
<skos:altLabel>HTML</skos:altLabel> | |
</skos:Concept> | |
</rdf:RDF> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?xml-stylesheet href="example.xsl" type="text/xsl"?> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>略語にtitle属性を挿入するサンプル</title> | |
<style type="text/css">abbr::after {content:"(" attr(title) ")";}</style> | |
</head> | |
<body> | |
<h1>略語にtitle属性を挿入するサンプル</h1> | |
<p><abbr>HTML</abbr></p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.0" | |
xmlns="http://www.w3.org/1999/xhtml" | |
xmlns:x="http://www.w3.org/1999/xhtml" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | |
xmlns:skos="http://www.w3.org/2004/02/skos/core#" | |
exclude-result-prefixes="x rdf skos"> | |
<xsl:key name="abbr-key" match="rdf:RDF/skos:Concept" use="skos:altLabel"/> | |
<xsl:template match="x:abbr"> | |
<xsl:choose> | |
<xsl:when test="x:abbr"> | |
<!-- title要素がある場合はそのまま出力 --> | |
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy> | |
</xsl:when> | |
<xsl:otherwise> | |
<!-- 用語集から略語の正式名を取得して出力 --> | |
<xsl:apply-templates select="document('example.rdf')/rdf:RDF/skos:Concept[skos:altLabel = current()]"> | |
<xsl:with-param name="abbr" select="self::node()"/> | |
</xsl:apply-templates> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
<xsl:template match="rdf:RDF/skos:Concept"> | |
<xsl:param name="abbr"/> | |
<abbr title="{key('abbr-key', $abbr)/skos:prefLabel}"><xsl:value-of select="key('abbr-key', $abbr)/skos:altLabel"/></abbr> | |
</xsl:template> | |
<xsl:template match="node()|@*" priority="-1.0"> | |
<!-- 指定のない要素は属性と共にそのまま出力する。 --> | |
<xsl:copy> | |
<xsl:apply-templates select="node()|@*"/> | |
</xsl:copy> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment