-1

我想更改此 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 实现结果吗?非常感谢您对这个问题的帮助。

4

1 回答 1

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>
于 2013-05-27T04:32:53.437 回答