【问题标题】:How do i modify the XSL to change the xml format如何修改 XSL 以更改 xml 格式
【发布时间】:2011-02-11 15:36:45
【问题描述】:

在下面的 XSL 中,每次 xsl:when 得到满足时,我都想附加尽可能多的 <a> 和 </a> 标记。但是需要在标签内填充的数据应该只有一次。我在最后展示了预期的输出。

<xsl:param name="insert-file" as="document-node()" />
<xsl:template match="*">
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
            <xsl:with-param name="nodeValue" select="$input"/> 
    </xsl:call-template> 
</xsl:variable>
<xsl:copy-of select="$Myxml"></xsl:copy-of>
</xsl:template>

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:for-each select="$insert-file/insert-data/data">
        <xsl:choose> 
            <xsl:when test="@index = 1">
                <a><xsl:value-of select="$nodeValue"></xsl:value-of></a> 
            </xsl:when>             
        </xsl:choose> 
    </xsl:for-each> 
</xsl:template>     

当前输出:

&lt;?xml version="1.0" encoding="UTF-8"?> &lt;a>我的短信&lt;/a> &lt;a>我的短信&lt;/a> &lt;a>我的短信&lt;/a> &lt;a>我的短信&lt;/a>

我希望模板“populateTag”向我返回以下格式的 xml。我如何修改模板“populateTag”以实现相同。

模板“populateTag”的预期输出: &lt;?xml version="1.0" encoding="UTF-8"?> &lt;a>&lt;a>&lt;a>&lt;a>我的短信&lt;/a>&lt;/a>&lt;/a>&lt;/a>

请说出你的想法。

【问题讨论】:

    标签: xml xslt-2.0 xslt


    【解决方案1】:

    为此,您需要某种递归(嵌套 a 元素)。

    没有尝试,因为我没有示例 XML 文档:

    <xsl:param name="insert-file" as="document-node()" />
    <xsl:template match="*">
    <xsl:variable name="input">My text</xsl:variable> 
    <xsl:variable name="Myxml" as="element()*"> 
        <xsl:call-template name="populateTag"> 
                <xsl:with-param name="nodeValue" select="$input"/> 
                <xsl:with-param name="position" select="1"/> 
        </xsl:call-template> 
    </xsl:variable>
    <xsl:copy-of select="$Myxml"></xsl:copy-of>
    </xsl:template>
    
    <xsl:template name="populateTag"> 
        <xsl:param name="nodeValue"/> 
        <xsl:param name="position"/> 
        <xsl:variable name="total" select="count($insert-file/insert-data/data[@index = 1])" />
        <xsl:for-each select="$insert-file/insert-data/data[@index = 1]">
          <xsl:if test="position() = $position" >
              <xsl:choose> 
                  <xsl:when test="position() = $total">
                      <a><xsl:value-of select="$nodeValue"></xsl:value-of></a>
                  </xsl:when>             
                  <xsl:otherwise>
                    <a>    
                          <xsl:call-template name="populateTag"> 
                                  <xsl:with-param name="nodeValue" select="$input"/> 
                                  <xsl:with-param name="position" select="$position+1"/> 
                          </xsl:call-template> 
                    </a>
                  </xsl:otherwise>
              </xsl:choose> 
            </xsl:if>
        </xsl:for-each> 
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2018-10-12
      相关资源
      最近更新 更多