【问题标题】:Required Transformation xslt code所需的转换 xslt 代码
【发布时间】:2023-03-29 22:51:01
【问题描述】:

我有以下输入

<?xml version="1.0" encoding="UTF-8"?>
    <Input>
     <Userlist>
       <name>ABC</name>
       <division>1</division>
    <method>online</method>
    <subject>M</subject>
    <fee>10k</fee>
    </Userlist>
    <Userlist>
       <name>BCD</name>
       <division>2</division>
    <method>Distance</method>
    <fee>5k</fee>
    <subject>C</subject>
    </Userlist>
    <Userlist>
       <name>CDF</name>
       <division>2</division>
    <method>Direct</method>
    <fee>15k</fee>
    <subject>P</subject>
    </Userlist>
    <Userlist>
       <name>FGH</name>
       <division>55</division>
    <method>Direct</method>
    <fee>25k</fee>
    <subject>E</subject>
    </Userlist>
    <Userlist>
       <name>HKM</name>
       <division>55</division>
    <method>Direct</method>
    <fee>40k</fee>
    <subject>H</subject>
    </Userlist>
    </Input>

下面是预期的输出:要求是,基于方法值(那些是预定义的值)循环通过它们,如果出现更多,我们需要将相对值放在过滤器下。如果我们有相同的部门,内部过滤器也属于一个细节元素,如下所示。我什至不能放代码,因为它没有给出更近的地方。感谢您的帮助。

<?xml version="1.0" encoding="UTF-8"?>
<Output>
 <AllUserslist>
  <User>
  <Methodtype>online</Methodtype>
  <Filters>
<Filter>
<Section>1</Section>
<Details>
<Detail>
<Studentname>ABC</Studentname>
<Fee>10k</Fee>
<subject>M</subject>
</Detail>
</Details>
</Filter>
</Filters>
</User>
 <User>
  <Methodtype>Distance</Methodtype>
  <Filters>
<Filter>
<Section>2</Section>
<Details>
<Detail>
<Studentname>BCD</Studentname>
<Fee>5k</Fee>
<subject>C</subject>
</Detail>
</Details>
</Filter>
</Filters>
</User>
 <User>
 <Methodtype>Direct</Methodtype>
<Filters>
<Filter>
<Section>2</Section>
<Details>
<Detail>
<Studentname>CDF</Studentname>
<Fee>15K</Fee>
<subject>P</subject>
</Detail>
</Details>
</Filter>
<Filter>
<Section>55</Section>
<Details>
<Detail>
<Studentname>FGH</Studentname>
<Fee>25K</Fee>
<subject>E</subject>
</Detail>
<Detail>
<Studentname>HKM</Studentname>
<Fee>40K</Fee>
<subject>H</subject>
</Detail>
</Details>
</Filter>
</Filters>
 </User>
 </AllUserslist>
 </Output>

【问题讨论】:

  • 您的预期输出格式不正确。还更新您当前的 XSLT
  • 输出格式正确。我更新了。
  • datapower 是否支持 XSLT 2.0 和 for-each-group group-by
  • 不,它没有。 DataPower 是带有一些奇怪变化的 XSLT 1.0。没有for-each-group 可用。

标签: xml xslt ibm-datapower


【解决方案1】:

试试这个方法?

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"/>

<xsl:key name="group-method" match="Userlist" use="method" />
<xsl:key name="group-division" match="Userlist" use="concat(method, '|', division)" />

<xsl:template match="/Input">
    <Output>
        <AllUserslist>
            <xsl:for-each select="Userlist[generate-id() = generate-id(key('group-method', method)[1])]">
                <User>
                    <Methodtype><xsl:value-of select="method"/></Methodtype> 
                    <Filters>
                        <xsl:for-each select="key('group-method', method)[generate-id() = generate-id(key('group-division', concat(method, '|', division))[1])]">
                            <Filter>
                                <Section><xsl:value-of select="current()/division"/></Section>
                                <Details>
                                    <xsl:for-each select="key('group-division', concat(method, '|', division))">
                                        <Detail>
                                            <Studentname><xsl:value-of select="name"/></Studentname>
                                            <Fee><xsl:value-of select="fee"/></Fee>
                                            <subject><xsl:value-of select="subject"/></subject>
                                        </Detail>
                                        </xsl:for-each>
                                </Details>
                            </Filter>       
                        </xsl:for-each> 
                    </Filters>   
                </User> 
            </xsl:for-each>    
        </AllUserslist> 
    </Output>  
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    假设您可以使用 XSLT 2.0,您可以使用两个嵌套的 for-each-group group-by 来解决它:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    
        <xsl:output indent="yes"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"></xsl:apply-templates>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Input">
            <Output>
                <AllUserslist>
                    <xsl:for-each-group select="Userlist" group-by="method">
                        <User>
                            <Methodtype>
                                <xsl:value-of select="current-grouping-key()"/>
                            </Methodtype>
                            <Filters>
                                <xsl:for-each-group select="current-group()" group-by="division">
                                    <Filter>
                                        <Section>
                                            <xsl:value-of select="current-grouping-key()"/>
                                        </Section>
                                        <Details>
                                            <xsl:apply-templates select="current-group()"/>
                                        </Details>
                                    </Filter>
                                </xsl:for-each-group>
                            </Filters>
                        </User>
                    </xsl:for-each-group>
                </AllUserslist>
            </Output>
        </xsl:template>
    
        <xsl:template match="Userlist">
            <Detail>
                <xsl:apply-templates select="* except (method, division)"/>
            </Detail>
        </xsl:template>
    
        <xsl:template match="name">
            <Studentname>
                <xsl:value-of select="."/>
            </Studentname>
        </xsl:template>
    
        <xsl:template match="fee">
            <Fee>
                <xsl:value-of select="."/>
            </Fee>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • datapower 不支持 xslt 2.0。使用 xslt 1.0 的任何替代代码
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 2017-12-30
    • 2012-12-22
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多