需要对xsl

时间:2015-09-17 16:32:02

标签: xslt xslt-1.0 xslt-2.0

我有两个不同的转换。第一次转换后需要应用但不能执行。我已经引用了其他stackoverflow帖子,但它不适用于我。

样本输入:

<Para>1<AAA>2<BBB>3</BBB>4</AAA>5</Para>

要求就像BBB也可以在AAA之外使用。我们需要删除所有AAA并以逗号分隔格式放置值。

First:

之后的预期输出
 <Para>12<BBB>3</BBB>45</Para>

第二个预期输出:

<Para>12,3,45</Para>

首先:

这里我只是从内容中删除标记AAA。并重新检索其内容和子项。

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

   <xsl:template match ="*/AAA">
    <xsl:apply-templates/>
  </xsl:template>

第二

这里我只是通过删除para中的所有标记并检索其内容来在节点元素之间添加逗号。

<xsl:param name="Para"></xsl:param>
<xsl:template match="Para">
    <xsl:copy>
       <xsl:variable name="BBB-element-exists">
            <xsl:for-each select="node()[self::BBB ][normalize-space(.)!='']">
            <xsl:if test="(self::Change and (child::BBB)) or (self::BBB)">
            <xsl:value-of select="'BBBexists'"/>
            </xsl:if>
            </xsl:for-each>
            </xsl:variable>
            <xsl:if test="$BBB-element-exists = 'BBBexists'">
                <xsl:for-each select=".//text()">
                <xsl:if test="position() >1 and not(parent::Change) ">
                <xsl:value-of select="','" />
                </xsl:if>
                <xsl:value-of select="normalize-space()" />
                </xsl:for-each>
            </xsl:if>
            <xsl:if test="$BBB-element-exists != 'BBBexists'">
             <xsl:for-each select=".//text()">
                <xsl:value-of select="normalize-space()" />
                </xsl:for-each>
            </xsl:if>
    </xsl:copy>
</xsl:template>

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

1 个答案:

答案 0 :(得分:0)

您需要使用至少一种模式将第二步的处理与第一步分开:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

   <xsl:template match="*/AAA">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:variable name="first-pass">
      <xsl:apply-templates select="node()"/>
  </xsl:variable>

  <xsl:template match="/">
      <xsl:apply-templates select="$first-pass/node()" mode="step2"/>
  </xsl:template>

<xsl:template match="Para" mode="step2">
    <xsl:copy>
       <xsl:variable name="BBB-element-exists">
            <xsl:for-each select="node()[self::BBB ][normalize-space(.)!='']">
            <xsl:if test="(self::Change and (child::BBB)) or (self::BBB)">
            <xsl:value-of select="'BBBexists'"/>
            </xsl:if>
            </xsl:for-each>
            </xsl:variable>
            <xsl:if test="$BBB-element-exists = 'BBBexists'">
                <xsl:for-each select=".//text()">
                <xsl:if test="position() >1 and not(parent::Change) ">
                <xsl:value-of select="','" />
                </xsl:if>
                <xsl:value-of select="normalize-space()" />
                </xsl:for-each>
            </xsl:if>
            <xsl:if test="$BBB-element-exists != 'BBBexists'">
             <xsl:for-each select=".//text()">
                <xsl:value-of select="normalize-space()" />
                </xsl:for-each>
            </xsl:if>
    </xsl:copy>
</xsl:template>
</xsl:transform>