将 XML 节点名称转换为大写 (XSLT 1.0) [英] Converting XML Node Names to Uppercase (XSLT 1.0)

查看:37
本文介绍了将 XML 节点名称转换为大写 (XSLT 1.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多 XSLT 样式表可以将一些 XML 转换为 csv 文件.所使用的 XML 文件是基于数据库列名生成的,在构建 XML 时,这些列名过去会自动转换为大写——这已经不能再完成了(我们没有使用 TSQL-FOR XML 来构建 XML).列名通常是大写和小写字母的混合.由于所有样式表当前都引用大写列名,因此 XPath 查询失败.

I have many XSLT stylesheet(s) that transform some XML into a csv file. The XML files used are generated based on database column names which used to be converted automatically to UPPERCASE when the XML was being built - which can no longer be done (we are no using TSQL-FOR XML to build the XML). The column names are Generally a mixture of uppercase and lowercase letters. As all Stylesheets currently reference Uppercase column names, the XPath queries are failing.

而不是通过所有 XSL 样式表并手动将 XPath 查询更改为数据库列名称的大小写 - 这将花费今年的大部分时间(!)有什么办法可以将所有的 XML 'Tag' 名称转换为大写,以便您可以在文档中使用它们吗?

Instead of going through ALL XSL stylesheets and manually changing the XPath queries to the case of the database column names - which would take most of this year(!) Is there any way to convert all the XML 'Tag' names to Uppercase, so you can use them in the document?

非常感谢任何帮助!!

谢谢!安德鲁

一个例子:下面将生成一个 csv 文件,右侧为 Of rows,但没有任何数据,因为 xslt 正在寻找STRNAME",当它作为strName"存储在 XML 中时我的 XML:

An example: The below will generate a csv file with the right about Of rows, but without any data, as the xslt is looking for "STRNAME", when it is stored in The XML as "strName" my XML:

<XMLWRAPPER>
   <STAFFTABLE>
      <ROW>
         <strName>Andrew</strName>
         <strSurname>Smith</strSurname>
         <intuserType>1</intUserType>
      </ROW>
      <ROW>
         <strName>Jackie</strName>
         <strSurname>collins</strSurname>
         <intuserType>2</intUserType>
      </ROW>
    </STAFFTABLE>
  </XMLWRAPPER>

和 xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes"/>

<xsl:template match="/">"First Name","Surname","User type"<xsl:text>&#013;</xsl:text>

    <xsl:for-each select="/XMLWRAPPER/STAFFTABLE/ROW">
        <xsl:value-of select="STRNAME"/>,<xsl:value-of select="STRSURNAME"/>,<xsl:value-of select="INTUSERTYPE"/><xsl:text>&#013;</xsl:text>
     </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

推荐答案

在 XSLT 1.0 中,我会使用 身份转换translate() 函数:

In XSLT 1.0 I would use the Identity Transformation and the translate() function:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{
            translate(name(.),
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

此转换将仅大写"元素,而不是属性.无论如何,只需将第二个模板的匹配模式更改为 *|@* 即可轻松将其扩展到属性.

This transform will "upper case" elements only, not the attributes. It is easy anyway extend it to attributes just by changing the match pattern of the second template to *|@*.

根据下面的评论和规范translate() 函数并不是所有语言中大小写转换的充分解决方案.

As per the comments below and the specs, translate() function is not a sufficient solution for case conversion in all languages.

这篇关于将 XML 节点名称转换为大写 (XSLT 1.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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