使用apply-template更改多个节点

时间:2013-05-24 14:20:00

标签: xml templates xslt

我想更改此xml格式

从这个xml节点:

<Loan>
  <accountNumber>111111</accountNumber>
</Loan>
<Loan>
  <accountNumber>222222</accountNumber>
</Loan>

到这个xml节点:

<HBItems>
  <HBItem>
    <Properties>
     <Property>
          <Code>LOAN_IDT</Code>
          <Value>111111</Value>
     </Property>
     <Property>
           <Code>LOAN_NUMBER</Code>
           <Value>111111</Value>
     </Property>
    </Properties>
  </HBItem>
  <HBItem>
   <Properties>
    <Property>
          <Code>LOAN_IDT</Code>
          <Value>222222</Value>
    </Property>
    <Property>
           <Code>LOAN_NUMBER</Code>
           <Value>222222</Value>
    </Property>
   </Properties>
  </HBItem>
</HBItems>

你能帮我用apply-template来实现结果吗? 非常感谢这个问题的帮助。

1 个答案:

答案 0 :(得分:0)

您的XML无效。正确的XML必须具有根元素,例如:

更改了XML:

<x>
<Loan>
  <accountNumber>111111</accountNumber>
</Loan>
<Loan>
  <accountNumber>222222</accountNumber>
</Loan>
</x>

<强> XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <HBItems>
      <xsl:apply-templates/>
    </HBItems>
  </xsl:template>

  <xsl:template match="Loan">
    <HBItem>
      <Properties>
        <Property>
          <Code>
            <xsl:text>LOAN_IDT</xsl:text>
          </Code>
          <Value>
            <xsl:value-of select="accountNumber"/>
          </Value>
        </Property>
        <Property>
          <Code>
            <xsl:text>LOAN_NUMBER</xsl:text>
          </Code>
          <Value>
            <xsl:value-of select="accountNumber"/>
          </Value>
        </Property>
      </Properties>
    </HBItem>
  </xsl:template>
</xsl:stylesheet>

<强>输出:

<HBItems>
   <HBItem>
      <Properties>
         <Property>
            <Code>LOAN_IDT</Code>
            <Value>111111</Value>
         </Property>
         <Property>
            <Code>LOAN_NUMBER</Code>
            <Value>111111</Value>
         </Property>
      </Properties>
   </HBItem>
   <HBItem>
      <Properties>
         <Property>
            <Code>LOAN_IDT</Code>
            <Value>222222</Value>
         </Property>
         <Property>
            <Code>LOAN_NUMBER</Code>
            <Value>222222</Value>
         </Property>
      </Properties>
   </HBItem>
</HBItems>