简体   繁体   English

Munechian 分组和排序 XSLt 1.0

[英]Munechian grouping and sorting XSLt 1.0

I have a requirement where i need to do sorting and grouping.我有一个要求,我需要在哪里进行排序和分组。 I have worked my XSLT with sorting but its not giving me the desired output,我已经对 XSLT 进行了排序,但它没有给我所需的输出,

Input XML:输入 XML:

<ns0:Root xmlns:ns0="http://TestXSLT1._0.Output">
  <SeqNo>1</SeqNo>
  <FileName>Test</FileName>
  <DestinationLocation>Miami</DestinationLocation>
  <DestinationName>State</DestinationName>
  <Detail>
    <ItemName>Item1</ItemName>
    <Rate>100</Rate>
  </Detail>
  <Detail>
    <ItemName>Item2</ItemName>
    <Rate>200</Rate>
  </Detail>
  <Detail>
    <ItemName>Item3</ItemName>
    <Rate>300</Rate>
  </Detail>
  <Detail>
    <ItemName>Item1</ItemName>
    <Quantity>1</Quantity>
  </Detail>
  <Detail>
    <ItemName>Item2</ItemName>
    <Quantity>2</Quantity>
  </Detail>
</ns0:Root>

Desired OutPut: Which I expect期望输出:我期望的

 <ns0:Root xmlns:ns0="http://TestXSLT1._0.Output">
        <SeqNo>1</SeqNo>
        <FileName>Test</FileName>
        <DestinationLocation>Miami</DestinationLocation>
        <DestinationName>State</DestinationName>
        <Detail>
            <ItemName>Item1</ItemName>
            <Quantity>1</Quantity>
            <Rate>100</Rate>
        </Detail>
        <Detail>
            <ItemName>Item2</ItemName>
            <Quantity>2</Quantity>
            <Rate>200</Rate>
        </Detail>
        <Detail>
            <ItemName>Item3</ItemName>
            <Rate>3</Rate>
        </Detail>
    </ns0:Root>

XSLT I have worked:我工作过的 XSLT:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var" version="1.0" xmlns:ns0="http://TestXSLT1._0.Output">
  <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
  <xsl:key name="group" match="Detail" use="ItemName"/>
  <xsl:key name="group1" match="Detail" use="Rate"/>
  <xsl:template match="/">
    <xsl:apply-templates select="/ns0:Root" />
  </xsl:template>
  <xsl:template match="/ns0:Root">
    <ns0:Root>
      <xsl:if test="SeqNo">
        <SeqNo>
          <xsl:value-of select="SeqNo/text()" />
        </SeqNo>
      </xsl:if>
      <xsl:if test="FileName">
        <FileName>
          <xsl:value-of select="FileName/text()" />
        </FileName>
      </xsl:if>
      <xsl:if test="DestinationLocation">
        <DestinationLocation>
          <xsl:value-of select="DestinationLocation/text()" />
        </DestinationLocation>
      </xsl:if>
      <xsl:if test="DestinationName">
        <DestinationName>
          <xsl:value-of select="DestinationName/text()" />
        </DestinationName>
      </xsl:if>
      <xsl:for-each select="Detail[generate-id(.)=generate-id(key('group',ItemName))]">
        <Detail>
          <xsl:if test="ItemName">
            <ItemName>
              <xsl:value-of select="ItemName/text()" />
            </ItemName>
          </xsl:if>
          <xsl:if test="Quantity">
            <Quantity>
              <xsl:value-of select="Quantity/text()" />
            </Quantity>
          </xsl:if>
          <xsl:if test="Rate">
            <Rate>
              <xsl:value-of select="sum(key('group1', Rate)/Rate)" />
            </Rate>
          </xsl:if>
        </Detail>
      </xsl:for-each>
    </ns0:Root>
  </xsl:template>
</xsl:stylesheet>

The XSLT I have made doesn't give me the desired output.我制作的 XSLT 没有给我想要的输出。 This should be done in XSLT 1.0 Appreciate for the help这应该在 XSLT 1.0 中完成 感谢您的帮助

AFAICT, you just want to group the Detail nodes by ItemName . AFAICT,您只想按ItemNameDetail节点进行分组。 This could be done simply using:这可以简单地使用:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

<xsl:key name="detail-by-item" match="Detail" use="ItemName"/>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:copy-of select="*[not(self::Detail)]"/>
        <xsl:for-each select="Detail[generate-id()=generate-id(key('detail-by-item', ItemName))]">
            <xsl:copy>
                <xsl:copy-of select="ItemName"/>
                <xsl:copy-of select="key('detail-by-item', ItemName)/*[not(self::ItemName)]"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM