如何处理“<BR/>”元素并在新段落中转换字符串

How to handle '<BR/>' element and convert string in new paragraph

在下面的示例中,我们尝试处理 'P' 元素下的 'BR' 元素,并使用 'XSLT 1.0':

转换为单独的段落

谁能帮忙。

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<CONTENT>
<P>A. 05° 55’ 47.81” S – 106° 32’ 10.76” E<BR/>B. 05° 55’ 47.81” S – 106° 34’ 10.76” E<BR/>C. 05° 55’ 47.81” S – 106° 36’ 10.76” E<BR/>D. 05° 57’ 47.81” S – 106° 32’ 10.76” E<BR/>E. 05° 57’ 47.81’’S – 106° 34’ 10.76’’E<BR/>F. 05° 57’ 47.81’’S – 106° 36’ 10.76’’E<BR/>G. 05° 59’ 47.81’’S – 106° 32’ 10.76’’E<BR/>H. 05° 59’ 47.81’’S – 106° 34’ 10.76’’E<BR/>I. 05° 59’ 47.81’’S – 106° 36’ 10.76’’E</P>
</CONTENT>

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<CONTENT>
<P>A. 05° 55’ 47.81” S – 106° 32’ 10.76” E</P>
<P>B. 05° 55’ 47.81” S – 106° 34’ 10.76” E</P>
<P>C. 05° 55’ 47.81” S – 106° 36’ 10.76” E</P>
<P>D. 05° 57’ 47.81” S – 106° 32’ 10.76” E</P>
<P>E. 05° 57’ 47.81’’S – 106° 34’ 10.76’’E</P>
<P>F. 05° 57’ 47.81’’S – 106° 36’ 10.76’’E</P>
<P>G. 05° 59’ 47.81’’S – 106° 32’ 10.76’’E</P>
<P>H. 05° 59’ 47.81’’S – 106° 34’ 10.76’’E</P>
<P>I. 05° 59’ 47.81’’S – 106° 36’ 10.76’’E</P>
</CONTENT>

XSLT 代码:

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

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

</xsl:stylesheet>

参考Link:https://xsltfiddle.liberty-development.net/bEJbVso/1

试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  >
  
  <xsl:template match="P[BR]">
    <p><xsl:value-of select="BR[1]/preceding-sibling::text()"/></p>
    <xsl:apply-templates select="BR"/>
  </xsl:template>
  
  <xsl:template match="P/BR[following-sibling::BR]">
    <p><xsl:value-of select="following-sibling::BR[1]/preceding-sibling::node()[1][self::text()]"/></p>
  </xsl:template>

  <xsl:template match="P/BR[not(following-sibling::BR)]">
    <p><xsl:value-of select="following-sibling::text()"/></p>
  </xsl:template>

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

</xsl:stylesheet>

we are trying to handle 'BR' element under the 'P' element and convert into the seprate paragraph

实际上,您 根本不需要处理 BR 个元素。

您需要做的就是解决 单独的文本节点 (已被 BR 元素分隔)并将其中的每一个转换为单独的 P元素:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/CONTENT">
    <xsl:copy>
        <xsl:for-each select="P/text()">
            <P>
                <xsl:value-of select="."/>
            </P>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>