1

我正在尝试将 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 个。谢谢。

4

1 回答 1

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>
于 2013-04-20T21:14:03.340 回答