XSLT前兄弟限制和解决方法

时间:2011-04-13 08:55:52

标签: xslt xpath

我遇到了兄弟姐妹的问题。这真的有效吗?

XSL 看起来像这样

<stored-procedure id="search-algorithm-parent-child">
    <xsl:stylesheet version="1.0" xmlns:spbc="urn:lsapps.spbcontext" xmlns:user="http://www.lodestarcorp.com" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
        <xsl:output omit-xml-declaration="yes"/>
        <xsl:template match="node()|/|@*">
            <query name="CMNUALGPARENTCHILD">
                <sql>
                    SELECT 
                      parent.UIDALGLIBPARENTDTL as UIDPARENT,
                      parent.PARENTCHANNELID as VMCHANNELNUMBER, 
                      parent.UOMCODE as UOM, 
                      child.CHILDDCFLOW AS DCFLOW,
                    FROM
                      ##Q.NUALGLIBPARENTDTL parent,
                      ##Q.NUALGLIBCHILDDTL child 
                    WHERE 
                      child.UIDALGLIBPARENTDTL = parent.UIDALGLIBPARENTDTL
                      AND parent.UIDALGLIBRARY = '<xsl:value-of select="@UIDALGLIBRARY"/>'
                      ORDER BY UIDPARENT
                </sql>
            </query>
        </xsl:template>
    </xsl:stylesheet>
</stored-procedure>

当我在我的asp 中将XSL传递给LsdbCommand时,我不知道XML是什么样子

var xmlTable5 = LsdbCommand("LSDB.search-algorithm-parent-child", PrefixParams1.valueOf());

我假设XML数据将是这样的(按UIDPARENT排序):

<CMNUALGPARENTCHILD>
  <someNode>
    <UIDPARENT>21</UIDPARENT>
    <VMCHANNELNUMBER>123</VMCHANNELNUMBER>
    <UOM>5<UOM>
    <DCFLOW>R<DCFLOW>
  </someNode>
  <someNode>
    <UIDPARENT>21</UIDPARENT>
    <VMCHANNELNUMBER>123</VMCHANNELNUMBER>
    <UOM>5<UOM>
    <DCFLOW>R<DCFLOW>
  </someNode>
  ...
</CMNUALGPARENTCHILD>

目前,我关注的是UIDPARENT ..我确信结果会有以下UIDPARENT ..(按顺序)

21
21
21
81
81
81

现在,在asp中,我称之为

tTable5 = ProcessXsl(xmlTable5, XmlFreeFile("../zAlgorithmLibrary/AlgorithmParentChild.xslt"));

其中AlgorithmParentChild.xslt只是

<x:stylesheet version="1.0"
  xmlns:x="http://www.w3.org/1999/XSL/Transform"
  xmlns:i="urn:ls-i18n-formatter"
  xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns:user="http://www.lodestarcorp.com/user"
  exclude-result-prefixes="x i ms user">

<x:param name="Portal">N</x:param>
<x:param name="Include">ALL</x:param>
<x:param name="OrderParams"></x:param>
<x:param name="FromParam"></x:param>
<x:param name="ROWPERPAGE">25</x:param>
<x:param name="AllowEdit"></x:param>

<x:output method="html" version="1.0" encoding="UTF-8" indent="yes"/> 
<x:template match="/">
  <table class="FormTable">
    <x:for-each select="//CMNUALGPARENTCHILD">
      <tr>
        <td>current <x:value-of select="@UIDPARENT"/></td>
        <td>previous <x:value-of select="preceding-sibling::node()/@UIDPARENT"/></td> 
        <td>next <x:value-of select="following-sibling::node()/@UIDPARENT"/></td>
      </tr>
    </x:for-each>
  </table>
</x:template>

<x:include href="../controls/MultiSortImages.xslt"/>
<x:include href="../controls/LockedColumns.xslt"/>
</x:stylesheet>

主表看起来像这样

current 21 previous  next 21 
current 21 previous 21 next 21 
current 21 previous 21 next 81 
current 81 previous 21 next 81 
current 81 previous 21 next 81 
current 81 previous 21 next  

前三个结果似乎是正确的..但是在第三次迭代之后,前一个元素怎么会失败?它应该返回以下内容:

current 21 previous  next 21 
current 21 previous 21 next 21 
current 21 previous 21 next 81 
current 81 previous 21 next 81 
current 81 previous 81 next 81 
current 81 previous 81 next  

以下兄弟工作正常..兄弟姐妹有任何已知的局限吗?你能建议任何解决方法吗?

请注意,我还尝试在previous-sibling :: node()/ @ UIDPARENT上附加[position=1][position()]或简单地[1],但它仍然具有相同的结果.. < / p>

请理解我是ASP,XSLT,XSL的新手......而且我只提了代码片段而不是整个代码..

我只是想了解兄弟姐妹的工作原理..它的语法,关于它的一切......

1 个答案:

答案 0 :(得分:4)

W3C specification

中所述
  

previous-sibling axis包含上下文节点的所有前面的兄弟节点

它没有说的是它反向运作!您需要在XPath选择的末尾添加[position()=1]谓词,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<x:stylesheet version="1.0"
  xmlns:x="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="x">
<x:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>   
<x:template match="CMNUALGPARENTCHILD">
  <table>
    <x:for-each select="UIDPARENT">
      <tr>
        <td>current <x:value-of select="."/></td>
        <td>previous <x:value-of select="preceding-sibling::UIDPARENT[position()=1]"/></td>
        <td>next <x:value-of select="following-sibling::UIDPARENT[position()=1]"/></td>
      </tr>
    </x:for-each>
 </table>
</x:template>
</x:stylesheet>

我无法让你的问题中的XSL工作。但这适用于我的问题中的XML。

编辑:从原始问题添加XML以供参考

<CMNUALGPARENTCHILD>
    <UIDPARENT>21</UIDPARENT>
    <UIDPARENT>21</UIDPARENT>
    <UIDPARENT>21</UIDPARENT>
    <UIDPARENT>81</UIDPARENT>
    <UIDPARENT>81</UIDPARENT>
    <UIDPARENT>81</UIDPARENT>
</CMNUALGPARENTCHILD>
相关问题