Xml 匹配的p元素丢失,标记我的句子

Xml 匹配的p元素丢失,标记我的句子,xml,xslt,Xml,Xslt,我有一个问题,我想它相当简单,但我不能绕着它转 我有一个包含多个文本元素的大型XML。我想在它们上匹配一个模板,但这会删除带有属性的p元素,这只会导致纯文本。 文件应以“!”标记,并保留…。它还删除了我的中的一半句子,我也不想这样做 输入: <root> <text> <body> <div> <p facs="001">Hello Guys! This is my example! T

我有一个问题,我想它相当简单,但我不能绕着它转

我有一个包含多个文本元素的大型XML。我想在它们上匹配一个模板,但这会删除带有属性的p元素,这只会导致纯文本。 文件应以“!”标记,并保留

。它还删除了我的
中的一半句子,我也不想这样做

输入:

<root>
 <text>
    <body>
        <div>
            <p facs="001">Hello Guys! This is my example! Thanks for your time!</p>
        </div>
        <div>
            <p facs="002">Some more text! And a little more!</p>
        </div>
        <div>
            <p facs="003">Here as well! See you later!</p>
        </div>
    </body>
 </text>
</root>

大家好!这就是我的例子!谢谢你的时间

更多文本!还有一点

这里也是!再见

我的XSLT

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

    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="/root/text/body/div/p">
            <xsl:variable name="tokens" select="tokenize(text(),'!')" as="xs:string*"/>
            <xsl:variable name="words" select="remove($tokens, 1)" as="xs:string*"/>
            <xsl:for-each select="1 to xs:integer(floor(count($words) div 1))">
                <xsl:variable name="vIndex" select="(.)" as="xs:integer"/>
                <w><xsl:attribute name="n"
                    select="position()"/>
                    <xsl:value-of select="normalize-space($words[$vIndex])"/>
                </w>
            </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我的输出:

 <root>
       <text>
          <body>
            <div>
                   <w n="1">This is my example</w>
                   <w n="2">Thanks for your time</w>
                   <w n="3"/>
            </div>
            <div>
                   <w n="1">And a little more</w>
                   <w n="2"/>
            </div>
            <div>
                   <w n="1">See you later</w>
                   <w n="2"/>
            </div>
          </body>
       </text>
    </root>

这是我的例子
谢谢你的时间
还有一点
回头见
我想要的输出:

 <root>
       <text>
          <body>
            <div>
                <p facs="001">
                   <w n="1">Hello Guys</w>
                   <w n="2">This is my example</w>
                   <w n="3">Thanks for your time</w>
                </p>
            </div>
            <div>
                <p facs="002">
                   <w n="1">Some more Text</w>
                   <w n="2">And a little more</w>
                </p>
            </div>
            <div>
                <p facs="003">
                   <w n="1">Here as well</w>
                   <w n="2">See you later</w>
                </p>
            </div>
          </body>
       </text>
    </root>

大家好 这是我的例子 谢谢你的时间

更多的文字 还有一点

这里也是 回头见

此外,即使没有必要,我想知道是否有办法保持“!”I标记化。我怎样才能保存它们

简言之: a) 我不想删除我的facs属性 b) 我不想失去第一句话 c) 如何保存我标记的角色?在这个例子中“!”


非常感谢

要保留
p
元素,您只需要在其模板中有一个
xsl:copy

<xsl:template match="/root/text/body/div/p">
   <xsl:copy>
     <xsl:copy-of select="@*"/>
        <xsl:variable name="tokens" select="tokenize(text(),'!')" as="xs:string*"/>
        <xsl:variable name="words" select="remove($tokens, 1)" as="xs:string*"/>
        <xsl:for-each select="1 to xs:integer(floor(count($words) div 1))">
            <xsl:variable name="vIndex" select="(.)" as="xs:integer"/>
            <w><xsl:attribute name="n"
                select="position()"/>
                <xsl:value-of select="normalize-space($words[$vIndex])"/>
            </w>
        </xsl:for-each>
   </xsl:copy>
</xsl:template>
这样输入

<root>
 <text>
    <body>
        <div>
            <p facs="001">Hello Guys! This is my example! Thanks for your time!</p>
        </div>
        <div>
            <p facs="002">Some more text! And a little more!</p>
        </div>
        <div>
            <p facs="003">Here as well! See you later!</p>
        </div>
    </body>
 </text>
</root>

大家好!这就是我的例子!谢谢你的时间

更多文本!还有一点

这里也是!再见

转化为结果

<root>
   <text>
      <body>
        <div>
            <p facs="001">
               <w n="1">Hello Guys</w>
               <w n="2"> This is my example</w>
               <w n="3"> Thanks for your time</w>
            </p>
        </div>
        <div>
            <p facs="002">
               <w n="1">Some more text</w>
               <w n="2"> And a little more</w>
            </p>
        </div>
        <div>
            <p facs="003">
               <w n="1">Here as well</w>
               <w n="2"> See you later</w>
            </p>
        </div>
      </body>
   </text>
</root>

大家好 这是我的例子 谢谢你的时间

更多的文字 还有一点

这里也是 回头见

<root>
 <text>
    <body>
        <div>
            <p facs="001">Hello Guys! This is my example! Thanks for your time!</p>
        </div>
        <div>
            <p facs="002">Some more text! And a little more!</p>
        </div>
        <div>
            <p facs="003">Here as well! See you later!</p>
        </div>
    </body>
 </text>
</root>
<root>
   <text>
      <body>
        <div>
            <p facs="001">
               <w n="1">Hello Guys</w>
               <w n="2"> This is my example</w>
               <w n="3"> Thanks for your time</w>
            </p>
        </div>
        <div>
            <p facs="002">
               <w n="1">Some more text</w>
               <w n="2"> And a little more</w>
            </p>
        </div>
        <div>
            <p facs="003">
               <w n="1">Here as well</w>
               <w n="2"> See you later</w>
            </p>
        </div>
      </body>
   </text>
</root>