[英]How to convert content between two patterns into specific format using shell
我們如何使用Shell代碼將2種模式之間的內容轉換為特定格式? 以下以<Mapping>
開頭並以</Mapping>
結束的示例XML需要轉換為計划格式代碼,如下所示。
輸入代碼示例:
<Mapping name="temp1"> /*rule name will the value of Mapping name*/
<phpCode>
boolean_out = copyfunc temp /*rule content output */
</phpCode>
</Mapping>
name
的值將是規則名稱,而boolean_out
的值將是規則內容。
示例輸出代碼:
rule temp1 { // temp1 is the mapping value
copyfunc temp //boolean_out value is rule content
}
給定的input.xml包含:
<Mapping name="temp1"> /*rule name will the value of Mapping name*/
<phpCode>
boolean_out = copyfunc temp /*rule content output */
</phpCode>
</Mapping>
並包含以下內容的transform.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8" indent="no" />
<xsl:template match="Mapping">
<xsl:text>rule </xsl:text>
<xsl:value-of select="@name" />
<xsl:text> { // </xsl:text>
<xsl:value-of select="@name" />
<xsl:text> is the mapping value</xsl:text>
<xsl:apply-templates select="./phpCode" mode="php-code" />
<xsl:text>
}
</xsl:text>
</xsl:template>
<xsl:template match="*" mode="php-code">
<xsl:text>
 </xsl:text>
<xsl:value-of select="substring-before(substring-after(normalize-space(text()),'= '),'/*')" />
<xsl:text>//</xsl:text>
<xsl:value-of select="substring-before(normalize-space(text()),'=')" />
<xsl:text> value is rule content</xsl:text>
</xsl:template>
</xsl:stylesheet>
XSLT轉換產生:
rule temp1 { // temp1 is the mapping value
copyfunc temp //boolean_out value is rule content
}
調用轉換的方法是特定於平台和工具的。 可以使用命令行工具來調用轉換,盡管可以通過多種方式來運行XSL腳本。 例如,一個xsltproc
命令是:
xsltproc transform.xsl input.xml
可以類似地使用msxsl.exe
,只是命令參數相反。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.