XSLT转换:根据条件将多个标签合并为单个标签

时间:2016-12-20 16:44:27

标签: xml xslt transformation

我在一个节点中有多个标签,我不想根据条件将它们组合成一个标签。

每个人都会依次拥有多个JobInfo标签,然后是多个EmpInfo。我想将第一个JobInfo与第一个EmpInfo合并,第二个JobInfo与第二个EmpInfo合并,如果'应用'指示符是'Y'并将它们转换为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>
    <answers>
        <entry>
            <key>Applying</key>
            <value>Y</value>
        </entry>
    </answers>
    <key>JobInfo</key>
 </answerSet>
 <answerSet>
    <answers>
        <entry>
            <key>monthlyIncome</key>
            <value>800</value>
        </entry>
    </answers>
    <answers>
        <entry>
            <key>Applying</key>
            <value>Y</value>
        </entry>
    </answers>
    <key>JobInfo</key>
</answerSet>
 <answerSet>
    <answers>
        <entry>
            <key>monthlyIncome</key>
            <value>450</value>
        </entry>
    </answers>
    <answers>
        <entry>
            <key>Applying</key>
            <value>N</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>

我目前正在使用下面的XSLT,它将获取每个标签的位置并与其他标签结合使用。但它没有检查'申请'是否为'Y'。

<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 :(得分:1)

如果您只想处理条件所适用的元素,那么我认为将模板更改为

CTE

应该实现这个条件。