如何将 XML 样式更改为 HTML 标签

问题描述 投票:0回答:1

我有一个 XML 文件,我正在使用 XSLT 版本 1.0 样式表将其转换为 HTML。

XML 有一个文本元素

ExampleText/AStr
,其中包含
Run
元素,其中一些元素的名为
namedStyle
的属性设置为
"Headword in Example"
。在我的输出 HTML 中,我想使用 HTML
<mark>
标记突出显示这些元素。

这是从输入 XML 中提取的示例:

<ExampleText>
<AStr ws="jii">
<Run ws="jii">Highlighting one piece with fancy style==</Run>
<Run namedStyle="Headword in Example" ws="jii">only expected highlighted text</Run>
<Run ws="jii">==after highlighted text</Run>
</AStr>
</ExampleText>

我希望上述 XML 的输出 HTML 包含以下内容:

Highlighting one piece with fancy style==<mark>only expected highlighted text</mark>==after highlighted text

我不介意 HTML 输出包含

<Run>
标记,只要它添加
<mark>
突出显示即可。

我的 XSLT 文件将输入 XML 文件转换为 HTML 表,其中框中包含

ExampleText/AStr
元素。 它正确处理大部分 XML,但无法正确查找和转换我上面提到的元素:

这是完整的输入 XML

test.xml
:

<?xml version="1.0" encoding="utf-8"?>
<LexExamplePatchSet>
<LexExamplePatch>
<ExampleText>
<AStr ws="jii">
<Run ws="jii">no highlighting</Run>
</AStr>
</ExampleText>
</LexExamplePatch>
<LexExamplePatch>
<ExampleText>
<AStr ws="jii">
<Run ws="jii">highlighting with html mark ==
<mark>highlighted text</mark>== unmarked after</Run>
</AStr>
</ExampleText>
</LexExamplePatch>
<LexExamplePatch>
<ExampleText>
<AStr ws="jii">
<Run ws="jii">Highlighting one piece with fancy style==</Run>
<Run namedStyle="Headword in Example" ws="jii">only expected highlighted text</Run>
<Run ws="jii">==after highlighted text</Run>
</AStr>
</ExampleText>
</LexExamplePatch>
<LexExamplePatch>
<ExampleText>
<AStr ws="jii">
<Run ws="jii">Highlighting two pieces with fancy style==</Run>
<Run namedStyle="Headword in Example" ws="jii">first expected highlighted text</Run>
<Run ws="jii">== between first and second text ==</Run>
<Run namedStyle="Headword in Example" ws="jii">second highlighted text</Run>
<Run ws="jii">== after the second</Run>
</AStr>
</ExampleText>
</LexExamplePatch>
</LexExamplePatchSet>

这是 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="1.0"
    exclude-result-prefixes="xs">
    
    <xsl:template match="LexExamplePatchSet">
        <html>
            <head>
                <title>Example Sentences (with highlighting)</title>
            </head>
            <body>
                <h2>Example Sentences</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Example Sentence</th>
                    </tr>
                    <xsl:for-each select="LexExamplePatch">
                        <tr>
                            <td>
                                <xsl:copy-of select="ExampleText"/>   
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
    
<xsl:template match="//LexExamplePatch/ExampleText/AStr/Run[@namedStyle='Headword in Example']">
        <mark><xsl:value-of select="."/></mark>
    </xsl:template>
    
    <!-- identity transform -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

我使用

xmlstarlet
测试了匹配标准并得到了预期的结果:

$ xmlstarlet sel  -t -m '//LexExamplePatch/ExampleText/AStr/Run[@namedStyle="Headword in Example"]' -v . -n <test.xml
only expected highlighted text
first expected highlighted text
second highlighted text

这是我生成

test-result.html
文件的方法

xsltproc test.xsl test.xml >test-result.html

这是生成的 HTML

test-result.html
文件,没有所需的 HTML
<mark>
标签(唯一的
<mark>
标签位于原始 XML 中)。它包含一些原始 XML,但我不介意,因为我的浏览器不会呈现它:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Example Sentences (with highlighting)</title>
</head>
<body>
<h2>Example Sentences</h2>
<table border="1">
<tr bgcolor="#9acd32"><th>Example Sentence</th></tr>
<tr><td><ExampleText>
<AStr ws="jii">
<Run ws="jii">no highlighting</Run>
</AStr>
</ExampleText></td></tr>
<tr><td><ExampleText>
<AStr ws="jii">
<Run ws="jii">highlighting with html mark ==
<mark>highlighted text</mark>== unmarked after</Run>
</AStr>
</ExampleText></td></tr>
<tr><td><ExampleText>
<AStr ws="jii">
<Run ws="jii">Highlighting one piece with fancy style==</Run>
<Run namedStyle="Headword in Example" ws="jii">only expected highlighted text</Run>
<Run ws="jii">==after highlighted text</Run>
</AStr>
</ExampleText></td></tr>
<tr><td><ExampleText>
<AStr ws="jii">
<Run ws="jii">Highlighting two pieces with fancy style==</Run>
<Run namedStyle="Headword in Example" ws="jii">first expected highlighted text</Run>
<Run ws="jii">== between first and second text ==</Run>
<Run namedStyle="Headword in Example" ws="jii">second highlighted text</Run>
<Run ws="jii">== after the second</Run>
</AStr>
</ExampleText></td></tr>
</table>
</body>
</html>
xml xslt
1个回答
0
投票

这将有助于查看您期望获得的确切结果。将非 HTML 元素复制到 HTML 输出中没有什么意义。

我认为这可能是您正在寻找的 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>

<xsl:template match="/LexExamplePatchSet">
    <html>
        <head>
            <title>Example Sentences (with highlighting)</title>
        </head>
        <body>
            <h2>Example Sentences</h2>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th>Example Sentence</th>
                </tr>
                <xsl:for-each select="LexExamplePatch">
                    <tr>
                        <td>
                            <xsl:apply-templates/>   
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="mark | Run[@namedStyle='Headword in Example']">
    <mark>
        <xsl:apply-templates/>   
    </mark>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.