2012-09-05 42 views
1

我有這些數據集,我想刪除一些特定的值。該方法刪除了我想要的,但也刪除了一些其他值。XSLT中的錯誤刪除

<Address> 
<Row> 
<LD>Dwelling place</LD> 
<LN>Dwelling (Part Of)</LN> 
</Row> 

<Row> 
<LD>jarkatar</LD> 
<LN>Edmund's Home</LN> 
</Row> 
</Address> 



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:Aa="Aa:Aa"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<Aa:LNDeletes> 
    <del>(Part Of)</del> 
<Aa:LNDeletes> 


<xsl:variable name="vLocNameDels" select="document('')/*/Aa:LNDeletes/*"/> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="LN/text()"> 
    <xsl:call-template name="makeDeletes"> 
     <xsl:with-param name="pDeletes" select="$vLocNameDels"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="makeDeletes"> 
    <xsl:param name="pText" select="."/> 
    <xsl:param name="pDeletes"/> 

    <xsl:variable name="vDelText" select="$pDeletes[contains($pText, .)][1]"/> 
    <xsl:if test="$vDelText"> 
     <xsl:variable name="vRough"> 
      <xsl:value-of select="substring-before($pText, $vDelText)"/> 
      <xsl:value-of select="substring-after($pText, $vDelText)"/> 
     </xsl:variable> 

     <xsl:value-of select="normalize-space($vRough)"/> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

與上面的代碼我得到這個

<Address> 
<Row> 
<LD>Dwelling place</LD> 
<LN>Dwelling</LN> 
</Row> 

<Row> 
<LD>jarkatar</LD> 
<LN></LN> 
</Row> 

,而不是這個

<Address> 
<Row> 
<LD>Dwelling place</LD> 
<LN>Dwelling</LN> 
</Row> 

<Row> 
<LD>jarkatar </LD> 
<LN>Edmund's Home</LN> 
</Row> 

代碼刪除

  • 這就是我想要的(部分)
  • 埃德蒙的家,這是不是意味着被刪除

回答

2

makeDeletes模板不佔如果pText不包含任何pDeletes值什麼就做什麼。

調整makeDeletes從xsl:if模板到xsl:choose

<xsl:template name="makeDeletes"> 
    <xsl:param name="pText" select="."/> 
    <xsl:param name="pDeletes"/> 

    <xsl:variable name="vDelText" select="$pDeletes[contains($pText, .)][1]"/> 
    <xsl:choose> 
     <xsl:when test="$vDelText"> 
      <xsl:variable name="vRough"> 
       <xsl:value-of select="substring-before($pText, $vDelText)"/> 
       <xsl:value-of select="substring-after($pText, $vDelText)"/> 
      </xsl:variable> 

      <xsl:value-of select="normalize-space($vRough)"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$pText"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
0

類似的解決方案,但是從makeDeletes模板向上移動的邏輯:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:Aa="Aa:Aa"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<Aa:LNDeletes> 
    <del>(Part Of)</del> 
</Aa:LNDeletes> 


<xsl:variable name="vLocNameDels" select="document('')/*/Aa:LNDeletes/*"/> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="LN/text()"> 
    <xsl:choose> 
     <xsl:when test="$vLocNameDels[contains(current(), .)]"> 
      <xsl:call-template name="makeDeletes"> 
       <xsl:with-param name="pDeletes" select="$vLocNameDels"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise><xsl:apply-imports/></xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template name="makeDeletes"> 
    <xsl:param name="pText" select="."/> 
    <xsl:param name="pDeletes"/> 

    <xsl:variable name="vDelText" select="$pDeletes[contains($pText, .)][1]"/> 
    <xsl:variable name="vRough"> 
     <xsl:value-of select="substring-before($pText, $vDelText)"/> 
     <xsl:value-of select="substring-after($pText, $vDelText)"/> 
    </xsl:variable> 

    <xsl:value-of select="normalize-space($vRough)"/> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉化是應用於提供的XML文檔

<Address> 
    <Row> 
     <LD>Dwelling place</LD> 
     <LN>Dwelling (Part Of)</LN> 
    </Row> 
    <Row> 
     <LD>jarkatar</LD> 
     <LN>Edmund's Home</LN> 
    </Row> 
</Address> 

有用,正確的結果產生

<Address> 
    <Row> 
     <LD>Dwelling place</LD> 
     <LN>Dwelling</LN> 
    </Row> 
    <Row> 
     <LD>jarkatar</LD> 
     <LN>Edmund's Home</LN> 
    </Row> 
</Address>