繁体   English   中英

是否可以将模板应用于元素的副本?

[英]Is it possible to apply template to copy of element?

是否可以在元素副本上应用模板? 假设我有这个XML:

<root>
  <a name="a1">
    A1
    <b name="b1">B1</b>
  </a>
  <b name="b2">B2</b>
  <a name="a2">
    <b name="b3">B3</b>
  </a>
  <b name="b4">B4</b>
  <a name="a3">
    <b name="b5">B5</b>
  </a>
</root>

并希望将其转换为此XML:

<root>
  <a name="a1">
    A1
    <b name="b1">B1</b>
    <b name="b2">B2</b>
  </a>
  <a name="a2">
    <b name="b3">B3</b>
    <b name="b4">B4</b>
  </a>
  <a name="a3">
    <b name="b5">B5</b>
  </a>
</root>

而不是在新结构上应用此模板:

<xsl:template match="/">
  1<xsl:apply-templates/>2
</xsl:template>
<xsl:template match="a">
  3<xsl:apply-templates select="b[2]"/>4
</xsl:template>
<xsl:template match="b[not(position()=2)]">
  5<xsl:apply-templates/>6
</xsl:template>
<xsl:template match="b[2]">
  7<xsl:apply-templates/>8
</xsl:template>

所以输出应该是:

1
  3
    A1
    5B16
    7B28
  4
  3
    5B36
    7B48
  4
  3
    5B56
  4
2

重要的是我想在新结构上使用普通模板

到目前为止,我有这个,但它不起作用(不承认新的结构)

<xsl:template match="/">
1
<xsl:apply-templates select="@*|node()"/>
2
</xsl:template>
<xsl:template match="a">
3
    <xsl:for-each select="following-sibling::b[1]">
     <xsl:apply-templates select="." mode="aaa"/>
    </xsl:for-each>
4
</xsl:template>
<xsl:template match="b"/>
<xsl:template match="b[not(position()=2)]" mode="aaa">
  5<xsl:apply-templates/>6
</xsl:template>
<xsl:template match="b[2]" mode="aaa">
  7<xsl:apply-templates/>8
</xsl:template>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM