1

我试图弄清楚这一点,但已经一周了。有人对如何进行有任何建议吗?

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

如果这是 C#,这已经结束了。但是在 XSLT1.0(并且只有 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

4

1 回答 1

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>
于 2012-12-03T17:37:08.293 回答