Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 读取XSLT内部变量中XML标记的值_Sql_Xml_Xslt - Fatal编程技术网

Sql 读取XSLT内部变量中XML标记的值

Sql 读取XSLT内部变量中XML标记的值,sql,xml,xslt,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.

我在任何地方都找不到类似的问题,这就是为什么我要发布这个新问题的原因。 我有一个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 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="." />

下一个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>

那么:

<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>

插入
(
, 
)价值(
, 

是否有理由需要执行类似于
/*/*/*/*
的操作,而不是通过名称显式地对元素进行寻址(这样不仅可读性更好,而且效率更高)?是的。因为我不知道处理时元素的名称,因为XML的XSD是在运行时生成的。嗨@michael.hor257k,你能帮我一下吗:谢谢michael。这就成功了。你能告诉我“*[@columnName]”实际上是什么意思吗?它是一个谓词。它的意思是“仅选择具有
columnName
属性的元素”。因此,它将替换
xsl:when
条件。如果
ItemRecord
的所有子项都是具有
columnName
属性的列,则不需要此条件。
<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>