我有以下XML结构(文件#1),我需要编写XSL文件,以便将其转换为不同的结构(文件#2)。用途:需要导入到数据库中。
在文件#1中可能有多个对象。file#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>发布于 2018-02-01 16:26:57
试试这个:
<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>https://stackoverflow.com/questions/48557004
复制相似问题