我想创建一个XSLT来将其转换为XML ... 例如,每个人将依次有2个JobInfo标签,然后是2个EmpInfo。我想将第一个JobInfo与第一个EmpInfo合并,第二个JobInfo与第二个EmpInfo合并。并将它们转换为EmpJobInfo。
<soap:Envelope xmlns:soap="schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<App>
<person>
<answerSet>
<answers>
<entry>
<key>monthlyIncome</key>
<value>1200</value>
</entry>
</answers>
<key>JobInfo</key>
</answerSet>
<answerSet>
<answers>
<entry>
<key>monthlyIncome</key>
<value>800</value>
</entry>
</answers>
<key>JobInfo</key>
</answerSet>
<answerSet>
<answers>
<entry>
<key>EmpName</key>
<value>Walmart</value>
</entry>
</answers>
<key>EmpInfo</key>
</answerSet>
<answerSet>
<answers>
<entry>
<key>EmpName</key>
<value>Target</value>
</entry>
</answers>
<key>EmpInfo</key>
</answerSet>
</person>
</App>
</soap:Body>
</soap:Envelope>
将结果显示为
<answerSet>
<answers>
<entry>
<key>EmpName</key>
<value>Walmart</value>
</entry>
</answers>
<answers>
<entry>
<key>monthlyIncome</key>
<value>1200</value>
</entry>
</answers>
<key>EmpJobInfo</key>
</answerSet>
<answerSet>
<answers>
<entry>
<key>EmpName</key>
<value>Target</value>
</entry>
</answers>
<answers>
<entry>
<key>monthlyIncome</key>
<value>800</value>
</entry>
</answers>
<key>EmpJobInfo</key>
</answerSet>
答案 0 :(得分:1)
假设answerSet节点始终以每人4个节点的组出现,如示例所示:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="*">
<App>
<xsl:for-each select="//person">
<xsl:variable name ="person" select="." />
<person>
<xsl:for-each select="answerSet[key='EmpInfo']">
<xsl:variable name ="pos" select="position()" />
<answerSet>
<xsl:copy-of select="answers" />
<xsl:copy-of select="$person/answerSet[key='JobInfo'][$pos]/answers" />
<key>EmpJobInfo</key>
</answerSet>
</xsl:for-each>
</person>
</xsl:for-each>
</App>
</xsl:template>
</xsl:stylesheet>
提供此输出:
<App>
<person>
<answerSet>
<answers>
<entry>
<key>EmpName</key>
<value>Walmart</value>
</entry>
</answers>
<answers>
<entry>
<key>monthlyIncome</key>
<value>1200</value>
</entry>
</answers>
<key>EmpJobInfo</key>
</answerSet>
<answerSet>
<answers>
<entry>
<key>EmpName</key>
<value>Target</value>
</entry>
</answers>
<answers>
<entry>
<key>monthlyIncome</key>
<value>800</value>
</entry>
</answers>
<key>EmpJobInfo</key>
</answerSet>
</person>
</App>
答案 1 :(得分:0)
每个人将依次有2个JobInfo标签,然后是2个 EmpInfo。我想将第一个JobInfo与第一个EmpInfo和第二个合并 JobInfo与第二个EmpInfo。并将它们转换为EmpJobInfo。
你可以这样:
<强> XSLT1.0 强>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soap">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/soap:Envelope">
<root>
<xsl:apply-templates select="soap:Body/App/person"/>
</root>
</xsl:template>
<xsl:template match="person">
<xsl:variable name="JobInfo" select="answerSet[key='JobInfo']/answers" />
<xsl:variable name="EmpInfo" select="answerSet[key='EmpInfo']/answers" />
<answerSet>
<xsl:apply-templates select="$EmpInfo[1]"/>
<xsl:apply-templates select="$JobInfo[1]"/>
<key>EmpJobInfo</key>
</answerSet>
<answerSet>
<xsl:apply-templates select="$EmpInfo[2]"/>
<xsl:apply-templates select="$JobInfo[2]"/>
<key>EmpJobInfo</key>
</answerSet>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请注意输出中添加的根元素。