1

我有一个看起来像这样的 xml 文档:

<div>
  <p>Hello my name is Bob <cb ed="#R" n="1/>and I live in a house</p>
  <p>My name is Susan.</p>
  <p>Where are you from?</p>
  <p>I am from <cb ed="#R" n="2/>Chicago, Illinois</p>
  <p>I also live in Chicago</p>
  <p>But I wish I <cb ed="#R" n="3"/>lived in New York</p>
</div>

等等 ...

我基本上想对其进行转换,以便标签围绕第一个和第二个之间的所有内容等等......但我也想保留新创建的 div 中的现有段落。这也意味着创建一个

文本节点周围的标记,该节点是该元素的紧随其后的兄弟。

我希望结果看起来像:

   <div id="1">
     <p>and I live in a house</p>
     <p>My name is Susan.</p>
     <p>Where are you from?</p>
     <p>I am from</p> 
   </div>
   <div id="2">
     <p>Chicago, Illinois</p>
     <p>I also live in Chicago</p>
     <p>But I wish I</p> 
   </div>
   <div id="3">
     <p>lived in New York</p>
   </div>

事实证明这是相当困难的。我想知道是否有人可以帮助我走上正确的道路——或者给我指出一个类似转变的例子。

这是我到目前为止所拥有的:

    <xsl:template match="tei:p">
        <xsl:choose>
            <xsl:when test="./tei:cb[@ed='#R']">
                <xsl:variable name="number" select="./tei:cb[@ed='#R']/@n"/>
                <div id="{$number}">
                    <span>test</span>
                    <xsl:for-each select="./tei:cb[@ed='#R']/following::p[preceding::tei:cb[@ed='#R']]">
                        <p><xsl:value-of select="."/></p>
                    </xsl:for-each>
                </div>
            </xsl:when>
            <xsl:otherwise>

            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

到目前为止,唯一的结果是:

     <div id="1rb"><span>test</span></div>
     <div id="1va"><span>test</span></div>
     <div id="1vb"><span>test</span></div>
4

2 回答 2

1

以下对我有用。它使用计数技术来查找两个cb元素之间的文本节点。

<xsl:template match="div">
    <xsl:apply-templates select=".//cb"/>
</xsl:template>

<xsl:template match="cb">
    <div id="{@n}">
        <xsl:variable name="numTextNodes" select="count(following::text()) - count(following::cb/following::text())"/>
        <xsl:for-each select="following::text()[position() &lt;= $numTextNodes]">
            <xsl:if test="normalize-space()">
                <p>
                    <xsl:value-of select="normalize-space()"/>
                </p>
            </xsl:if>
        </xsl:for-each>
    </div>
</xsl:template>
于 2013-06-11T21:05:12.883 回答
1

假设<cb>总是在同一级别(内部<p>)发生,这或多或少类似于“从平面 xml 创建分层 xml”,或者可能看一下Kayessian 方法

尝试这样的事情(仍然可以改进):

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="p[cb]" >
        <!-- following siblings including next p[cb]  -->
        <xsl:variable name="fs" select="following-sibling::*[ 
                                             generate-id( preceding-sibling::p[cb][1]
                                          ) = generate-id(current()) ]" />

        <div id="{cb/@n}">
            <xsl:apply-templates select="cb" mode="start"/>
            <xsl:apply-templates select="$fs[position() != last()]" />
            <xsl:apply-templates select="$fs[last()]" mode="stop"/>
        </div>
    </xsl:template>

    <xsl:template match="p[cb]" mode="stop">
        <p>
            <xsl:copy-of select="cb/preceding-sibling::node()"/>
        </p>
    </xsl:template>

    <xsl:template match="cb[parent::p]" mode="start">
        <p>
            <xsl:copy-of select="following-sibling::node()"/>
        </p>
    </xsl:template>

    <xsl:template match="div[p/cb]">
        <xsl:copy>
            <xsl:apply-templates select="p[cb][1]" mode="stop"/>
            <xsl:apply-templates select="p[cb]"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

这将生成以下输出:

  <div>
    <p>Hello my name is Bob </p>
    <div id="1">
      <p>and I live in a house</p>
      <p>My name is Susan.</p>
      <p>Where are you from?</p>
      <p>I am from </p>
    </div>
    <div id="2">
      <p>Chicago, Illinois</p>
      <p>I also live in Chicago</p>
      <p>But I wish I </p>
    </div>
    <div id="3">
      <p>lived in New York</p>
    </div>
  </div>
于 2013-06-12T07:37:06.617 回答