所以我有一个 XML文件,我想使用xmlstarlet解析BASH脚本等(或者如果人们可以给我一个例子). 基本结构是这样的: character literal恵/literal misc stroke_count10/stroke_count /misc reading_meaning rmgroup read
基本结构是这样的:
<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>
那里还有其他一些领域,其含义和读数可能会发生变化.基本上我想得到所有的读数,含义,笔画数等,并用BASH生成一个HTML表格.
这也是一个包含许多需要查找的字符的大文件.所以我想用一个接受1美元的脚本来做这件事,并使用它来根据标签查找值.理想情况下它应该是:
kanjilookup.sh 恵
然后根据内容生成一个html表.
思考? (我也会使用像xpath这样的其他程序)
正如@thatotherguy建议的那样,你可能想用XSLT而不是Bash这样做.你 can parse XML with Bash,但它可能会很快变得棘手.按照@theotherguy的建议,你可以有一个类似于这样的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>
您可以使用XMLStarlet在其上运行kanjilookup.xsl,如下所示:
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样式表以满足您的需求.