我试图通过将2个<element>
节点分组为一个<item>
来将XML转换为特定的XML格式。然后为每个<element>
重复格式。如果没有日期可以填补空白节点,我想要。示例将在所需结果的最底端。请注意,最后三个节点为空。
我的XML
<feed version="2.4">
<list>
<element>
<field name="01">text1</field>
<field name="02">text2</field>
<field name="03">text3</field>
<field name="04">text4</field>
</element>
<element>
<field name="01">text5</field>
<field name="02">text6</field>
<field name="03">text7</field>
<field name="04">text8</field>
</element>
<element>
<field name="01">text9</field>
<field name="02">text10</field>
<field name="03">text11</field>
<field name="04">text12</field>
</element>
<element>
<field name="01">text13</field>
<field name="02">text14</field>
<field name="03">text15</field>
<field name="04">text16</field>
</element>
<element>
<field name="01">text17</field>
<field name="02">text18</field>
<field name="03">text19</field>
<field name="04">text20</field>
</element>
</list>
所需结果
<item>
<title1>text1</title1>
<desc1>text2</desc1>
<opt1>text3</opt1>
<title2>text5</title2>
<desc2>text6</desc2>
<opt2>text7</opt2>
</item>
<item>
<title1>text9</title1>
<desc1>text10</desc1>
<opt1>text11</opt1>
<title2>text13</title2>
<desc2>text14</desc2>
<opt2>text15</opt2>
</item>
<item>
<title1>text17</title1>
<desc1>text18</desc1>
<opt1>text19</opt1>
<title2></title2>
<desc2></desc2>
<opt2></opt2>
</item>
正在使用的XSLT。我很想获得title2,desc2,opt2的下一个值。
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="feed/list/element">
<xsl:if test="not(number()=0) and position() mod 2 = 0">
<Item>
<title1><xsl:value-of select="field[@name='01']"/></title1>
<desc1><xsl:value-of select="field[@name='02']"/></desc1>
<opt1><xsl:value-of select="field[@name='03']"/></opt1>
<title2><xsl:value-of select="field[@name='01']"/></title2>
<desc2><xsl:value-of select="field[@name='02']"/></desc2>
<opt2><xsl:value-of select="field[@name='03']"/></opt2>
</Item>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:2)
怎么样:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/feed">
<output>
<xsl:for-each select="list/element[position() mod 2 = 1]">
<item>
<title1>
<xsl:value-of select="field[@name='01']"/>
</title1>
<desc1>
<xsl:value-of select="field[@name='02']"/>
</desc1>
<opt1>
<xsl:value-of select="field[@name='03']"/>
</opt1>
<xsl:variable name="next" select="following-sibling::element[1]" />
<title2>
<xsl:value-of select="$next/field[@name='01']"/>
</title2>
<desc2>
<xsl:value-of select="$next/field[@name='02']"/>
</desc2>
<opt2>
<xsl:value-of select="$next/field[@name='03']"/>
</opt2>
</item>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>