2012-04-06 19 views
1

xsltを使用してxmlsを変換し、Receipt/Process ノードを反復し、ノードにタイプ属性を追加します。ノードの値に基づいて、string/int/floatになります。 その部分は正常に動作します。xsltを使用してノードの名前をノードの名前に変更します。

"Node"ノードの名前を "Node"に変更し、 "Name"ノードの値にする属性を追加する必要があります。私はこのような何かに変換したい複数の「ノード」を持っているかもしれ

「Cuda3DLutは」

<Name>Cuda3DLut</Name>

<Node1 type="Cuda3DLut" > 
<Input type="ToolLink" role="Input" format="red,green,blue">Source</Input> 
<Lut type="string">Identity</Lut> 
<Output type="ToolLink" role="Output" format="red,green,blue">RESULT1</Output> 
<bypass type="int">0</bypass> 
<nodeRole type="string">viewing</nodeRole> 
<nodeShortName type="string">LUT</nodeShortName> 
</Node1> 

第2のノードから来てどこへ

最初のノード

<Node2 type="CudaTool" > 
... 
</Node2> 

"MainMedia"から "Source"の場合は "Input"ノードの値を変更したいだけですが、それだけです。

ありがとうございます。

Source XML: 

<Receipt> 

    <Process> 
    <Node> 
     <Name>Cuda3DLut</Name> 
     <Input>MainMedia</Input> 
     <Lut>Identity</Lut> 
     <Output>RESULT1</Output> 
     <bypass>0</bypass> 
     <nodeRole>viewing</nodeRole> 
     <nodeShortName>LUT</nodeShortName> 
    </Node> 
    <Node> 
     <Name>CudaTool</Name> 
     <Input>MainMedia</Input> 
     <Lut>Identity</Lut> 
     <Output>RESULT1</Output> 
     <bypass>0</bypass> 
     <nodeRole>viewing</nodeRole> 
     <nodeShortName>LUT</nodeShortName> 
    </Node> 
    </Process> 

    <Encode> 
    <Job> 
    ... 
    </Job> 
    </Encode> 

</Receipt> 

xslt: 

<?xml version="1.0"?> 
<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:template match="/"> 
     <cut> 
     <xsl:apply-templates/>   
    </cut> 
</xsl:template> 


    <xsl:template match="Process"> 

     <xsl:for-each select="Node/*"> 

     <xsl:choose> 

      <xsl:when test="name() = 'Input'"> 

       <xsl:copy> 
       <xsl:attribute name="type">ToolLink</xsl:attribute> 
       <xsl:attribute name="role">Input</xsl:attribute> 
       <xsl:attribute name="format">red,green,blue</xsl:attribute> 
       <xsl:apply-templates select="@*|node()" /> 
       </xsl:copy>    

      </xsl:when> 

      <xsl:when test="name() = 'Output'"> 
       <xsl:copy> 
       <xsl:attribute name="type">ToolLink</xsl:attribute> 
       <xsl:attribute name="role">Output</xsl:attribute> 
       <xsl:attribute name="format">red,green,blue</xsl:attribute> 
       <xsl:apply-templates select="@*|node()" /> 
       </xsl:copy>    

      </xsl:when> 

      <xsl:otherwise> 

      <!-- Add type attribute to the node based on its value --> 
      <xsl:choose> 
      <xsl:when test="number(.) = ."> 
       <xsl:choose> 
       <xsl:when test="contains(., '.')"> 
       <xsl:copy> 
        <xsl:attribute name="type">float</xsl:attribute> 
        <xsl:apply-templates select="@*|node()" /> 
       </xsl:copy> 
       </xsl:when> 
       <xsl:otherwise> 
       <xsl:copy> 
        <xsl:attribute name="type">int</xsl:attribute> 
        <xsl:apply-templates select="@*|node()" /> 
       </xsl:copy> 
       </xsl:otherwise> 
      </xsl:choose>          
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:copy> 
       <xsl:attribute name="type">string</xsl:attribute> 
       <xsl:apply-templates select="@*|node()" /> 
       </xsl:copy>    
      </xsl:otherwise>    
      </xsl:choose> 

     </xsl:otherwise>    
     </xsl:choose> 

     </xsl:for-each> 

    </xsl:template> 

    <xsl:template match="Encode">  
    </xsl:template> 

</xsl:stylesheet> 
+0

あなたは私たちに多くの重要なことを見せてくれませんでした。 2)変換が実装しなければならない要件。質問を編集してこれらを提供してください。あなたの心を読むことはできません。また、あなたが提供したコードは、あなたの要求に矛盾しています(あなたの要求に反します)。どのように 'type'属性が生成されるべきですか? –

