2016-09-12 18 views
0

ソースxmlからの特定の一致がターゲットxmlの異なるコンテキストに含まれるターゲットxmlにソースxmlを変換したいとします。異なるコンテキストで同じマッチストリングを持つテンプレートを呼び出す

:私のようなテンプレートを適用したい名前カスタマーのテンプレートでは

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

    <xsl:template match="/"> 
    <xsl:call-template name="root" /> 
    </xsl:template> 

    <xsl:template name="root"> 
    <root> 
     <xsl:apply-templates select="/shiporder"/> 
     <xsl:call-template name="Customer"/> 
    </root> 
</xsl:template> 

<xsl:template name="Customer"> 
    <Customer> 
     <!--<xsl:apply-templates select="/shiporder"/>--> 
    </Customer> 
</xsl:template> 

<xsl:template match="/shiporder"> 
<xsl:apply-templates select="shipto"/> 
</xsl:template> 

<xsl:template match="/shiporder/shipto"> 
    <Address> 
     <xsl:apply-templates select="text()"/> 
    </Address> 
</xsl:template> 
</xsl:stylesheet> 

:私は、次のスタイルシートを適用し、このソースXMLに

<shiporder> 
    <shipto>orderperson1</shipto> 
    <shipto>orderperson1</shipto> 
    <city>London</city> 
</shiporder> 

:たとえば、私は、ソースXML等を有していて

<xsl:template match="/shiporder"> 
<xsl:apply-templates select="city"/> 
</xsl:template> 

<xsl:template match="/shiporder/city"> 
    <City> 
     <xsl:apply-templates select="text()"/> 
    </City> 
</xsl:template> 

しかし、既に/shiporderと一致するテンプレートを定義しました。だから私は同じマッチの両方のテンプレートがそれぞれのコンテキストで存在するスタイルシートをどのように設計するのか分かりません。

+2

'mode'を使用してください - https://www.w3.org/TR/xslt/#modes –

+0

あなたの質問はあいまいです。 1) '/ shiporder'は本当に何にもマッチできません。なぜならあなたの文書は'/root'で始まるからです。例として作業コードを使用してください。たとえそれが正しい結果をもたらさないとしても、それはそのような間違いを含むべきではありません。 2)あなたはどんな出力を期待しているかは言わなかった。入力サンプルと一致する出力を含めてください。 – Tomalak

+0

@Tomalak私はあなたの文句を理解していません1)作業例[here](http://xsltransform.net/3NSSEuL/3)を参照してください – StellaMaris

答えて

0

@michael.hor257kのようにmodeを使用すると、同じ要素に一致するが異なる結果を持つ2つ以上のテンプレートを区別することができます。このように見て終わる可能性があなたの場合は

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
    <xsl:call-template name="root" /> 
    </xsl:template> 

    <xsl:template name="root"> 
    <root> 
     <xsl:apply-templates select="/shiporder" mode="root"/> 
     <xsl:call-template name="Customer"/> 
    </root> 
</xsl:template> 

<xsl:template name="Customer"> 
    <Customer> 
     <xsl:apply-templates select="/shiporder" mode="customer"/> 
    </Customer> 
</xsl:template> 

<xsl:template match="/shiporder" mode="root"> 
<xsl:apply-templates select="shipto"/> 
</xsl:template> 

    <xsl:template match="/shiporder" mode="customer"> 
<xsl:apply-templates select="city"/> 
</xsl:template> 

<xsl:template match="shipto"> 
    <Address> 
     <xsl:apply-templates select="text()"/> 
    </Address> 
</xsl:template> 

    <xsl:template match="city"> 
    <City> 
     <xsl:apply-templates select="text()"/> 
    </City> 
</xsl:template> 

</xsl:stylesheet> 

は明らかにここにすべてのクレジットは、最初にこれを指摘するためにマイケルに行きます。

+0

Thx、私はマイケルが示唆したヒントを得ました。より多くの解決策があるのか​​、それとも「モード」だけですか?この例の場合、1つのモードで十分です。 "xsl:apply-templates要素にmode属性がない場合、mode属性を持たないxsl:template要素のテンプレート規則にのみ適用されます。 – StellaMaris

関連する問題