我有以下XML结构(文件#1),我需要编写XSL文件,以便将其转换为不同的结构(文件#2)。 目的:需要导入到DB。
在文件#1中可能有多个对象。 文件#1中的每个对象将基于XML文件#2转换到我的表中的4条记录。
你能帮我解决XSL语法吗?
感谢您的帮助。
文件#1:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root>
<Object>
<location>[X12][Y20]</location>
<serial>1224719</serial>
<side_left>
<color>black</color>
<point>
<name>1</name>
<value>2</value>
</point>
<point>
<name>2</name>
<value>3</value>
</point>
<total>5</total>
</side_left>
<side_right>
<color>yellow</color>
<point>
<name>1</name>
<value>5</value>
</point>
<point>
<name>2</name>
<value>6</value>
</point>
<total>11</total>
</side_right>
</Object>
</Root>
档案#2
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root>
<MyTable>
<serial>1224719</serial>
<location>[X12][Y20]</location>
<color>black</color>
<name>1</name>
<value>2</value>
</MyTable>
<MyTable>
<serial>1224719</serial>
<location>[X12][Y20]</location>
<color>black</color>
<name>2</name>
<value>3</value>
</MyTable>
<MyTable>
<serial>1224719</serial>
<location>[X12][Y20]</location>
<color>yellow</color>
<name>1</name>
<value>5</value>
</MyTable>
<MyTable>
<serial>1224719</serial>
<location>[X12][Y20]</location>
<color>yellow</color>
<name>2</name>
<value>6</value>
</MyTable>
</Root>
答案 0 :(得分:0)
试试这个:
<xsl:template match="Root">
<xsl:copy>
<xsl:for-each select="//point">
<MyTable>
<xsl:copy-of select="ancestor::Object/serial"/>
<xsl:copy-of select="ancestor::Object/location"/>
<xsl:copy-of select="../color"/>
<xsl:copy-of select="*"/>
</MyTable>
</xsl:for-each>
</xsl:copy>
</xsl:template>
请参阅http://xsltransform.net/aiwQ3u
处的转化对于您的其他查询,请尝试此
<xsl:template match="Root">
<xsl:copy>
<xsl:for-each select="//point">
<MyTable>
<xsl:copy-of select="ancestor::Object/serial"/>
<locationX><xsl:value-of select="substring-before(substring-after(ancestor::Object/location, 'X'), ']')"/></locationX>
<locationY><xsl:value-of select="substring-before(substring-after(ancestor::Object/location, 'Y'), ']')"/></locationY>
<xsl:copy-of select="../color"/>
<xsl:copy-of select="*"/>
</MyTable>
</xsl:for-each>
</xsl:copy>
</xsl:template>