2012-12-03 35 views
1

我想弄明白這一點,但已經過了一個星期。任何人都有如何繼續的建議?目標:讀取兩個字符串,拆分csv並比較xslt中的值1.0

我想: splitOne(逗號分隔的任意長度的字符串)的foreach條目,與每個splitTwo(任何長度的逗號分隔的字符串)進行比較。如果匹配,則顯示該節點。如果不是,不要。

如果這是C#,這將已經結束。但是在XSLT1.0(和僅有的XSLT 1.0)中,這對我來說是個大問題。

splitOne是外環,splitTwo是INNERLOOP。

對於XSLT來說,我可以採用其他方式來執行此操作,但最終產品必須是XML。

--blue

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" 
       extension-element-prefixes="exsl" 
       version="1.0" > 

    <xsl:param name="Groups_From_Logged_In_User"></xsl:param> 
    <xsl:template match="/"> 
    <application> 
     <xsl:apply-templates/> 
    </application> 
    </xsl:template> 

    <xsl:template match="Module"> 
    <Module> 
     <xsl:attribute name="Control"> 
     <xsl:value-of select="@Control"/> 
     </xsl:attribute> 
     <xsl:apply-templates select="Program"/> 
    </Module> 
    </xsl:template> 

    <xsl:template match="Program"> 

    <xsl:call-template name="split"> 
     <xsl:with-param name="pText" select="@assocGroups"/> 
    </xsl:call-template> 
    <xsl:call-template name="splitTwo"> 
     <xsl:with-param name="pText" select="$Groups_From_Logged_In_User"/> 
    </xsl:call-template> 

    <!-- foreach splitOne, compare with each splitTwo. If there's a match, show the node. If not, don't.--> 


    <Program> 
     <xsl:attribute name="Control"> 
     <xsl:value-of select="@Control"/> 
     </xsl:attribute> 
     <xsl:attribute name="Value"> 
     <xsl:value-of select="@Value"/> 
     </xsl:attribute> 
     <xsl:attribute name="assocGroups"> 
     <xsl:value-of select="@assocGroups"/> 
     </xsl:attribute> 
    </Program> 
    <!-- </xsl:if> --> 
    </xsl:template> 


    <xsl:template name="split"> 
    <xsl:param name="pText"/> 
    <xsl:variable name="separator">,</xsl:variable> 
    <xsl:choose> 
     <xsl:when test="string-length($pText) = 0"/> 
     <xsl:when test="contains($pText, $separator)"> 
     <splitOne> 
      <xsl:value-of select="substring-before($pText, $separator)"/> 
     </splitOne> 
     <xsl:call-template name="split"> 
      <xsl:with-param name="pText" select="substring-after($pText, $separator)"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <splitOne> 
      <xsl:value-of select="$pText"/> 
     </splitOne> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:template name="splitTwo"> 
    <xsl:param name="pText"/> 
    <xsl:variable name="separator">,</xsl:variable> 
    <xsl:choose> 
     <xsl:when test="string-length($pText) = 0"/> 
     <xsl:when test="contains($pText, $separator)"> 
     <splitTwo> 
      <xsl:value-of select="substring-before($pText, $separator)"/> 
     </splitTwo> 
     <xsl:call-template name="splitTwo"> 
      <xsl:with-param name="pText" select="substring-after($pText, $separator)"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <splitTwo> 
      <xsl:value-of select="$pText"/> 
     </splitTwo> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

這是我運行這個對XML。

<?xml version="1.0" encoding="utf-8" ?> 
<application name="Home"> 
<Module Control="PayStation Setup"> 
    <Program Control="PSF0010 Facility Group Codes" Value="PSF0010 Facility Group Codes" assocGroups="1,2,3" /> 
    <Program Control="PSF0015 Facility Reps" Value="PSF0015 Facility Reps" assocGroups="1,2" /> 
    <Program Control="PSF0016 Facility Gasoline" Value="PSF0016 Facility Gasoline" assocGroups="1,2" /> 
    <Program Control="PSF0017 Facility Warranty" Value="PSF0017 Facility Warranty" assocGroups="1,2" /> 
    <Program Control="PSF0020 Facility Codes" Value="PSF0020 Facility Codes" assocGroups="1,2" /> 
    <Program Control="PSF0021 Facility Codes Inquiry" Value="PSF0021 Facility Codes Inquiry" assocGroups="1,2" /> 
    <Program Control="PSF0023 Facility VDN" Value="PSF0023 Facility VDN" assocGroups="1,2" /> 
    <Program Control="PSF0025 AAR Facilities" Value="PSF0025 Facility Facilities" assocGroups="1,2" /> 
    <Program Control="PSF0030 Club Codes" Value="PSF0030 Club Codes" assocGroups="1,2" /> 
    <Program Control="PSF0040 Additional Services" Value="PSF0040 Additional Services" assocGroups="1,2" /> 
    <Program Control="PSF0050 Additional Services Detail" Value="PSF0050 Additional Services Detail" assocGroups="1,2" /> 
    <Program Control="PSF0060 Start Miles (OM)" Value="PSF0060 Start Miles (OM)" assocGroups="1,2" /> 

    </Module> 
</application> 

我只需要上述其中assocGroup值也是$ Groups_From_Logged_In_User

+0

我們需要查看您正在運行的XML以及您期望輸出的XML。 – ABach

+0

輸出XML與上面的XML相同,但如果第一個CSV的值不在第二個CSV中,則節點將被刪除。 – bluerain

回答

0

程序節點我會做一個恆等變換,只有專門處理<Program>元素。我遞歸地分割出第一個CSV並查看它是否出現在assocGroups屬性中。

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

    <xsl:param name="Groups_From_Logged_In_User"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Program"> 
    <!-- We have to append a ',' to $Groups_From_Logged_In_User so that we still get 
     something back from substring-before(...,',') in the last iteration --> 
    <xsl:param name="list" select="concat($Groups_From_Logged_In_User,',')"/> 
    <!-- We also add commas around @assocGroups because we want to check for appearances of ",1," 
     rather than "1", as this would also be found in "11", which is not what we want to find --> 
    <xsl:param name="assocGroups" select="concat(',',@assocGroups,',')"/> 
    <xsl:choose> 
     <!-- We we've chipped everything off the list, we quit --> 
     <xsl:when test="$list=''"/> 
     <!-- Now we chip off the first bit of the list, wrap it in commas and see whether it appears in $asscoGroups --> 
     <xsl:when test="contains($assocGroups,concat(',',substring-before($list,','),','))"> 
     <xsl:copy-of select="."/> 
     </xsl:when> 
     <!-- If we didn't find anything, we drop the first value and recurse. --> 
     <xsl:otherwise> 
     <xsl:apply-templates select="."> 
      <xsl:with-param name="list" select="substring-after($list,',')"/> 
      <xsl:with-param name="assocGroups" select="$assocGroups"/> 
     </xsl:apply-templates> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 
+0

托馬斯W,感謝您的教育和完整答案。 – bluerain