0

単語のリストを含む xml ファイルを変換しようとしていますが、結果のドキュメントからいくつかの要素をより具体的に除外しようとしています。

私のリストは次のとおりです。

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl"?>

    <dic:englishWords xmlns:dic = "dictionary">
        <dic:words>
                <dic:englishWords xmlns:dic = "dictionary">
    <dic:title>
    English Dictionary
    </dic:title>
    <dic:author>
        <dic:authorsName>
            Author:
        <dic:name>
            User
        </dic:name>
        <dic:lastName>
            Name
        </dic:lastName> 
        </dic:authorsName>
    </dic:author>
    <dic:words>
            <dic:name>Water</dic:name><br/>
            <dic:name>Room</dic:name><br/>
            <dic:name>Computer</dic:name><br/>
            <dic:name>Book</dic:name><br/>
            <dic:name>Garage</dic:name><br/>
            <dic:name>Car</dic:name><br/>
            <dic:name>Ship</dic:name><br/>
            <dic:name>Food</dic:name><br/>
            <dic:name>Coffee</dic:name><br/>
            <dic:name>Program</dic:name><br/>
    </dic:words>
</dic:englishWords>

単語のリストへのパスは、次のように xml ファイルに含まれています。

<dic:dictionary xmlns:dic = "dictionary">
    <dic:Logo>Logo</dic:Logo>
    <dic:Author>User Name</dic:Author>
    <dic:EnglishWords>english</dic:EnglishWords>
    <dic:SwedishTranslation>swedish</dic:SwedishTranslation>
    <dic:SwedishWords>swedish</dic:SwedishWords>
    <dic:EnglishTranslation>english</dic:EnglishTranslation>
</dic:dictionary>

私の変換は次のとおりです

    <!--Declare a parameter with the nodes to be removed-->
   <xsl:param name="removeElementsNamed" select="'|dic:author|dic:title'"/>

    <!--create a template and call it remove node-->
    <xsl:template match="node()|@*" name="removeNode">
          <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
    </xsl:template>

    <!--remove the actual nodes-->
    <xsl:template match="*">
          <xsl:if test="not(contains($removeElementsNamed, concat('|',name(),'|')))">
            <xsl:call-template name="removeNode"/>
          </xsl:if>
    </xsl:template>

私はここで見つけた例に従おうとしています:

要素を除外する方法

...しかし、私の場合はうまくいきません。

どんな助けでも大歓迎です...

Bluetxxth

4

2 に答える 2

0

|ELEMENTNAME|したがって、 $removeElementsNamed に存在するかどうかに基づいて要素を除外しようとしているように見えますがdic:author、そのリストで両側にパイプを持つ唯一のアイテムです。これを行うと、ほとんど機能する可能性があります。

<xsl:param name="removeElementsNamed" select="'|dic:author|dic:title|'"/>

ただし、これはちょっとしたハックです。

より良いアプローチは、次のようなことをするだけです:

<xsl:template match="dic:author | dic:title" />

dic:authorこれにより、 andが出力から除外さdic:titleれます。

もう 1 つの問題は、このテンプレートの名前が間違っていることです。

<xsl:template match="node()|@*" name="removeNode"> 

このテンプレートが実際に機能する場合、送信されたノードを含めるmatchことになりますが、テンプレートに属性と属性の両方を含めることはできませんname。XSLT を次のように書き直して、ここから始めることをお勧めします。

<!--Declare a parameter with the nodes to be removed-->
<xsl:template match="dic:author | dic:title" />

<!--create a template and call it remove node-->
<xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
</xsl:template>
于 2013-01-22T02:28:11.193 に答える
0

不要な要素に一致する可能性がありますが、何も出力できません。

    <xsl:template match="nodeToMatch" />
于 2013-01-22T00:31:42.533 に答える