通过XSLT中的文本处理创建新元素

时间:2010-07-12 23:42:48

标签: xml xslt

我想知道是否/如何在XSLT中完成以下操作。如果没有,你会用什么? (我使用过OmniMark,但我想知道这是否可以在XSLT中使用。)

以下是输入XML的示例:

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
    <firstElem>
        <secondElem>D12</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J7-10</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C2-4</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP20-25</secondElem>
    </firstElem>
</fragment>

我需要做的是获取secondElem元素的文本内容,并根据需要创建尽可能多的新firstElem元素。 ' - '是一个“通过”,所以'A3J7-10'实际上是'A3J7'到'A3J10'(A3J7,A3J8,A3J9,A3J10)。 (有时候“穿透”会相当大,比如A1B2C1-150(A1B2C1到A1B2C150)。)

如果没有破折号,则无需任何操作。

以下是输出XML的示例:

<?xml version="1.0" encoding="UTF-8"?>
<fragment>
    <firstElem>
        <secondElem>D12</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J7</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J8</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J9</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J10</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C2</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C3</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C4</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP20</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP21</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP22</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP23</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP24</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP25</secondElem>
    </firstElem>
</fragment>

有没有办法在XSLT中执行此操作?

谢谢!

2 个答案:

答案 0 :(得分:2)

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="firstElem[contains(.,'-')]" name="firstElem">
        <xsl:param name="from">
            <xsl:call-template name="getfrom"/>
        </xsl:param>
        <xsl:param name="base" select="normalize-space(substring-before(.,concat($from,'-')))"/>
        <xsl:param name="to" select="substring-after(.,'-')"/>
        <xsl:if test="$to >= $from">
            <firstElem>
                <secondElem>
                    <xsl:value-of select="concat($base,$from)"/>
                </secondElem>
            </firstElem>
            <xsl:call-template name="firstElem">
                <xsl:with-param name="from" select="$from + 1"/>
                <xsl:with-param name="base" select="$base"/>
                <xsl:with-param name="to" select="$to"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template name="getfrom">
        <xsl:param name="string" select="substring-before(.,'-')"/>
        <xsl:variable name="last" select="substring($string,string-length($string))"/>
        <xsl:if test="contains('0123456789',$last)">
            <xsl:call-template name="getfrom">
                <xsl:with-param name="string" select="substring($string,1,string-length($string)-1)"/>
            </xsl:call-template>
            <xsl:value-of select="$last"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出:

<fragment>
    <firstElem>
        <secondElem>D12</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J7</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J8</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J9</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J10</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C2</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C3</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C4</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP20</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP21</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP22</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP23</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP24</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP25</secondElem>
    </firstElem>
</fragment>

答案 1 :(得分:1)

这是一个XSLT 2.0解决方案

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="firstElem[contains(secondElem, '-')]">
   <xsl:analyze-string select="secondElem"
    regex="(^.*[^\d])([\d]*)-([\d]*)">

     <xsl:matching-substring>
        <xsl:variable name="vbaseName" select="regex-group(1)"/>
        <xsl:variable name="vstart" select="regex-group(2)"/>
        <xsl:variable name="vend" select="regex-group(3)"/>

        <xsl:for-each select=
          "xs:integer($vstart) to xs:integer($vend)">
                <firstElem>
                    <secondElem>
                      <xsl:value-of select="concat($vbaseName, .)"/>
                    </secondElem>
                </firstElem>
        </xsl:for-each>
     </xsl:matching-substring>
   </xsl:analyze-string>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用上述转换时

<fragment>
    <firstElem>
        <secondElem>D12</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>A3J7-10</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>C2-4</secondElem>
    </firstElem>
    <firstElem>
        <secondElem>QW9R7NP20-25</secondElem>
    </firstElem>
</fragment>

产生了想要的正确结果

<fragment>
    <firstElem>
        <secondElem>D12</secondElem>
    </firstElem>
    <firstElem>
      <secondElem>A3J7</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>A3J8</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>A3J9</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>A3J10</secondElem>
   </firstElem>
    <firstElem>
      <secondElem>C2</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>C3</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>C4</secondElem>
   </firstElem>
    <firstElem>
      <secondElem>QW9R7NP20</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>QW9R7NP21</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>QW9R7NP22</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>QW9R7NP23</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>QW9R7NP24</secondElem>
   </firstElem>
   <firstElem>
      <secondElem>QW9R7NP25</secondElem>
   </firstElem>
</fragment>

请注意

  1. 使用正则表达式。

  2. 子表达式捕获和regex-group()函数。

  3. 使用<xsl:analyze-string><xsl:matching-substring>

  4. 使用to运算符创建要在<xsl:for-each>中使用的序列

  5. 在一系列非节点(整数)上使用<xsl:for-each>