XSLT:Muenchian多个分组困境

时间:2017-09-22 22:43:56

标签: xslt xslt-1.0 xslt-grouping

我有一个spa服务列表,我需要通过XSLT 1.0通过分组将它们分解出来。我可以通过分组来突破这些名称,但是在我的小组的连续性中挣扎。如果您运行我的脚本,您将看到我的第一个Muenchian组工作正常,明确地放置客户名称,但不确定为什么我的第二个连接密钥不起作用(日期)。谁能指出我正确的方向?我对XSLT不是很熟练,但我喜欢尝试!

简化输入



<SQLXMLExport>
<Rows>
<Row>
<Field alias="BOOK_FOR">Phone, Angie</Field>
<Field alias="START_DATE">Tuesday, December 5, 2017</Field>
<Field alias="ITEM_NAME">Sea Soak</Field>
</Row>
<Row>
<Field alias="BOOK_FOR">Phone, Angie</Field>
<Field alias="START_DATE">Tuesday, December 5, 2017</Field>
<Field alias="ITEM_NAME">Ocean Package - Weekday</Field>
</Row>
<Row>
<Field alias="BOOK_FOR">McNotes, Sue</Field>
<Field alias="START_DATE">Tuesday, December 5, 2017</Field>
<Field alias="ITEM_NAME">Experience Package - Weekday</Field>
</Row>
<Row>
<Field alias="BOOK_FOR">McNotes, Sue</Field>
<Field alias="START_DATE">Tuesday, December 5, 2017</Field>
<Field alias="ITEM_NAME">Morning Soak - Weekday</Field>
</Row>
<Row>
<Field alias="BOOK_FOR">McNotes, Sue</Field>
<Field alias="START_DATE">Wednesday, December 6, 2017</Field>
<Field alias="ITEM_NAME">Test Scrub - Weekday</Field>
</Row>
</Rows>
</SQLXMLExport>
&#13;
&#13;
&#13;

我的XSLT 1.0

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:key name="guests" match="Rows/Row" use="./Field[@alias='BOOK_FOR']"/>
    <xsl:key name="guests-date" match="Rows/Row" use="concat( ./Field[@alias='BOOK_FOR'] , '|' , ./Field[@alias='START_DATE'] )"/>
        
    <xsl:template match="Rows">
        <!-- Now, we need to iterate on the guest key -->
        <xsl:for-each select="Row[count(. | key('guests', ./Field[@alias='BOOK_FOR'])[1]) = 1]">
            <!-- Sort by the guest -->
            <xsl:sort select="./Field[@alias='BOOK_FOR']" />
            <br/><xsl:value-of select="./Field[@alias='BOOK_FOR']" />
            <hr />

            <!-- Now loop on the items of this guest, we get them from the key we defined -->
            <xsl:for-each select="Row[count(. | key( 'guests-date' , concat(./Field[@alias='BOOK_FOR'] , '|' , ./Field[@alias='START_DATE'] ) )[1]) = 1]">
                <!-- Sort by the start date -->
                <!-- year  -->
                <xsl:sort select="substring-after(substring-after(./Field[@alias='START_DATE'],','), ',')" order="ascending" data-type="number" />
                <!-- day  -->
                <xsl:sort select="substring-after(substring-after(substring-before(substring-after(./Field[@alias='START_DATE'],','), ','), ' '), ' ' )" order="ascending" data-type="number" />
                 <!-- month...this is a mess, tackle later
                <xsl:sort select="substring-before(substring-after(./Field[@alias='START_DATE'],','), ' ')" order="ascending" data-type="number" />
                -->
                <xsl:value-of select="./Field[@alias='START_DATE']" /><br/>           
                <xsl:value-of select="./Field[@alias='ITEM_NAME']" /><br/>
            </xsl:for-each>
            <br />
        </xsl:for-each>
    </xsl:template>
      
</xsl:stylesheet>
&#13;
&#13;
&#13;

所需的输出

我想在名字第一的地方得到它,然后在每个名字下面是他们来的日子,他们的服务列在每个相应的日期之下。如下面的示例:

&#13;
&#13;
McNotes, Sue
<hr>
<b>Tuesday, December 5, 2017</b><br>
Experience Package - Weekday<br>
Morning Soak - Weekday<br>
<b>Wednesday, December 6, 2017</b>
Test Scrub - Weekday<br>

Phone, Angie
<hr>
<b>Tuesday, December 5, 2017</b><br>
Sea Soak<br>
Ocean Package - Weekday<br>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

改变这个:

>>> x= 5

>>> y = 2

>>> a = x*y

>>> x,y,a

(5, 2, 10)

>>> x = 3

>>> x,y,a

(3, 2, 10)

为:

<xsl:for-each select="Row[count(. | key( 'guests-date' , concat(./Field[@alias='BOOK_FOR'] , '|' , ./Field[@alias='START_DATE'] ) )[1]) = 1]">

解释

你在<xsl:for-each select="../Row[count(. | key( 'guests-date' , concat(./Field[@alias='BOOK_FOR'] , '|' , ./Field[@alias='START_DATE'] ) )[1]) = 1]"> 的背景下;您需要前往父Row才能选择同一组中的同级Rows