目标:读入两个字符串,拆分csv并比较xslt 1.0中的值

时间:2012-12-03 16:15:19

标签: xslt xslt-1.0

我想弄清楚这一点,但已经过了一周。有人建议如何继续吗?

我想: 在splitOne中的foreach条目(任何长度的逗号分隔的字符串),与每个splitTwo(任何长度的逗号分隔的字符串)进行比较。如果匹配,则显示节点。如果没有,请不要。

如果这是C#,这已经结束了。但是在XSLT1.0(和ONLY XSLT 1.0)中,这对我来说是个大问题。

splitOne是OUTER LOOP,splitTwo是INNERLOOP。

我愿意接受除XSLT以外的其他方式,但最终产品必须是XML。

- 蓝色

<?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

1 个答案:

答案 0 :(得分: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>