XSLT:在递增属性和值的同时多次复制对象xml [英] XSLT: copy object xml multiple times while incrementing attribute and value

查看:68
本文介绍了XSLT:在递增属性和值的同时多次复制对象xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的xml,我想复制n次,同时增加其元素之一和其属性之一.

I have a xml as below that I'd like to copy n times while incrementing one of its element and one of its attribute.

<?xml version="1.0"?>
<header xmlns="http://test.com" >
<Batch>
<test document="dump" >
<Person position=1>
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
</test>
</Batch>
</header>

并且我想要下面类似的东西,其增量数是一个变量.

and I'd like something like below with the number of increment to be a variable.

<?xml version="1.0"?>
<header xmlns="http://test.com" >
<Batch>
<test document="dump" >
<Person position=1>
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
<Person position=2>
    <properties>
        <name>John</name>
        <number>2</number>
    </properties>
</Person>
...
<Person position=n>
    <properties>
        <name>John</name>
        <number>n</number>
    </properties>
</Person>
</test>
</Batch>
</header>

为解决这个问题,我从下面的xslt开始:

To solve this, I've started with the xslt below:

 <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:param name="pTimes" select="2"/>


 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/*">
     <xsl:call-template name="applyNTimes">
         <xsl:with-param name="pTimes" select="$pTimes"/>
         <xsl:with-param name="pPosition" select="1"/>
     </xsl:call-template>
 </xsl:template>

 <xsl:template name="applyNTimes">
     <xsl:param name="pTimes" select="0"/>
     <xsl:param name="pPosition" select="1"/>

     <xsl:if test="$pTimes > 0">
         <xsl:choose>
         <xsl:when test="$pTimes = 1">
             <xsl:apply-templates select="*">
             <xsl:with-param name="pPosition" select="$pPosition"/>
             </xsl:apply-templates>
         </xsl:when>
         <xsl:otherwise>
             <xsl:variable name="vHalf" select="floor($pTimes div 2)"/>

             <xsl:call-template name="applyNTimes">
             <xsl:with-param name="pTimes" select="$vHalf"/>
             <xsl:with-param name="pPosition" select="$pPosition"/>
             </xsl:call-template>

             <xsl:call-template name="applyNTimes">
             <xsl:with-param name="pTimes" select="$pTimes - $vHalf"/>
             <xsl:with-param name="pPosition" select="$pPosition + $vHalf"/>
             </xsl:call-template>
         </xsl:otherwise>
         </xsl:choose>
     </xsl:if>
 </xsl:template>

 <xsl:template match="Person">
     <xsl:param name="pPosition" select="1"/>

     <xsl:value-of select="$newline"/>
     <Person position="{$pPosition}">
         <xsl:apply-templates>
         <xsl:with-param name="pPosition" select="$pPosition"/>
         </xsl:apply-templates>
      </Person>
 </xsl:template>

 <xsl:template match="number">
      <xsl:param name="pPosition" select="1"/>

      <number><xsl:value-of select="$pPosition"/></number>
 </xsl:template>

 </xsl:stylesheet>

,但是输出在元素中包含名称空间.元素和属性@position始终设置为1.此外,标头围绕每个元素. 请参考下面的输出,其中n = 2

but the output includes the namespace in elements. The element and attribute @position are always set to 1. Also, the header surrounds each element. Please refer to the output below with n=2

<Batch xmlns="http://test.com">
<test document="dump">
<Person position="1">
    <properties>
        <name>John</name>
        <number>1</number>
    </properties>
</Person>
</test>
</Batch>
<Batch xmlns="http://test.com">
<test document="dump">
    <Person position="1">
        <properties>
            <name>John</name>
            <number>1</number>
        </properties>
    </Person>
</test>
</Batch>

有任何线索吗?

推荐答案

此转换:

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

     <xsl:param name="pTimes" select="2"/>

     <xsl:template match="node()|@*">
      <xsl:param name="pPosition" select="1"/>
      <xsl:copy>
       <xsl:apply-templates select="node()|@*">
         <xsl:with-param name="pPosition" select="$pPosition"/>
       </xsl:apply-templates>
      </xsl:copy>
     </xsl:template>

     <xsl:template match="t:test">
         <xsl:call-template name="applyNTimes">
             <xsl:with-param name="pTimes" select="$pTimes"/>
             <xsl:with-param name="pPosition" select="1"/>
         </xsl:call-template>
     </xsl:template>

     <xsl:template name="applyNTimes">
         <xsl:param name="pTimes" select="0"/>
         <xsl:param name="pPosition" select="1"/>

         <xsl:if test="$pTimes > 0">
             <xsl:choose>
             <xsl:when test="$pTimes = 1">
                 <xsl:apply-templates select="*">
                 <xsl:with-param name="pPosition" select="$pPosition"/>
                 </xsl:apply-templates>
             </xsl:when>
             <xsl:otherwise>
                 <xsl:variable name="vHalf" select="floor($pTimes div 2)"/>

                 <xsl:call-template name="applyNTimes">
                 <xsl:with-param name="pTimes" select="$vHalf"/>
                 <xsl:with-param name="pPosition" select="$pPosition"/>
                 </xsl:call-template>

                 <xsl:call-template name="applyNTimes">
                 <xsl:with-param name="pTimes" select="$pTimes - $vHalf"/>
                 <xsl:with-param name="pPosition" select="$pPosition + $vHalf"/>
                 </xsl:call-template>
             </xsl:otherwise>
             </xsl:choose>
         </xsl:if>
     </xsl:template>

     <xsl:template match="t:Person">
         <xsl:param name="pPosition" select="1"/>

         <xsl:copy>
             <xsl:copy-of select="@*"/>
             <xsl:attribute name="position">
              <xsl:value-of select="$pPosition"/>
             </xsl:attribute>
             <xsl:apply-templates>
             <xsl:with-param name="pPosition" select="$pPosition"/>
             </xsl:apply-templates>
          </xsl:copy>
     </xsl:template>

     <xsl:template match="t:number">
          <xsl:param name="pPosition" select="1"/>
            <xsl:copy>
              <xsl:value-of select="$pPosition"/>
            </xsl:copy>
     </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档:

<header xmlns="http://test.com" >
    <Batch>
        <test document="dump" >
            <Person position="1">
                <properties>
                    <name>John</name>
                    <number>1</number>
                </properties>
            </Person>
        </test>
    </Batch>
</header>

产生想要的结果:

<header xmlns="http://test.com">
    <Batch>
        <Person position="1">
            <properties>
                <name>John</name>
                <number>1</number>
            </properties>
        </Person>
        <Person position="2">
            <properties>
                <name>John</name>
                <number>2</number>
            </properties>
        </Person>
    </Batch>
</header>

这篇关于XSLT:在递增属性和值的同时多次复制对象xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