2016-06-02 7 views
2

から2つのノードを作る:私は上記のすべてのノードをコピーするだけでなく、一定の値を持つノードのいくつかの追加のコピーを作成する必要がXSLTコピー - 私は、次のXML持つ1つのノード

<records> 
     <record> 
      <id>111</id> 
      <amount>123.45</amount> 
      <taxCode>A</taxCode> 
     </record> 

    </records> 

をtaxCodeのことです。 は、どのように私は次のようachiveになります。これですべてのヘルプは高く評価され

<?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" 
       encoding="UTF-8" 
       omit-xml-declaration="no" 
       indent="yes" /> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="records/taxCode[text() = 'A']" > 
     <xsl:copy select="node()" /> 
    </xsl:template> 

</xsl:stylesheet> 

<records> 
     <record> 
      <id>111</id> 
      <amount>123.45</amount> 
      <taxCode>A</taxCode> 
     </record> 

     <!-- copy of the node above with amount and taxCode changed --> 
     <record> 
      <id>111</id> 
      <amount>-123.45</amount> 
      <taxCode>B</taxCode> 
     </record> 
    </records> 

私は次のような単純なXSLしかし、一度そのちょうどコピーのすべてを使用してみました。

答えて

1

方法について:

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

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="records"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
     <xsl:apply-templates select="record[taxCode='A']" mode="B"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="record" mode="B"> 
    <xsl:copy> 
     <xsl:copy-of select="id"/> 
     <amount> 
      <xsl:value-of select="-amount"/> 
     </amount> 
     <taxCode>B</taxCode> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

または、:ここで

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

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="record[taxCode='A']" > 
    <xsl:copy> 
     <xsl:apply-templates/> 
    </xsl:copy> 
    <xsl:copy> 
     <xsl:copy-of select="id"/> 
     <amount> 
      <xsl:value-of select="-amount"/> 
     </amount> 
     <taxCode>B</taxCode> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
1

は、古いものと新しい税コードの間のマッピングを動的に指定することを可能にする一般的な変換であり、

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="my:my" exclude-result-prefixes="my"> 
<xsl:output omit-xml-declaration="yes"/> 

<my:Mapping> 
    <code old="A">B</code> 
    <code old="M">P</code> 
</my:Mapping> 

    <xsl:template match="node()|@*" name="identity"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="node()|@*" mode="new"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*" mode="new"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="record[taxCode = document('')/*/my:Mapping/*/@old]"> 
    <xsl:call-template name="identity"/> 
    <xsl:apply-templates select="." mode="new"/> 
    </xsl:template> 

    <xsl:template mode="new" 
     match="record[taxCode = document('')/*/my:Mapping/*/@old]/amount/text()" > 
    <xsl:value-of select="-1*."/> 
    </xsl:template> 

    <xsl:template mode="new" 
     match="record[taxCode = document('')/*/my:Mapping/*/@old]/taxCode/text()" > 
    <xsl:value-of select="document('')/*/my:Mapping/*[@old = current()]"/> 
    </xsl:template> 
</xsl:stylesheet> 

この変換は、次のXML文書に適用された場合:ザ・が欲しかった

<records> 
    <record> 
     <id>111</id> 
     <amount>123.45</amount> 
     <taxCode>A</taxCode> 
    </record> 
    <record> 
     <id>111</id> 
     <amount>123.45</amount> 
     <taxCode>C</taxCode> 
    </record> 
    <record> 
     <id>111</id> 
     <amount>123.45</amount> 
     <taxCode>M</taxCode> 
    </record> 
</records> 

、正しい結果がが生成されます

<records> 
    <record> 
     <id>111</id> 
     <amount>123.45</amount> 
     <taxCode>A</taxCode> 
    </record><record> 
     <id>111</id> 
     <amount>-123.45</amount> 
     <taxCode>B</taxCode> 
    </record> 
    <record> 
     <id>111</id> 
     <amount>123.45</amount> 
     <taxCode>C</taxCode> 
    </record> 
    <record> 
     <id>111</id> 
     <amount>123.45</amount> 
     <taxCode>M</taxCode> 
    </record><record> 
     <id>111</id> 
     <amount>-123.45</amount> 
     <taxCode>P</taxCode> 
    </record> 
</records> 

に注意してください:

my:Mappingツリー典型的独自のXML文書(ファイル)にすることができ、マッピングが変更されたときに変換を変更する必要はありませんg。

関連する問題