源XML:
<record>
<protein>AAA</protein>
<reference>1234</reference>
<reference>679</reference>
</record>
<record>
<protein>BBB</protein>
<reference>9876</reference>
</record>
<record>
<protein>CCC</protein>
<reference>9876</reference>
<reference>14846</reference>
<reference>982</reference>
</record>
我是使用XLST的新手,但无法找到问题的解决方案。我需要专门创建一个XML文件,其中记录中的数据根据列表中的值分成一个或多个新记录。请注意,列表(参考)中的值数量变化很大。
所需的XML:
<record>
<protein>AAA</protein>
<reference>1234</reference>
</record>
<record>
<protein>AAA</protein>
<reference>679</reference>
</record>
<record>
<protein>BBB</protein>
<reference>9876</reference>
</record>
<record>
<protein>CCC</protein>
<reference>9876</reference>
</record>
<record>
<protein>CCC</protein>
<reference>14846</reference>
</record>
<record>
<protein>CCC</protein>
<reference>982</reference>
</record>
非常感谢任何帮助。
答案 0 :(得分:1)
我必须调整您的示例XML输入文件。
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record>
<protein>AAA</protein>
<reference>1234</reference>
<reference>679</reference>
</record>
<record>
<protein>BBB</protein>
<reference>9876</reference>
</record>
<record>
<protein>CCC</protein>
<reference>9876</reference>
<reference>14846</reference>
<reference>982</reference>
</record>
</records>
然后,XSL本身可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:output exclude-result-prefixes="xsl xs" indent="yes"/>
<xsl:template match="/records/record">
<xsl:for-each select="reference">
<xsl:element name="record">
<xsl:element name="protein">
<xsl:value-of select="../protein/text()"/>
</xsl:element>
<xsl:element name="reference">
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>