在XSLT中的变量中读取XML标记的值

时间:2014-06-13 09:47:18

标签: sql xml xslt

我无法在任何地方找到类似的问题,所以这就是为什么发布这个新问题。  我有一个XML,我想阅读并使用XSLT转换为SQL。技巧部分是XML元素(即名称)未知,XML的XSD是即时生成的。 但众所周知的是元素的某些属性。

XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<et:ItemRecordList xmlns:et="urn:org:easetech:easytest:schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:org:easetech:easytest:schema ItemRecord.xsd ">

  <ItemRecord recordId="idvalue0" tableName="item_record">
    <itemId columnName="item_id" idColumn="true" length="36" nullable="false">itemId</itemId>
    <databaseInstitution columnName="database_institution" length="255" nullable="false">0</databaseInstitution>
    <lastModifiedDate columnName="last_modified_date" length="255" nullable="false">2001-12-31T12:00:00</lastModifiedDate>
  </ItemRecord>
</et:ItemRecordList>

我想使用这个XML并使用XXSLT将其转换为INSERT SQL语句。

我创建了一个像这样的XSLT:

<?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" />
<xsl:template match="/">
<xsl:for-each select="/*/*">

INSERT INTO
<xsl:value-of select="@tableName" />
(
<xsl:for-each select="/*/*/*">
 <xsl:variable name="columnName"><xsl:value-of select="@columnName"/></xsl:variable>
 <xsl:choose>
 <xsl:when test="@columnName"> 
    <xsl:value-of select="$columnName"/>
    <xsl:if test="position()!=last()">
        <xsl:text>, </xsl:text>
    </xsl:if>
</xsl:when> 
</xsl:choose>  
</xsl:for-each>
) 
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

这给了我这样的输出:

INSERT INTO item_record (item_id, database_institution, last_modified_date)

但我不知道如何创建查询的值部分。该值是我们使用的值

 <xsl:value-of select="." />

我尝试连接这些值,但遗憾的是concat并没有为我工作。我也玩过xsl:variable但是无法使它工作。如果有人可以帮助我使用XSLT,我可以使用它创建输出,如下所示,非常感谢。

   INSERT INTO item_record (item_id, database_institution, last_modified_date) values (itemId,0,2001-12-31T12:00:00)

2 个答案:

答案 0 :(得分:2)

怎么样:

<xsl:template match="/">
    <xsl:for-each select="/*/*">
        <xsl:text>INSERT INTO </xsl:text>
        <xsl:value-of select="@tableName" />

        <xsl:text> (</xsl:text>
        <xsl:for-each select="*[@columnName]">
            <xsl:value-of select="@columnName"/>
            <xsl:if test="position()!=last()">
                <xsl:text>, </xsl:text>
            </xsl:if>  
        </xsl:for-each>

        <xsl:text>) values (</xsl:text> 
        <xsl:for-each select="*[@columnName]">
            <xsl:value-of select="."/>
            <xsl:if test="position()!=last()">
                <xsl:text>, </xsl:text>
            </xsl:if>  
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

答案 1 :(得分:1)

下一个XSLT将生成所需的查询

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />

    <xsl:variable name="newline" select="'&#xa;'" />

    <xsl:template match="/">
        <xsl:apply-templates select="*/*/@tableName" />
    </xsl:template>

    <xsl:template match="@tableName">
        <xsl:value-of select="concat('INSERT INTO ', ., ' (')" />
        <xsl:apply-templates select="parent::*/*/@columnName" />
        <xsl:value-of select="') VALUES ('" />
        <xsl:apply-templates select="parent::*/*" mode="values" />
        <xsl:value-of select="concat(')', $newline)" />
    </xsl:template>

    <xsl:template match="@columnName">
        <xsl:choose>
            <xsl:when test="position() = last()">
                <xsl:value-of select="." />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(., ', ')" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="*" mode="values">
        <xsl:choose>
            <xsl:when test="position() = last()">
                <xsl:value-of select="." />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(., ', ')" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>