使用XSLT

时间:2019-03-05 16:31:31

标签: xslt xslt-2.0

我有一个类似下面的XML

<Screenings>
    <Screening type="EFG" desc="Financial Report">
        <ScreeningStatus>
            <OrderStatus>Complete</OrderStatus>
            <ResultStatus>Review</ResultStatus>
        </ScreeningStatus>
    </Screening>
    <Screening type="EFG" desc="Financial Report">
        <ScreeningStatus>
            <OrderStatus>Complete</OrderStatus>
            <ResultStatus>Fail</ResultStatus>
        </ScreeningStatus>
    </Screening>ngStatus>
    </Screening>
    <Screening subtype="CARG" type="ABCD"  desc="registry search">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
    <Screening subtype="CARG" type="ABCD"  desc="registry search">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
    <Screening subtype="KARG" type="ABCD">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
    <Screening subtype="KARG" type="ABCD"  desc="registry search">
        <ScreeningStatus>
            <OrderStatus>InProgress</OrderStatus>
        </ScreeningStatus>
    </Screening>
</Screenings>

我需要如下所示的字符串(获取唯一的typesubtype属性)

EFG-|ABCD-CARG|ABCD-KARG

然后通过管道将其分割|并循环遍历。 在循环内部,我需要按连字符(-)拆分类型和子类型 类型和子类型需要两个variables-如下所示

for (split by pipe | val : array) {
       split by hyphen and create two variable for type and subtype
       (ABCD-KARG)
       var type=ABCD
       var subtype=KARG
       // I have some business logic to do here
}

我尝试过-

<xsl:variable name="typeSubTypeArray" select="string-join(./ns0:Screening/@type, ',')"/>

但是我无法为其添加子类型值并创建唯一的组合值字符串

如果仅输入而不输入子类型,则使用此命令我将获得唯一值-

<xsl:for-each select="distinct-values(./ns0:Screening/@type))">

但是如何使用类型/子类型组合获得唯一值。 我需要属性值。

2 个答案:

答案 0 :(得分:2)

Are you over-complicating the process here? I think you can just use xsl:for-each-group to get the values you need, without the need for building up a string then splitting it.

<xsl:for-each-group select="Screening" group-by="@type">
  <xsl:for-each-group select="current-group()" group-by="string(@subtype)">
    <xsl:value-of select="concat('Processing ', @type, ' - ', @subtype, '&#10;')" />
  </xsl:for-each-group>
</xsl:for-each-group>

Note that, if you really did want to create your pipe-delimited string, you would do this...

<xsl:variable name="distinct" select="string-join(distinct-values(Screening/concat(@type, '-', @subtype)), '|')" />

答案 1 :(得分:1)

如果我正确理解了您的问题,则可以:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/Screenings">
    <xsl:variable name="groups">
        <xsl:for-each-group select="Screening" group-by="concat(@type, '-', @subtype)">
            <group key="{current-grouping-key()}"/>
        </xsl:for-each-group>
    </xsl:variable>
    <xsl:value-of select="$groups/group/@key" separator="|"/>
</xsl:template>

</xsl:stylesheet>

或者只是:

<xsl:template match="/Screenings">
    <xsl:value-of select="distinct-values(Screening/concat(@type, '-', @subtype))" separator="|"/>
</xsl:template>