如何在XSLT中动态创建节点?

3
我是一名有用的助手,可以为您翻译文本。
我遇到了一个与XSLT转换相关的问题。我输入的XML如下所示:
<root>
    <photoname>image</photoname>
    <photocount>3</photocount>
</root>

我需要将它转换为以下的XML格式:
<root>
    <response>
        <images>
            <image>
                <small>image_1_120.jpg</small>              
            </image>
        </images>
        <images>
            <image>
                <small>image_2_120.jpg</small>              
            </image>
        </images>
        <images>
            <image>
                <small>image_3_120.jpg</small>              
            </image>
        </images>
    </response> 
</root>

是否可以使用XSLT(或XSLT中的C#函数)来实现?


如果计数器等于'0'怎么办?是否应该创建<root><response><images>节点? - Rookie Programmer Aravind
如果计数为null,我可以进行检查。 - Mirodil
4个回答

2
这里是一种非递归解决方案(您需要有足够的节点: )),也称为 Piez方法
<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:variable name="vCount" select="/*/photocount"/>

 <xsl:template match="/*">
     <root>
      <response>
        <xsl:for-each select=
          "(document('')//node())[not(position() > $vCount)]">
                <images>
                  <image>
                      <small>image_1_120.jpg</small>
                  </image>
          </images>
        </xsl:for-each>
      </response>
     </root>
 </xsl:template>
</xsl:stylesheet>

当应用此转换到提供的XML文档时:
<root>
    <photoname>image</photoname>
    <photocount>3</photocount>
</root>

期望的、正确的结果已经产生:
<root>
   <response>
      <images>
         <image>
            <small>image_1_120.jpg</small>
         </image>
      </images>
      <images>
         <image>
            <small>image_1_120.jpg</small>
         </image>
      </images>
      <images>
         <image>
            <small>image_1_120.jpg</small>
         </image>
      </images>
   </response>
</root>

@Miro:这并不是一个问题。你可以随时使用:document('Hamlet.xml')//node()——就像我以前做的那样... :) - Dimitre Novatchev

1

您可以使用递归模板在XSLT中实现此功能,以基于photocount元素进行迭代。

请尝试以下XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="root">
      <root>
         <response>
            <xsl:call-template name="photo">
               <xsl:with-param name="name" select="photoname"/>
               <xsl:with-param name="count" select="photocount"/>
            </xsl:call-template>
         </response>
      </root>
   </xsl:template>

   <xsl:template name="photo">
      <xsl:param name="name"/>
      <xsl:param name="count"/>
      <xsl:param name="current" select="1" />
      <images>
         <image>
            <small>
               <xsl:value-of select="concat($name, '_', $current, '_120.jpg')"/>
            </small>
         </image>
      </images>
      <xsl:if test="not($current >= $count)">
         <xsl:call-template name="photo">
            <xsl:with-param name="name" select="$name"/>
            <xsl:with-param name="count" select="$count"/>
            <xsl:with-param name="current" select="$current + 1"/>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>

当应用于您的示例XML时,将输出以下内容:
<root>
   <response>
      <images>
         <image>
            <small>image_1_120.jpg</small>
         </image>
      </images>
      <images>
         <image>
            <small>image_2_120.jpg</small>
         </image>
      </images>
      <images>
         <image>
            <small>image_3_120.jpg</small>
         </image>
      </images>
   </response>
</root>

请注意,我不知道_120后缀是如何生成的,所以我不得不在XSLT中硬编码它。

1

类似这样:

<xsl:template match="photocount">
  <xsl:call-template name="for.loop"> 
    <xsl:with-param name="i">1</xsl:with-param> 
    <xsl:with-param name="count" select="." /> 
  </xsl:call-template>  
</xsl:template>

<xsl:template name="for.loop">
  <xsl:param name="i" /> 
  <xsl:param name="count" /> 
  <xsl:if test="$i &lt;= $count">
    <images>
      <image>
        <small>image_<xsl:value-of select="$i"/>_120.jpg</small>              
      </image>
    </images>
  </xsl:if> 

  <xsl:if test="$i &lt;= $count"> 
    <xsl:call-template name="for.loop"> 
      <xsl:with-param name="i"> 
        <xsl:value-of select="$i + 1"/> 
      </xsl:with-param> 
      <xsl:with-param name="count"> 
        <xsl:value-of select="$count"/> 
      </xsl:with-param> 
    </xsl:call-template> 
  </xsl:if> 

</xsl:template> 

1

这个问题可能有很多解决方法,以下是其中之一:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:output indent="yes" />

    <xsl:template match="root">
        <xsl:element name="root">
           <xsl:element name="response">

                <xsl:call-template name="output">
                    <xsl:with-param name="name" select="photoname" />
                    <xsl:with-param name="value" select="photocount" />
                </xsl:call-template>

            </xsl:element>

        </xsl:element>
    </xsl:template>

    <xsl:template name="output">
        <xsl:param name="name" />
        <xsl:param name="value" as="xs:integer" />

        <xsl:if test="$value &gt; 1">
            <xsl:call-template name="output">
                <xsl:with-param name="name" select="$name" />
                <xsl:with-param name="value" select="$value - 1" />
            </xsl:call-template>
        </xsl:if>

        <xsl:element name="images">
           <xsl:element name="image">       
               <xsl:element name="small">
                   <xsl:value-of select="concat($name, '_', $value, '_120.jpg')" />
               </xsl:element>
            </xsl:element>
        </xsl:element>

    </xsl:template>
</xsl:stylesheet>

这是我的输出(在Altova XMLSpy下):

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <response>
        <images>
            <image>
                <small>image_1_120.jpg</small>
            </image>
        </images>
        <images>
            <image>
                <small>image_2_120.jpg</small>
            </image>
        </images>
        <images>
            <image>
                <small>image_3_120.jpg</small>
            </image>
        </images>
    </response>
</root>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接