+0

こんにちはDimitre、私はポストを更新しました。よろしく、 –

+0

ありがとう、ガボール。私の答えを見てください。 –

答えて

1
<xsl:template match="Receipt/Process/Node"> 
    <xsl:variable name="nodename"> 
     <xsl:text>Node</xsl:text> 
     <xsl:value-of select="position()"/> 
    </xsl:variable> 
    <xsl:element name="{$nodename}"> 
     ... 
    </xsl:element> 
</xsl:template> 

この変換:提供されるXML文書に塗布

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

<xsl:template match="Node"> 
    <xsl:element name="Node{position()}"> 
     <xsl:attribute name="type"> 
     <xsl:value-of select="Name"/> 
     </xsl:attribute> 
     <xsl:apply-templates select="*[not(self::Name)]"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="Node/*" priority="-1"> 
    <xsl:copy> 
    <xsl:attribute name="type">string</xsl:attribute> 
    <xsl:value-of select="."/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Node/Input"> 
    <Input type="ToolLink" role="Input" format="red,green,blue">Source</Input> 
</xsl:template> 

<xsl:template match="Output"> 
    <Output type="ToolLink" role="Output" format="red,green,blue"> 
    <xsl:value-of select="."/> 
    </Output> 
</xsl:template> 

<xsl:template match="bypass"> 
    <bypass type="int"><xsl:value-of select="."/></bypass> 
</xsl:template> 
</xsl:stylesheet> 

0:

<Receipt> 
    <Process> 
     <Node> 
      <Name>Cuda3DLut</Name> 
      <Input>MainMedia</Input> 
      <Lut>Identity</Lut> 
      <Output>RESULT1</Output> 
      <bypass>0</bypass> 
      <nodeRole>viewing</nodeRole> 
      <nodeShortName>LUT</nodeShortName> 
     </Node> 
     <Node> 
      <Name>CudaTool</Name> 
      <Input>MainMedia</Input> 
      <Lut>Identity</Lut> 
      <Output>RESULT1</Output> 
      <bypass>0</bypass> 
      <nodeRole>viewing</nodeRole> 
      <nodeShortName>LUT</nodeShortName> 
     </Node> 
    </Process> 
    <Encode> 
     <Job>  ...  </Job> 
    </Encode> 
</Receipt> 

募集、正しい結果を生成します

<Node1 type="Cuda3DLut"> <Input type="ToolLink" role="Input" format="red,green,blue">Source</Input> <Lut type="string">Identity</Lut> <Output type="ToolLink" role="Output" format="red,green,blue">RESULT1</Output> <bypass type="int">0</bypass> <nodeRole type="string">viewing</nodeRole> <nodeShortName type="string">LUT</nodeShortName> </Node1> <Node2 type="CudaTool"> <Input type="ToolLink" role="Input" format="red,green,blue">Source</Input> <Lut type="string">Identity</Lut> <Output type="ToolLink" role="Output" format="red,green,blue">RESULT1</Output> <bypass type="int">0</bypass> <nodeRole type="string">viewing</nodeRole> <nodeShortName type="string">LUT</nodeShortName> </Node2> 
+0

Dimitreありがとうございます。残念ながら、 "Node"の下にあるタグ名は変更される可能性がありますので、値に基づいてタイプを把握する必要があります。また、タイプ検出のために後で例外を作る必要がある場合もあります。私はあなたのソリューションをそれぞれのタイプの検出と組み合わせようとします。まだ運はありません。再度、感謝します。 –

+0

@GaborForgacs:どうぞよろしくお願いいたします。この処理のタイプは 'xsl:for-each'を全く必要とせず、依存しません。 'xsl:for-each'を避け、代わりに機能に集中してください。 –

+0

@GaborForgacs:「値に基づいてタイプを把握する必要がある」ということは、あなたが望むものがまだ正確に分からないということです。この答えは、あなたが質問で定義した正確な問題、つまり 'Nodexxx'のような名前の要素を生成する方法を解決します。解決策は現在受け入れられている答えよりも短く簡単です。 –

0

私はあなたが<xsl:element/>position()機能を使用してノードを生成することができると思います。

+0

残念ながら、私はあなたのテンプレートを追加して、基本的にNode1/Node2/...ノードと階層内のすべての子を持つように、予想される結果を得るべきかどうかわかりません。どうもありがとう。 –

+0

私はあなたが ''を使っているのを見て、テンプレートの代わりに ''要素にこのループを使い、 '' + ' '内側にある。しかし、テンプレートについて自分自身を文書化する必要があります。テンプレートを使用するのは素晴らしいことです! –

関連する問題