开发者

create child nodes from sibling nodes until a different sibbling occurs

开发者 https://www.devze.com 2023-01-03 00:23 出处:网络
Hi does anyone know what the xsl would look like to transform this XML. There can be N nte\'s after pid and N nte\'s after pv1. The structure is guaranteed in that all nte that follows pid belongs to

Hi does anyone know what the xsl would look like to transform this XML. There can be N nte's after pid and N nte's after pv1. The structure is guaranteed in that all nte that follows pid belongs to pid and all nte following pv1 belongs to pv1.

From:

<pid>
</pid>
<nte> 
  <nte-1>1</nte-1>
  <nte-3>Note 1</nte-1>
</nte>
<nte></nte>
<pv1></pv1>
<nte>
</nte>

into:

<pid>
  <nte> 
    <nte-1>1</nte-1>
    <nte-3>Note 1</n开发者_开发知识库te-1>
  </nte>
  <nte>
  </nte>
</pid>
<pv1>
  <nte>
  </nte>
</pv1>

Thanks!


This transformation:

<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="kLogicalChildren" match="nte"
  use="generate-id(preceding-sibling::*
                        [self::pid or self::pv1]
                         [1])"/>

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

 <xsl:template match="pid|pv1">
  <xsl:copy>
    <xsl:copy-of select="@*"/>

    <xsl:copy-of select=
    "key('kLogicalChildren', generate-id())"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="nte"/>
</xsl:stylesheet>

when applied on the provided XML document (corrected to be well-formed):

<t>
    <pid></pid>
    <nte>
        <nte-1>1</nte-1>
        <nte-3>Note 1</nte-3>
    </nte>
    <nte></nte>
    <pv1></pv1>
    <nte></nte>
</t>

produces the wanted, correct result:

<t>
    <pid>
        <nte>
            <nte-1>1</nte-1>
            <nte-3>Note 1</nte-3>
        </nte>
        <nte/>
    </pid>
    <pv1>
        <nte/>
    </pv1>
</t>
0

精彩评论

暂无评论...
验证码 换一张
取 消