使用 XML 和 XSLT 生成 SQL [英] Generating SQL using XML and XSLT

查看:30
本文介绍了使用 XML 和 XSLT 生成 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 定义,其中包含一个带有子元素的元素.例如:

I have an XML definition that contains an element with child elements. For example:

<a>
 <b>
  <c>C</c>
  <d>D</d>
 </b>
</a>

我有一个带有文本输出的 XSLT.例如:

I have an XSLT with an output of text. For example:

<xsl...>
  <xsl:output method="text" indent="yes"/>
  <xsl:template match="/">
    <xsl:copy-of select="/a/b" />
  ...

我想将整个 b 元素及其子元素复制到一个去除空格的字符串中,以便我可以生成 SQL 查询.例如:

I want to copy the entire b element and its children into a whitespace-removed string so that I can generate a SQL query. For example:

select * from some-table where xml = '<b><c>C</c><d>D</d></b>'

目前 copy-of 正在查找 b 元素,但删除所有元素和属性信息,只留下每个元素中的文本内容.我认为这可能与输出类型有关.

At the moment copy-of is finding the b element but dropping off all element and attribute information leaving only the text content within each. I think this might be to do with the output type.

有什么想法吗?

推荐答案

这是如何做到的:

<xsl:output method="xml" />

<xsl:template match="/"><xsl:apply-templates select="/a/b" mode="normalize-space" /></xsl:template>

<xsl:template match="text()" mode="normalize-space"><xsl:value-of select="normalize-space(.)" /></xsl:template>
<xsl:template match="@*|node()" mode="normalize-space"><xsl:copy><xsl:apply-templates select="@*|node()" mode="normalize-space" /></xsl:copy></xsl:template>

此方法复制节点、具有命名空间和属性的节点.

This method copies nodes, nodes with namespaces and attributes.

方法要求输出为xml"(而不是原始示例中的文本").它为所有 TEXT 节点使用自定义模板来规范它们内部的空间(删除前导/尾随空格,将多个空格压缩为一个空格).然后,它使用简单的身份"模板复制所有节点及其属性.两个模板都使用特殊模式来不干扰 XSL 的其余部分.

Method requires output to be "xml" (not "text" as in original sample). It uses custom template for all TEXT nodes to normalize space inside them (remove leading/trailing whitespace, condense multiple spaces into a single space). Then, it uses simple "identity" template that copies all nodes and their attributes. Both templates use special mode to not to interfere with the rest of XSL.

不幸的是,XSLT 处理器将 xsl:template 标签内的所有未知"节点复制到输出文档中,而空格就是这样的节点之一.这就是为什么所有这些模板都需要写在一行中,没有多余的空格.

Unforunately, XSLT processor copies all "unknown" nodes inside xsl:template tag into output document and spaces are one of such nodes. That's why all those templates need to be written in one line with no extra spaces.

PS 虽然,我同意在 RDBMS 中搜索规范化的 XML 有点奇怪.

PS Although, I agree that searching a normalized XML in RDBMS is kind of weird.

这篇关于使用 XML 和 XSLT 生成 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