English 中文(简体)
重组节点
原标题:Restructure nodes
  • 时间:2012-05-25 18:45:56
  •  标签:
  • xslt

我有一个Xml, 结构如下

<xml> 
<object context="3-cumulative" > 
  <metadata> 
    <ref cite="4.2" relevance="first.2"/> 
  </metadata> 
<body> 
   <para> 
      <text>  
             applicable on and after December 14,2007. 
      </text> 
   </para> 
 </body> 
</object> 

 <object context="1-cumulative" > 
  <metadata> 
    <ref cite="4.2" relevance="first.1"/> 
  </metadata> 
<body> 
   <para> 
     <text>  
       applicable on and after December 14,2006. 
     </text> 
   </para> 
</body> 
 </object> 


   <object context="1-cumulative" > 
   <metadata> 
      <related-content-ref cite="5 annuity" relevance="first.1"/> 
   </metadata> 
  <body> 
    <para> 
      <text> 
         applicable on and after December 14, 2008 
      </text> 
     </para> 
   </body> 
  </object> 


   <mainbody> 
        <num cite="4.2">4.2</num> 
        <num cite="2" type="para">2</num> 
        <heading>Stock exchanges</heading> 
        <prov-body> 
          <text> 
              Notwithstanding the provisions of a convention ... as defined in the 
              <italic>Income Tax Act</italic>. 
          </text> 
            <prov> 
              <num cite="1 annuity"/> 
              <num cite="5 annuity"/> 
              <num cite="3 annuity"/> 

                 <heading>“annuity”</heading> 
                <text> 
                <term>“annuity”</term>does not include any pension payment ... 
                </text> 
               <text> 
                 any pension payment ... 
               </text> 
           </prov> 
       </prov-body> 
      </mainbody> 
    </xml>   

我需要,如果在“mainbody” num/@cite and objects/@context/@context is 1-cummmmmm/@cite and@context is 1-cumulation and@context/text is 1-cumulative then para/ text from 对象的文本节点应在第一个文本节点结尾处复制,并且应该按对象/metatadata/ref/@relegations排序,或者如果在“mambody” num/@cite and contite and context/@context is 3-crecum and@context of the spextword in the firstext ext node with its Text elmet/metatadata/ref/@relegentation

产出应当是:

<xml> 
 <mainbody> 
 <num cite="4.2">4.2</num> 
 <num cite="2" type="para">2</num> 
 <heading>Stock exchanges</heading> 
 <prov-body> 
  <text> 
    Notwithstanding the provisions of a convention ... as defined in the 
    <italic>Income Tax Act</italic>. 
    **applicable on and after December 14, 2006** 
  </text> 
  <text>   **applicable on and after December 14, 2007** </text>

 <prov> 
        <num cite="1 annuity"/> 
        <num cite="5 annuity"/> 
        <num cite="3 annuity"/> 
        <heading>“annuity”</heading> 
        <text> 
        <term>“annuity”</term>does not include any pension payment ... 
         **applicable on and after December 14, 2008** 
        </text> 
        <text> 
        any pension payment ... 
       </text> 
   </prov> 
  </prov-body> 
 </mainbody> 
</xml>   
最佳回答

<% 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:key name="kRefByCite"
          match="metadata/*" use="@cite" />

     <xsl:key name="kRefByCite1"
          match="*[@context= 1-cumulative ]/metadata/*"
          use="@cite" />

     <xsl:key name="kRefByCite3"
          match="*[@context= 3-cumulative ]/metadata/*"
          use="@cite" />

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

     <xsl:template match=
       "text[not(preceding-sibling::text[1])
           and
             preceding::num
                  [key( kRefByCite , @cite)]]">
       <text>
         <xsl:apply-templates/>
         <xsl:for-each select=
           "key( kRefByCite1 , (.|..)/preceding-sibling::num/@cite)">
           <xsl:sort select="@relevance"/>

           <xsl:value-of select="../../body/para/text"/>
         </xsl:for-each>
       </text>
       <xsl:for-each select=
         "key( kRefByCite3 , (.|..)/preceding-sibling::num/@cite)">
         <xsl:sort select="@relevance"/>

         <text><xsl:value-of select="../../body/para/text"/></text>
       </xsl:for-each>

     </xsl:template>

     <xsl:template match=
       "node()
          [parent::* and not(ancestor-or-self::mainbody)]"/>
</xsl:stylesheet>

在提供 XML 文档 上应用时 < 坚固> :

<xml>
    <object context="3-cumulative" >
      <metadata>
        <ref cite="4.2" relevance="first.2"/>
      </metadata>
    <body>
       <para>
          <text>
                 applicable on and after December 14,2007.
          </text>
       </para>
     </body>
    </object>

     <object context="1-cumulative" >
      <metadata>
        <ref cite="4.2" relevance="first.1"/>
      </metadata>
    <body>
       <para>
         <text>
           applicable on and after December 14,2006.
         </text>
       </para>
    </body>
     </object>


       <object context="1-cumulative" >
       <metadata>
          <related-content-ref cite="5 annuity" relevance="first.1"/>
       </metadata>
      <body>
        <para>
          <text>
             applicable on and after December 14, 2008
          </text>
         </para>
       </body>
      </object>


       <mainbody>
            <num cite="4.2">4.2</num>
            <num cite="2" type="para">2</num>
            <heading>Stock exchanges</heading>
            <prov-body>
              <text>
                  Notwithstanding the provisions of a convention ... as defined in the
                  <italic>Income Tax Act</italic>.
              </text>
                <prov>
                  <num cite="1 annuity"/>
                  <num cite="5 annuity"/>
                  <num cite="3 annuity"/>

                     <heading>“annuity”</heading>
                    <text>
                    <term>“annuity”</term>does not include any pension payment ...
                    </text>
                   <text>
                     any pension payment ...
                   </text>
               </prov>
           </prov-body>
          </mainbody>
</xml>

< 强力 > 生成通缉对象,正确结果:

<xml>
   <mainbody>
      <num cite="4.2">4.2</num>
      <num cite="2" type="para">2</num>
      <heading>Stock exchanges</heading>
      <prov-body>
         <text>
                  Notwithstanding the provisions of a convention ... as defined in the
                  <italic>Income Tax Act</italic>.

           applicable on and after December 14,2006.
         </text>
         <text>
                 applicable on and after December 14,2007.
          </text>
         <prov>
            <num cite="1 annuity"/>
            <num cite="5 annuity"/>
            <num cite="3 annuity"/>
            <heading>“annuity”</heading>
            <text>
               <term>“annuity”</term>does not include any pension payment ...

             applicable on and after December 14, 2008
          </text>
            <text>
                     any pension payment ...
                   </text>
         </prov>
      </prov-body>
   </mainbody>
</xml>
问题回答

暂无回答




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签