1

これらのデータ セットがあり、特定の値を削除したいと考えています。このメソッドは、必要なものを削除しましたが、それとともに他の値も削除しました。

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

削除されたコード

  • (の一部) 私が欲しかったもの
  • 削除するつもりのなかったエドマンドの家
4

2 に答える 2

2

makeDeletesテンプレートは、 に値が含まれていない場合に何をすべきかを考慮していません。pTextpDeletes

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>
于 2012-09-05T10:51:54.577 に答える
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>
于 2012-09-05T12:58:24.853 に答える