Xml 使用xslt将节点重命名为节点';的名称与其索引连接在一起

Xml 使用xslt将节点重命名为节点';的名称与其索引连接在一起,xml,xslt,xls,Xml,Xslt,Xls,我使用下面的xslt来转换xmls,它通过接收/处理进行迭代 节点,并将类型属性添加到节点。根据节点的值,它将是string/int/float。 那部分很好用 我还应该将“Node”节点重命名为与其索引和 添加一个属性,该属性应该是“Name”节点的值 我可能有多个“节点”,我想将其转换为如下内容: “Cuda3DLut”来自Cuda3DLut 来源 身份 结果1 0 观看 LUT 第二个节点到 <Node2 type="CudaTool" > ... </Node2&g

我使用下面的xslt来转换xmls,它通过接收/处理进行迭代 节点,并将类型属性添加到节点。根据节点的值,它将是string/int/float。 那部分很好用

我还应该将“Node”节点重命名为与其索引和 添加一个属性,该属性应该是“Name”节点的值

我可能有多个“节点”,我想将其转换为如下内容:

“Cuda3DLut”来自
Cuda3DLut


来源
身份
结果1
0
观看
LUT
第二个节点到

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

...
我还想将“输入”节点的值(如果它是“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>
源XML:
Cuda3DLut
主流媒体
身份
结果1
0
观看
LUT
CudaTool
主流媒体
身份
结果1
0
观看
LUT
...
xslt:
工具链接
输入
红,绿,蓝
工具链接
输出
红,绿,蓝
浮动
int
一串

我认为您可以使用
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>

节点
...

我认为您可以使用
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>

节点
...

此转换:

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

一串
来源
应用于提供的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>
<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>

Cuda3DLut
主流媒体
身份
结果1
0
观看
LUT
CudaTool
主流媒体
身份
结果1
0
观看
LUT
...     
生成所需的正确结果:

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

来源
身份
结果1
0
观看
LUT
来源
身份
结果1
0
观看
LUT

此转换:

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

一串
来源
应用于提供的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>
<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>

Cuda3DLut
主流媒体
身份
结果1
0
观看
LUT
CudaTool
主流媒体
身份
结果1
0
观看
LUT
...     
生成所需的正确结果:

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

来源
身份
结果1
0
观看
LUT
来源
身份
结果1
0
观看
LUT

我对模板的使用不太了解不幸的是,我不确定应该如何添加您的模板以获得预期的结果,这样基本上就可以得到Node1/Node2/。。。节点和层次结构中下面的所有子节点。非常感谢。我看到您使用了
,您可以在
元素上使用此循环而不是我的模板,并将我的
+
放入其中。不过,您应该记录自己关于模板的情况,使用它们很好!我对模板的使用不太了解,不幸的是,我不确定我应该如何添加你的模板以获得预期的结果,所以基本上要有Node1/Node2/。。。节点和层次结构中下面的所有子节点。非常感谢。我看到您使用了
,您可以在
元素上使用此循环而不是我的模板,并将我的
+
放入其中。不过,您应该记录自己关于模板的情况,使用它们很好!你错过了很多重要的东西,比如:1)想要的结果;2) 转换必须实现的需求。请编辑问题并提供这些——我们无法读懂你的心思。另外,您提供的代码与您的要求相矛盾(做了一些与之相反的事情)
type
属性应该如何生成。尊敬的汉克斯,加博。请看一下我的答案。你错过了很多重要的东西,比如:1)想要的结果;2) 转换必须实现的需求。请编辑问题并提供这些——我们无法读懂你的心思。另外,您提供的代码与您的要求相矛盾(做了一些与之相反的事情)
type
属性应该如何生成。尊敬的汉克斯,加博。请看一下我的答案。谢谢你,迪米特。不幸的是,“节点”下的标记名可能会更改,因此我需要根据值确定类型。在这种情况下,我也可能需要在以后为类型检测创建异常。我试着把你的信合起来