1

以下の xslt を使用して xml を変換します。これは Receipt/Process ノードを反復処理し、タイプ属性をノードに追加します。ノードの値に基づいて、string/int/float になります。その部分はうまく機能します。

また、「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>

また、「Input」ノードの値が「MainMedia」の場合は「Source」に変更したいと思いますが、その場合のみです。

どうもありがとう。

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

2 に答える 2

1

この変換:

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

提供された 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>

必要な正しい結果が生成されます。

<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> 
于 2012-04-06T13:26:45.560 に答える
0

<xsl:element/>と関数でノードを生成できると思いますposition()

<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>
于 2012-04-06T11:06:03.800 に答える