Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XSLT1.0在3个级别上分组数据_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Xml 使用XSLT1.0在3个级别上分组数据

Xml 使用XSLT1.0在3个级别上分组数据,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有一个包含日期、会话和子主题的XML文件。我的前两个级别运行良好,但第三个级别无法正常分组 日期为1级组 一天中的所有会议应分组在该日期下 课程编号上的2级组 具有相同编号的所有会话应分组在一起 第3级应在摘要标题上分组。 如果摘要标题相同,则应显示一次,并在其下列出所有作者 这是我的XSLT: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XS

我有一个包含日期、会话和子主题的XML文件。我的前两个级别运行良好,但第三个级别无法正常分组

日期为1级组 一天中的所有会议应分组在该日期下

课程编号上的2级组 具有相同编号的所有会话应分组在一起

第3级应在摘要标题上分组。 如果摘要标题相同,则应显示一次,并在其下列出所有作者

这是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:strip-space elements="*"/>

<xsl:key name="sessions-by-startDate" match="session" use="startDate"/>
<xsl:key name="sessions-by-Number" match="session" use="concat(startDate, '|', Session_Number)"/>
<xsl:key name="sessions-by-Abstract" match="stamp" use="concat(startDate, '|', Session_Number, '|', Abstract_Title)"/>
<xsl:template match="sessions">
<Guide>     
<xsl:for-each select="session[generate-id() = generate-id(key('sessions-by-startDate', startDate)[1])]">
<xsl:text>
</xsl:text><SessionDay>
<startDate><xsl:value-of select="startDate"/></startDate>

 <xsl:for-each select="key('sessions-by-startDate', startDate)[generate-id() = generate-id(key('sessions-by-Number', concat(startDate, '|', Session_Number))[1])]">
 <sessions>
 <xsl:apply-templates select="startTime"/>
 <xsl:apply-templates select="Session_Number" />
 <xsl:apply-templates select="Session_Title"/><xsl:text>
 </xsl:text><TopicTitle>Topics &amp; Faculty</TopicTitle>

 <xsl:for-each select="key('sessions-by-Number', concat(startDate, '|', Session_Number))">


 <xsl:for-each
 select="key('sessions-by-Number', concat(startDate, '|', Session_Number))[count(. | key('sessions-by-Abstract', concat(startDate, '|', Session_Number, '|', Abstract_Title))[1]) = 1]">
 <xsl:sort select="Abstract_Title"/>
 <xsl:text>
 </xsl:text><session>  
 <xsl:apply-templates select="Abstract_Title"/>

 <xsl:for-each select="key('sessions-by-Abstract', concat(startDate, '|', Session_Number, '|', Abstract_Title))">
 <xsl:apply-templates select="Author_LastName"/>
 </xsl:for-each>
 </session>  
 </xsl:for-each>


 </xsl:for-each>
 </sessions>

 </xsl:for-each>
 </SessionDay> 
 </xsl:for-each>
 </Guide>    
 </xsl:template>

<xsl:template match="startDate">
<startDate><xsl:value-of select="."/></startDate></xsl:template>

<xsl:template match="Session_Title"><xsl:text>
</xsl:text><Session_Title><xsl:value-of select="."/></Session_Title>    </xsl:template>

<xsl:template match="Session_Number"><xsl:text>
Session Number </xsl:text><Session_Number><xsl:value-of select="."/>    </Session_Number></xsl:template>

<xsl:template match="Abstract_Title"><Abstract_Title><xsl:value-of select="."/>    </Abstract_Title></xsl:template>

<xsl:template match="Author_LastName">
<Author_LastName><xsl:value-of select="."/></Author_LastName></xsl:template>

</xsl:stylesheet>
以下是一些示例XML:

