如何连接“<”和“>”字符以通过XML和XSL从postgresql构建一个img标签到HTML
How to CONCAT "<" and ">" characters to build an img tag from postgresql via XML and XSL to HTML
我正在扩展现有的 HTML 报告以添加一行图像。所有数据都来自 PostgreSQL 数据库,查询被转换为 XML,然后通过 XSLT-1.0 转换生成最终的 HTML 输出页面。
图像数据以BYTEA形式存储,提取如下(在SQL文件中):
SELECT string_agg(
xmlelement(NAME "other_data", comes.from_here),
xmlelement(NAME "more_data", comes.from_here_too),
xmlelement(NAME "image_file", CONCAT('<img src="data:image/png;base64,iVBORw0KG...', encode(rpaf.image_file, 'base64'), '"/>'))
)::TEXT
XSL 文件使用提取的数据执行此操作...
<tr>
<td colspan="12" class="table_data" align="left">
<xsl:value-of select="image_file"/>
</td>
</tr>
但是,'<' 和 '>' 字符被替换为 '<'
和 '>'
,这导致 HTML 只输出原始文本而不是图像( wikipedia)...
中的红点图像
<td colspan="12" class="table_data" align="left"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="></td>
如果不在 SQL 中,我应该在哪里添加“<”和“>”字符?我试过在 XSL 文件中添加字符,但它也不起作用。
答案:
您不应混合使用 xml 和 string_agg。此外,您应该将 img 形成为 xml 元素,而不是文本值:
SELECT
xmlelement(NAME "table_data",
xmlelement(NAME "other_data", comes.from_here),
xmlelement(NAME "more_data", comes.from_here_too),
xmlelement(NAME "image_file",
xmlelement(NAME "img", XMLATTRIBUTES( CONCAT('data:image/png;base64,iVBORw0KG...', encode(rpaf.image_file, 'base64')) as src )))
)
FROM
(VALUES('here', 'here-too')) comes(from_here , from_here_too),
(VALUES('image'::bytea)) rpaf(image_file)
它returns:
<table_data>
<other_data>here</other_data>
<more_data>here-too</more_data>
<image_file>
<img src="data:image/png;base64,iVBORw0KG...aW1hZ2U="/>
</image_file>
</table_data>
我的2美分:
我认为您在尝试将 html img
元素导入 xsl 模板时犯了一个错误。查看 与您的问题类似的问题。
我正在扩展现有的 HTML 报告以添加一行图像。所有数据都来自 PostgreSQL 数据库,查询被转换为 XML,然后通过 XSLT-1.0 转换生成最终的 HTML 输出页面。
图像数据以BYTEA形式存储,提取如下(在SQL文件中):
SELECT string_agg(
xmlelement(NAME "other_data", comes.from_here),
xmlelement(NAME "more_data", comes.from_here_too),
xmlelement(NAME "image_file", CONCAT('<img src="data:image/png;base64,iVBORw0KG...', encode(rpaf.image_file, 'base64'), '"/>'))
)::TEXT
XSL 文件使用提取的数据执行此操作...
<tr>
<td colspan="12" class="table_data" align="left">
<xsl:value-of select="image_file"/>
</td>
</tr>
但是,'<' 和 '>' 字符被替换为 '<'
和 '>'
,这导致 HTML 只输出原始文本而不是图像( wikipedia)...
<td colspan="12" class="table_data" align="left"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="></td>
如果不在 SQL 中,我应该在哪里添加“<”和“>”字符?我试过在 XSL 文件中添加字符,但它也不起作用。
答案:
您不应混合使用 xml 和 string_agg。此外,您应该将 img 形成为 xml 元素,而不是文本值:
SELECT
xmlelement(NAME "table_data",
xmlelement(NAME "other_data", comes.from_here),
xmlelement(NAME "more_data", comes.from_here_too),
xmlelement(NAME "image_file",
xmlelement(NAME "img", XMLATTRIBUTES( CONCAT('data:image/png;base64,iVBORw0KG...', encode(rpaf.image_file, 'base64')) as src )))
)
FROM
(VALUES('here', 'here-too')) comes(from_here , from_here_too),
(VALUES('image'::bytea)) rpaf(image_file)
它returns:
<table_data>
<other_data>here</other_data>
<more_data>here-too</more_data>
<image_file>
<img src="data:image/png;base64,iVBORw0KG...aW1hZ2U="/>
</image_file>
</table_data>
我的2美分:
我认为您在尝试将 html img
元素导入 xsl 模板时犯了一个错误。查看