正如@thatotherguy 所建议的那样,您可能希望使用 XSLT 而不是 Bash 来执行此操作。您可以使用 Bash 解析 XML,但它可能会很快变得棘手。
按照@thatotherguy 的建议,您可以拥有一个看起来像这样的 XSLT 样式表:
<!-- kanjilookup.xsl -->
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="character"/>
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<!--
From https://stackoverflow.com/questions/9611569/xsl-how-do-you-capitalize-first-letter
-->
<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template name="capitalize">
<xsl:param name="string"/>
<xsl:value-of select=
"concat(translate(substring(
$string, 1, 1), $vLower, $vUpper),
substring($string, 2)
)
"/>
</xsl:template>
<xsl:template match="/">
<xsl:if test="string-length($character) = 0 or not(//literal[. = $character])">
<xsl:message terminate="yes">ERR: No input character given.</xsl:message>
</xsl:if>
<xsl:apply-templates select="characters/character[literal[. = $character]]"/>
</xsl:template>
<xsl:template match="character">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html>
</xsl:text>
<html>
<head/>
<body>
<table>
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="literal">
<caption>
<xsl:value-of select="."/>
</caption>
</xsl:template>
<xsl:template match="stroke_count">
<tr>
<td>
<xsl:call-template name="capitalize">
<xsl:with-param name="string" select="translate(local-name(), '_', ' ')"/>
</xsl:call-template>
</td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>
<xsl:template match="misc | reading_meaning | rmgroup">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="reading | meaning">
<tr>
<td>
<xsl:call-template name="capitalize">
<xsl:with-param name="string" select="local-name()"/>
</xsl:call-template>
<xsl:apply-templates select="@r_type"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
<xsl:template match="@r_type">
<xsl:value-of select="concat(' ', '(', ., ')')"/>
</xsl:template>
</xsl:stylesheet>
假设您有一个名为characters.xml
:
<characters>
<character>
<literal>恵</literal>
<misc>
<stroke_count>10</stroke_count>
</misc>
<reading_meaning>
<rmgroup>
<reading r_type="ja_on">ケイ</reading>
<reading r_type="ja_on">エ</reading>
<reading r_type="ja_kun">めぐ.む</reading>
<reading r_type="ja_kun">めぐ.み</reading>
<meaning>favor</meaning>
<meaning>blessing</meaning>
<meaning>grace</meaning>
<meaning>kindness</meaning>
</rmgroup>
</reading_meaning>
</character>
</characters>
您可以kanjilookup.xsl
像这样使用 XMLStarlet 在其上运行:
xml tr kanjilookup.xsl -s character=恵 characters.xml
这将生成一个如下所示的 HTML 表格(经过漂亮打印后):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table>
<tbody>
<caption>恵</caption>
<tr>
<td>Stroke count</td>
<td>10</td>
</tr>
<tr>
<td>Reading (ja_on)</td>
<td>ケイ</td>
</tr>
<tr>
<td>Reading (ja_on)</td>
<td>エ</td>
</tr>
<tr>
<td>Reading (ja_kun)</td>
<td>めぐ.む</td>
</tr>
<tr>
<td>Reading (ja_kun)</td>
<td>めぐ.み</td>
</tr>
<tr>
<td>Meaning</td>
<td>favor</td>
</tr>
<tr>
<td>Meaning</td>
<td>blessing</td>
</tr>
<tr>
<td>Meaning</td>
<td>grace</td>
</tr>
<tr>
<td>Meaning</td>
<td>kindness</td>
</tr>
</tbody>
</table>
</body>
</html>
当然,您必须修改 XSLT 样式表以满足您的需要。