<?xml version="1.0" encoding="utf-8"?><sessions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session><startDate>10/24/2015</startDate><Session_Number>92</Session_Number><Session_Title>Sleep Medicine</Session_Title><Abstract_Title>Concluding Remarks</Abstract_Title><Author_LastName>Stoller</Author_LastName></session>
<session><startDate>10/24/2015</startDate><Session_Number>92</Session_Number><Session_Title>Sleep Medicine</Session_Title><Abstract_Title>Welcome and Introduction</Abstract_Title><Author_LastName>Stoller</Author_LastName></session>
<session><startDate>10/24/2015</startDate><Session_Number>568</Session_Number><Session_Title>Hands-on Simulation</Session_Title><Abstract_Title>Airway</Abstract_Title><Author_LastName>Roth</Author_LastName></session>
<session><startDate>10/24/2015</startDate><Session_Number>568</Session_Number><Session_Title>Hands-on Simulation</Session_Title><Abstract_Title>Airway</Abstract_Title><Author_LastName>Eling</Author_LastName></session>
<session><startDate>10/24/2015</startDate><Session_Number>568</Session_Number><Session_Title>Hands-on Simulation</Session_Title><Abstract_Title>Airway</Abstract_Title><Author_LastName>Bell</Author_LastName></session>
<session><startDate>10/25/2015</startDate><Session_Number>1</Session_Number><Session_Title>Diagnosis of Lung Cancer</Session_Title><Abstract_Title>The Role of EBUS</Abstract_Title><Author_LastName>Silvestri</Author_LastName></session>
<session><startDate>10/25/2015</startDate><Session_Number>1</Session_Number><Session_Title>Diagnosis of Lung Cancer</Session_Title><Abstract_Title>Lung Cancer Staging</Abstract_Title><Author_LastName>Liberman</Author_LastName></session>
<session><startDate>10/25/2015</startDate><Session_Number>1</Session_Number><Session_Title>Diagnosis of Lung Cancer</Session_Title><Abstract_Title>Lung Cancer Staging</Abstract_Title><Author_LastName>Hong</Author_LastName></session>
<session><startDate>10/25/2015</startDate><Session_Number>9</Session_Number><Session_Title>Non-small Cell Lung Cancer??</Session_Title><Abstract_Title>Imaging </Abstract_Title><Author_LastName>Duong</Author_LastName></session>
</sessions>
生成的XML应如下所示:

<?xml version="1.0" encoding="utf-8"?><Guide>
<SessionDay>
<startDate>10/24/2015</startDate>
<sessions>Session Number <Session_Number>92</Session_Number>
<Session_Title>Sleep Medicine</Session_Title>    
<TopicTitle>Topics &amp; Faculty</TopicTitle>

<session><Abstract_Title>Concluding Remarks</Abstract_Title>
<Author_LastName>Stoller</Author_LastName></session>

<session><Abstract_Title>Welcome and Introduction</Abstract_Title>    
<Author_LastName>Stoller</Author_LastName></session>
</sessions>

<sessions>Session Number <Session_Number>568</Session_Number>
<Session_Title>Hands-on Simulation</Session_Title>
<TopicTitle>Topics &amp; Faculty</TopicTitle>

<session><Abstract_Title>Airway</Abstract_Title>     
<Author_LastName>Roth</Author_LastName>
<Author_LastName>Eling</Author_LastName>
<Author_LastName>Bell</Author_LastName></session>
</sessions></SessionDay>

<SessionDay><startDate>10/25/2015</startDate>
<sessions>Session Number <Session_Number>1</Session_Number>
<Session_Title>Diagnosis of Lung Cancer</Session_Title>
<TopicTitle>Topics &amp; Faculty</TopicTitle>

<session><Abstract_Title>The Role of EBUS</Abstract_Title>
<Author_LastName>Silvestri</Author_LastName></session>

<session><Abstract_Title>Lung Cancer Staging</Abstract_Title>
<Author_LastName>Liberman</Author_LastName></session>

<session><Abstract_Title>Lung Cancer Diagnosis</Abstract_Title>
<Author_LastName>Hong</Author_LastName></session></sessions>

<sessions>Session Number <Session_Number>9</Session_Number>
<Session_Title>Non-small Cell Lung Cancer??</Session_Title>
<TopicTitle>Topics &amp; Faculty</TopicTitle>

<session><Abstract_Title>Imaging</Abstract_Title>
<Author_LastName>Duong</Author_LastName></session>
</sessions></SessionDay>
</Guide>
我当前的XSLT创建了前两个组。但是我得到了重复的抽象标题元素。相反,它应该只包含一个唯一的抽象标题元素,然后列出所有唯一的作者姓名


我需要添加第三个密钥并生成另一个ID。但我不确定要将其插入何处或如何写入。任何帮助都将不胜感激

您的XSLT有几个问题:

一,。xsl:key name=sessions by Abstract匹配的是stamp而不是session,因此该键与任何内容都不匹配

