结合深度节点

时间:2012-09-01 16:40:11

标签: xslt-1.0 xslt-2.0

我对如何最好地遵循以下XML示例感到困惑:我的内容如下:

<Story>
<Content para="div"><local>This is some (normal) text to start with.</local></Content>
<Content para="div"><local>Connect something (</local></Content>
<Content para="div"><local><refnr value="58236"/></local></Content>
<Content para="div"><local>) to something else (</local></Content>
<Content para="div"><local><refnr value="58237"/></local></Content>
<Content para="div"><local>), and make sure it's connected to this (</local></Content>
<Content para="div"><local><refnr value="58239"/></local></Content>
<Content para="div"><local>).</local></Content>
<Content para="div"><local>If that's ok do the same with this (</local></Content>
<Content para="div"><local><refnr value="58238"/></local></Content>
<Content para="div"><local>) also.</local></Content>
<Content para="div"><local>This is some normal text.</local></Content>
<Content para="div"><local>This also.</local></Content>
</Story>

我希望获得的输出如下:

<Story>
<Content para="div"><local>This is some (normal) text to start with.</local></Content>
<Content para="div"><local>Connect something (<refnr value="58236"/>) to something else (<refnr value="58237"/>), and make sure it's connected to this (<refnr value="58239"/>).</local></Content>
<Content para="div"><local>If that's ok do the same with this (<refnr value="58238"/>) also.</local></Content>
<Content para="div"><local>This is some normal text.</local></Content>
<Content para="div"><local>This also.</local></Content>
</Story>

或者,'代码'它:任何[Content para = div] [local]节点以开括号结尾,需要与以下[Content para = div] [local]节点合并,直到包含该节点的节点最后的结束括号和句子的结尾(由关闭点标识)。我能够做一些事情,但它变得过于复杂和缓慢,没有所有想要的结果。任何使用xslt2的建议?

2 个答案:

答案 0 :(得分:1)

我认为你需要嵌套两种方法:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="Story">
  <xsl:copy>
    <xsl:for-each-group select="Content" group-adjacent="contains(., '(') or contains(., ')') or boolean(self::Content[local[refnr]])">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <xsl:for-each-group select="current-group()" group-ending-with="Content[local[ends-with(., '.')]]">
            <Content para="div">
              <local>
                <xsl:copy-of select="current-group()/local/node()"/>
              </local>
            </Content>
          </xsl:for-each-group>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

使用该样式表和您的示例输入,我得到您发布的结果。但我不确定我使用的条件是否对于实际输入来说太简单了,请报告是否可以存在包含refnr元素的“普通”文本。

[编辑] 使用

中的matches
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="Story">
  <xsl:copy>
    <xsl:for-each-group select="Content" 
      group-adjacent="not(matches(., '\(.*\)')) 
                       or boolean(self::Content[local[refnr]])">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <xsl:for-each-group select="current-group()" group-ending-with="Content[local[ends-with(., '.')]]">
            <Content para="div">
              <local>
                <xsl:copy-of select="current-group()/local/node()"/>
              </local>
            </Content>
          </xsl:for-each-group>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

我认为编辑后的输入样本会根据需要进行转换。当然,一旦你有了例如<Content para="div"><local>Connect (foo) something (</local></Content>这种方法也太简单了。

答案 1 :(得分:1)

此XSLT 2.0转换(可以轻松转换为XSLT 1.0):

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

 <xsl:template match="/*">
  <Story>
   <xsl:apply-templates select="Content[1]/local"/>
  </Story>
 </xsl:template>

 <xsl:template match="Content/local[not(ends-with(., '('))]">
  <Content para="div"><local><xsl:apply-templates/></local></Content>
  <xsl:apply-templates select="../following-sibling::Content[1]"/>
 </xsl:template>

 <xsl:template match="Content/local[ends-with(., '(')]">
  <Content para="div"><local><xsl:apply-templates/>
   <xsl:apply-templates mode="inGroup" select="../following-sibling::Content[1]"/>
  </local></Content>

  <xsl:apply-templates select=
   "../following-sibling::Content
             [local[starts-with(.,')') and ends-with(.,'.')]][1]
               /following-sibling::Content[1]"/>
 </xsl:template>

 <xsl:template match="Content/local" mode="inGroup">
    <xsl:apply-templates/>
    <xsl:apply-templates mode="#current"
         select="../following-sibling::Content[1]"/>
 </xsl:template>

 <xsl:template mode="inGroup" match="Content/local[starts-with(.,')') and ends-with(.,'.')]">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="refnr"><xsl:copy-of select="."/></xsl:template>
</xsl:stylesheet>

应用于提供的XML文档

<Story>
 <Content para="div"><local>This is some (normal) text to start with.</local></Content>
 <Content para="div"><local>Connect something (</local></Content>
 <Content para="div"><local><refnr value="58236"/></local></Content>
 <Content para="div"><local>) to something else (</local></Content>
 <Content para="div"><local><refnr value="58237"/></local></Content>
 <Content para="div"><local>), and make sure it's connected to this (</local></Content>
 <Content para="div"><local><refnr value="58239"/></local></Content>
 <Content para="div"><local>).</local></Content>
 <Content para="div"><local>If that's ok do the same with this (</local></Content>
 <Content para="div"><local><refnr value="58238"/></local></Content>
 <Content para="div"><local>) also.</local></Content>
 <Content para="div"><local>This is some normal text.</local></Content>
 <Content para="div"><local>This also.</local></Content>
</Story>

生成想要的正确结果

<Story>
   <Content para="div">
      <local>This is some (normal) text to start with.</local>
   </Content>
   <Content para="div">
      <local>Connect something (<refnr value="58236"/>) to something else (<refnr value="58237"/>), and make sure it's connected to this (<refnr value="58239"/>).</local>
   </Content>
   <Content para="div">
      <local>If that's ok do the same with this (<refnr value="58238"/>) also.</local>
   </Content>
   <Content para="div">
      <local>This is some normal text.</local>
   </Content>
   <Content para="div">
      <local>This also.</local>
   </Content>
</Story