nodename的xml组元素,后缀更改

时间:2013-04-20 17:27:45

标签: xml transform nodes

我正在尝试将XML转换为特定的输出XML。但我无法按照N对一组字段的变化进行分组。

    <Balances>
        <Balances_Line_Item0>
          <Page>
            <Field>EmpID</Field>
            <Letter>45</Letter>
            <Letter>45</Letter>
            <Letter>45</Letter>
          </Page>
        </Balances_Line_Item0>
        <Balances_Line_Item0>
          <Page>
            <Field>Name</Field>
            <Letter>53</Letter>
            <Letter>56</Letter>
            <Letter>76</Letter>
             <Letter>53</Letter>
            <Letter>56</Letter>
            <Letter>76</Letter>
         </Page>
        </Balances_Line_Item0>
        <Balances_Line_Item0>
          <Page>
            <Field>Department</Field>
            <Letter>45</Letter>
            <Letter>45</Letter>
            <Letter>45</Letter>
          </Page>
        </Balances_Line_Item0>
        <Balances_Line_Item1>
          <Page>
            <Field>EmpID</Field>
            <Letter>45</Letter>
            <Letter>45</Letter>
            <Letter>45</Letter>
          </Page>
        </Balances_Line_Item1>
        <Balances_Line_Item1>
          <Page>
            <Field>Name</Field>
            <Letter>53</Letter>
            <Letter>56</Letter>
            <Letter>76</Letter>
             <Letter>53</Letter>
            <Letter>56</Letter>
            <Letter>76</Letter>
         </Page>
        </Balances_Line_Item1>
        <Balances_Line_Item1>
          <Page>
            <Field>Department</Field>
            <Letter>45</Letter>
            <Letter>45</Letter>
            <Letter>45</Letter>
          </Page>
        </Balances_Line_Item1>
        <Balances_Line_Item2>
          <Page>
            <Field>EmpID</Field>
            <Letter>45</Letter>
            <Letter>45</Letter>
            <Letter>45</Letter>
          </Page>
        </Balances_Line_Item2>
        <Balances_Line_Item2>
          <Page>
            <Field>Name</Field>
            <Letter>53</Letter>
            <Letter>56</Letter>
            <Letter>76</Letter>
             <Letter>53</Letter>
            <Letter>56</Letter>
            <Letter>76</Letter>
         </Page>
        </Balances_Line_Item2>
        <Balances_Line_Item2>
          <Page>
            <Field>Department</Field>
            <Letter>45</Letter>
            <Letter>45</Letter>
            <Letter>45</Letter>
          </Page>
        </Balances_Line_Item2>
    </Balances>

我的转换后的XML应该是:

<Balances>
<Balances_Line_Item0>
<EmpID>
        <Letter>45</Letter>
        <Letter>45</Letter>
        <Letter>45</Letter>
</EmpID>
<Name>
        <Letter>53</Letter>
        <Letter>56</Letter>
        <Letter>76</Letter>
         <Letter>53</Letter>
        <Letter>56</Letter>
        <Letter>76</Letter>
</Name>
<Dept>
        <Letter>45</Letter>
        <Letter>45</Letter>
        <Letter>45</Letter>
</Dept>
</Balances_Line_Item0>
<Balances_Line_Item1>
<EmpID>
        <Letter>45</Letter>
        <Letter>45</Letter>
        <Letter>45</Letter>
</EmpID>
<Name>
        <Letter>53</Letter>
        <Letter>56</Letter>
        <Letter>76</Letter>
         <Letter>53</Letter>
        <Letter>56</Letter>
        <Letter>76</Letter>
</Name>
<Dept>
        <Letter>45</Letter>
        <Letter>45</Letter>
        <Letter>45</Letter>
</Dept>
</Balances_Line_Item1>
<Balances_Line_Item2>
<EmpID>
        <Letter>45</Letter>
        <Letter>45</Letter>
        <Letter>45</Letter>
</EmpID>
<Name>
        <Letter>53</Letter>
        <Letter>56</Letter>
        <Letter>76</Letter>
         <Letter>53</Letter>
        <Letter>56</Letter>
        <Letter>76</Letter>
</Name>
<Dept>
        <Letter>45</Letter>
        <Letter>45</Letter>
        <Letter>45</Letter>
</Dept>
</Balances_Line_Item2>
</Balances>

如何将输入xml转换为XML以上?请建议。


我在生成的xml上再应用了一次转换:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <xsl:copy>
      <xsl:element name="Balances">
        <xsl:for-each select="Balances/node()[position() mod 5 = 1]">
          <xsl:element name="LineItem">
            <xsl:for-each select=". | following-sibling::node()[not(position() > 4)]">
              <xsl:copy-of select="."/>
            </xsl:for-each>
          </xsl:element>
        </xsl:for-each>
      </xsl:element>
          </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但我的xslt非常基本,我不能继续使用这样的XSL进行更复杂的转换。有人可以建议调整和更好的xslt,可能是一个XSLT,而不是2。 谢谢。

1 个答案:

答案 0 :(得分:0)

以下XSLT 2.0样式表完成了这项工作:

<?xml version="1.0" encoding="UTF-8"?>

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

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

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

  <xsl:template match="Balances">
    <xsl:copy>
      <xsl:for-each-group select="*" group-adjacent="name()">
        <xsl:copy>
          <xsl:apply-templates select="current-group()/*"/>
        </xsl:copy>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Page">
    <xsl:apply-templates select="Field"/>
  </xsl:template>

  <xsl:template match="Field">
    <xsl:element name="{.}">
      <xsl:apply-templates select="following-sibling::*"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>