二,。for-each是按数字键从会话中选择项目,然后在下一个xsl:for-each中使用作为标准从同一个键中进行选择,从而产生重复项

三,。如果要在输出中插入换行符,最好使用实体而不是换行符。例如:&xA;。这样,如果要对XSLT进行代码格式化,就不会有丢失换行符或在输出中添加额外空格的风险。但是,如果您只是尝试更好地格式化输出XML,那么它将更易于使用,这将非常方便地打印格式和缩进XML

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*"/>

    <xsl:key name="sessions-by-startDate" match="session" use="startDate"/>
    <xsl:key name="sessions-by-Number" match="session" use="concat(startDate, '|', Session_Number)"/>
    <xsl:key name="sessions-by-Abstract" match="session" use="concat(startDate, '|', Session_Number, '|', Abstract_Title)"/>

    <xsl:template match="sessions">
        <Guide>     
            <xsl:for-each select="session[generate-id() = generate-id(key('sessions-by-startDate', startDate)[1])]">
                <xsl:text>&#xA;</xsl:text>
                <SessionDay>
                <startDate><xsl:value-of select="startDate"/></startDate>
                 <xsl:for-each select="key('sessions-by-startDate', startDate)[generate-id() = generate-id(key('sessions-by-Number', concat(startDate, '|', Session_Number))[1])]">
                     <sessions>
                         <xsl:apply-templates select="startTime"/>
                         <xsl:apply-templates select="Session_Number"/>
                         <xsl:apply-templates select="Session_Title"/>
                         <xsl:text>&#xA;</xsl:text>
                         <TopicTitle>Topics &amp; Faculty</TopicTitle>
                         <xsl:for-each
                             select="key('sessions-by-Number', concat(startDate, '|', Session_Number))[count(. | key('sessions-by-Abstract', concat(startDate, '|', Session_Number, '|', Abstract_Title))[1]) = 1]">
                             <xsl:sort select="Abstract_Title"/>
                             <xsl:text>&#xA;</xsl:text>
                             <session>
                                 <xsl:apply-templates select="Abstract_Title"/>

                                 <xsl:for-each
                                     select="key('sessions-by-Abstract', concat(startDate, '|', Session_Number, '|', Abstract_Title))">
                                     <xsl:apply-templates select="Author_LastName"/>
                                 </xsl:for-each>
                             </session>
                         </xsl:for-each>
                     </sessions>
                 </xsl:for-each>
                </SessionDay> 
            </xsl:for-each>
        </Guide>    
    </xsl:template>

    <xsl:template match="startDate">
        <startDate><xsl:value-of select="."/></startDate>
    </xsl:template>

    <xsl:template match="Session_Title">
        <xsl:text>&#xA;</xsl:text>
        <Session_Title><xsl:value-of select="."/></Session_Title>    
    </xsl:template>

    <xsl:template match="Session_Number">
        <xsl:text>Session Number </xsl:text>
        <Session_Number>
            <xsl:value-of select="."/>
        </Session_Number>
    </xsl:template>

    <xsl:template match="Abstract_Title"><Abstract_Title><xsl:value-of select="."/>    
        </Abstract_Title>
    </xsl:template>

    <xsl:template match="Author_LastName">
        <Author_LastName><xsl:value-of select="."/></Author_LastName>
    </xsl:template>

</xsl:stylesheet>

我更新了XSLT以显示第三个键。但这并不是根据独特的元素进行分组。有人能找出第三把钥匙的毛病吗?谢谢。我来看看你的建议。仅供参考,我使用换行符是因为我正在导入Adobe InDesign。我必须检查InDesign是否符合建议的实体。它确实尊重使用xsl:text条目的换行符。我不敢相信我在键中漏掉了那个错误。谢谢你的其他建议。然而,实体&xA;没有在InDesign中工作。我将尝试另一个实体。我和其他人相处得很好。&xA;这是一条新线。如果需要回车,请尝试&xd;我去看看。谢谢你建议使用$xA;但是如果没有xsl:text包装器,它会在Oxygen中抛出一个错误。相反,我使用了&10;这很有效,也很好地验证了这一点。我注意到十进制实体比十六进制实体工作得更好。我能够去掉所有xsl:text元素。当导入InDesign时,它们也可以正常工作。谢谢