我正在处理XSLT文件以将表示XML的文档转换为SQL命令。该XML描述两个表的列和每一行的数据结构如下:XSLT动态属性里面每个
<Table>
<ObjectAttributes>
<AttributeDetail>
<Name>COLNAME1</Name>
...
</AttributeDetail>
<AttributeDetail>
<Name>COLNAME2</Name>
...
</AttributeDetail>
...
</ObjectAttributes>
<Record RowID="00001">
<Attributes>
<Attr>
<AttrName>COLNAME1</AttrName>
<AttrValue>VALUE</AttrValue>
</Attr>
<Attr>
<AttrName>COLNAME2</AttrName>
<AttrValue>VALUE</AttrValue>
</Attr>
...
</Attributes>
</Record>
<Record RowID="00002">
<Attributes>
<Attr>
<AttrName>COLNAME1</AttrName>
<AttrValue>VALUE</AttrValue>
</Attr>
<Attr>
<AttrName>COLNAME2</AttrName>
<AttrValue>VALUE</AttrValue>
</Attr>
...
</Attributes>
</Record>
...
</Table>
我写了下面的XSLT创建INSERT命令:
<xsl:for-each select="/Table/Record">
<xsl:variable name="ThisRecord" select="."/>
<Command>
INSERT INTO <xsl:value-of select="../ObjectName"/>
(ROWID,
<xsl:for-each select="../ObjectAttributes/AttributeDetail">
<xsl:value-of select="Name"/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>)
VALUES
(<xsl:value-of select="@RowID"/>,
<xsl:for-each select="../ObjectAttributes/AttributeDetail">
<xsl:text>'</xsl:text><xsl:value-of select="$ThisRecord/Attributes/Attr/AttrValue[../AttrName='COLNAME1']"/><xsl:text>'</xsl:text>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>)
</Command>
</xsl:for-each>
这个工程除了事实巨大我正在努力想出如何与将导致从值替换值AttrName =“”:
<xsl:value-of select="Name"/>
我试图用一个变量替换,但THI s没有工作:
<xsl:variable name="ThisAttr" select="Name"/>
<xsl:value-of select="$ThisRecord/Attributes/Attr/AttrValue[../AttrName='$ThisAttr']"/>
如何动态地填写此属性?
谢谢!
你就不能换每遍/录音/属性两次,一次为属性的名称,以及第二次为属性值? ObjectAttributes中的名称是否与Attributes部分中的AttrName相同? – dash 2012-01-06 21:04:20
这会工作,但我希望将每个/ Record/Attributes匹配到ObjectAttributes,以确保记录中的属性存在于对象中。 – Paul 2012-01-06 21:07:30
够公平的!下面的XPath表达式应该适用于你:$ ThisRecord/Attributes/Attr [AttrName = $ ThisAttr]/AttrValue - 你只需要你的AttrName检查错误的地方,因为你想说的是找到我的attr与一个孩子attrName = $ ThisAttr名称,然后选择AttrValue。 – dash 2012-01-06 21:10:53