how can i transform this using keys

[亡魂溺海] 提交于 2019-12-11 05:26:23

问题


I'm able to write a horribly bad xslt without using keys but it's quite slow and messy. Does anyone know how i would transform the following XML file into the expected result cleanly? Thanks!

input:

<testExecution>
    <test>
        <test.1/>
        <test.2/>
        <test.3/>
    </test>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <executor>
        <executor.1/>
        <executor.2/>
        <executor.3/>
    </executor>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <invoker>
        <invoker.1/>
        <invoker.2/>
        <invoker.3/>
    </invoker>
    <recipient>
        <recipient.1/>
        <recipient.2/>
        <recipient.3/>
    </recipient>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
</testExecution>

result:

<testExecution>
    <test>
        <test.1/>
        <test.2/>
        <test.3/>
    </test>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <people>
        <importantPeople>
            <executor>
                <executor.1/>
                <executor.2/>
                <executor.3/>
            </executor>
            <comment>
                <comment.1/>
                <comment.2/>
                <comment.3/>
            </comment>
            <comment>
                <comment.1/>
                <comment.2/>
                <comment.3/>
            </comment>
        </importantPeople>
        <invoker>
            <invoker.1/>
            <invoker.2/>
            <invoker.3/>
        </invoker>
    </people>
    <recipient>
        <recipient.1/>
        <recipient.2/>
        <recipient.3/>
    </recipient>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
</testExecution>

回答1:


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kElementByTest" match="invoker|recipient"
             use="generate-id(preceding-sibling::test[1])"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()[1]|@*"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="executor">
        <xsl:variable name="vFollowing"
                      select="key('kElementByTest',
                                  generate-id(preceding-sibling::test[1]))"/>
        <people>
            <importantPeople>
                <xsl:call-template name="identity"/>
            </importantPeople>
            <xsl:apply-templates select="$vFollowing/self::invoker"
                                     mode="copy"/>
        </people>
        <xsl:apply-templates select="$vFollowing/self::recipient"
                             mode="copy"/>
    </xsl:template>
    <xsl:template match="invoker"/>
    <xsl:template match="recipient"/>
    <xsl:template match="node()" mode="copy">
        <xsl:call-template name="identity"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<testExecution>
    <test>
        <test.1></test.1>
        <test.2></test.2>
        <test.3></test.3>
    </test>
    <comment>
        <comment.1></comment.1>
        <comment.2></comment.2>
        <comment.3></comment.3>
    </comment>
    <people>
        <importantPeople>
            <executor>
                <executor.1></executor.1>
                <executor.2></executor.2>
                <executor.3></executor.3>
            </executor>
            <comment>
                <comment.1></comment.1>
                <comment.2></comment.2>
                <comment.3></comment.3>
            </comment>
            <comment>
                <comment.1></comment.1>
                <comment.2></comment.2>
                <comment.3></comment.3>
            </comment>
        </importantPeople>
        <invoker>
            <invoker.1></invoker.1>
            <invoker.2></invoker.2>
            <invoker.3></invoker.3>
        </invoker>
    </people>
    <recipient>
        <recipient.1></recipient.1>
        <recipient.2></recipient.2>
        <recipient.3></recipient.3>
    </recipient>
    <comment>
        <comment.1></comment.1>
        <comment.2></comment.2>
        <comment.3></comment.3>
    </comment>
    <comment>
        <comment.1></comment.1>
        <comment.2></comment.2>
        <comment.3></comment.3>
    </comment>
</testExecution>

Note: Fine grained traversal. Keys for "just for this test" followings. Empty templates for "close this level". Mode for push style "keep processing". It could be full "pull style" matching "the previus before the mark".




回答2:


This transformation uses and overrides the identity rule. Comments immediately following executor are retrieved using the key() function. executor , invoker and comment immediately following executor are copied in special mode named people. In the usual, anonymous mode these are matched by an empty template to avoid copying them a second time:

<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="kExecComments" match=
  "comment
          [preceding-sibling::*
              [not(self::comment)][1]
                       [self::executor]
          ]"
  use="generate-id(preceding-sibling::executor[1])"/>

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

 <xsl:template match="executor">
  <people>
    <importantPeople>
      <xsl:apply-templates mode="people"
        select=".|key('kExecComments', generate-id())"/>
    </importantPeople>
    <xsl:apply-templates mode="people" select=
    "following-sibling::invoker"/>
  </people>
 </xsl:template>

 <xsl:template match="executor|comment|invoker"
      mode="people">
    <xsl:call-template name="identity"/>
 </xsl:template>

 <xsl:template match=
  "invoker |
   comment
          [preceding-sibling::*
              [not(self::comment)][1]
                       [self::executor]
          ]"/>

</xsl:stylesheet>

when applied on the provided XML document:

<testExecution>
    <test>
        <test.1/>
        <test.2/>
        <test.3/>
    </test>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <executor>
        <executor.1/>
        <executor.2/>
        <executor.3/>
    </executor>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <invoker>
        <invoker.1/>
        <invoker.2/>
        <invoker.3/>
    </invoker>
    <recipient>
        <recipient.1/>
        <recipient.2/>
        <recipient.3/>
    </recipient>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
    <comment>
        <comment.1/>
        <comment.2/>
        <comment.3/>
    </comment>
</testExecution>

produces the wanted, correct result:

<testExecution>
   <test>
      <test.1/>
      <test.2/>
      <test.3/>
   </test>
   <comment>
      <comment.1/>
      <comment.2/>
      <comment.3/>
   </comment>
   <people>
      <importantPeople>
         <executor>
            <executor.1/>
            <executor.2/>
            <executor.3/>
         </executor>
         <comment>
            <comment.1/>
            <comment.2/>
            <comment.3/>
         </comment>
         <comment>
            <comment.1/>
            <comment.2/>
            <comment.3/>
         </comment>
      </importantPeople>
      <invoker>
         <invoker.1/>
         <invoker.2/>
         <invoker.3/>
      </invoker>
   </people>
   <recipient>
      <recipient.1/>
      <recipient.2/>
      <recipient.3/>
   </recipient>
   <comment>
      <comment.1/>
      <comment.2/>
      <comment.3/>
   </comment>
   <comment>
      <comment.1/>
      <comment.2/>
      <comment.3/>
   </comment>
</testExecution>


来源:https://stackoverflow.com/questions/4281431/how-can-i-transform-this-using-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!