Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何处理'&书信电报;BR/>';元素并转换新段落中的字符串_Xml_Xslt_Xpath_Xslt 1.0_Xquery - Fatal编程技术网

Xml 如何处理'&书信电报;BR/>';元素并转换新段落中的字符串

Xml 如何处理'&书信电报;BR/>';元素并转换新段落中的字符串,xml,xslt,xpath,xslt-1.0,xquery,Xml,Xslt,Xpath,Xslt 1.0,Xquery,在下面的示例中,我们尝试在“p”元素下处理“BR”元素,并使用“XSLT 1.0”将其转换为seprate段落: 有人能帮忙吗 输入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

在下面的示例中,我们尝试在“p”元素下处理“BR”元素,并使用“XSLT 1.0”将其转换为seprate段落:

有人能帮忙吗

输入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>
<?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>

A.05°55'47.81“S–106°32'10.76”E
B.05°55'47.81“S–106°34'10.76”E
C.05°55'47.81“S–106°36'10.76”E
D.05°57'47.81“S–106°32'10.76”E
E.05°57'47.81“S–106°34'10.76”E
。05°59'47.81'S–106°32'10.76'E
H.05°59'47.81'S–106°34'10.76'E
I.05°59'47.81'S–106°36'10.76'E

预期输出:

<?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>
<?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>

A.05°55'47.81英寸南-106°32'10.76英寸东

B.05°55'47.81英寸南-106°34'10.76英寸东

C.05°55'47.81英寸南–106°36'10.76英寸东

D.05°57'47.81英寸南–106°32'10.76英寸东

E.05°57'47.81'S–106°34'10.76'E

F.05°57'47.81'S–106°36'10.76'E

G.05°59'47.81'S–106°32'10.76'E

H.05°59'47.81'S–106°34'10.76'E

I.05°59'47.81'S–106°36'10.76'E

XSLT代码:

<?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>
<?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>

参考链接:

试试这个:

<?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>

我们正在尝试在“p”元素下处理“BR”元素,并将其转换为seprate段落

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

您只需寻址单个文本节点(已由
BR
元素分隔),并将每个节点转换为单独的
p
元素:

XSLT1.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>


为什么需要标记
?@PierreFrançois来创建根
内容
元素。--另外,这是说明,不是标签。回答得很好。但是为什么每个都使用。我已经学会了尽可能多地使用应用模板@SiebeJongebloed您学错了。与之相比,此模板有三个几乎相同但不完全相同的模板:在有时需要更新的较长样式表中,它们可能很难维护(典型情况:您记得修复一两个,而忘记了其他模板).他的答案最容易理解,也可能最容易维护…这取决于是否引入了其他内联元素。如果两个br之间没有文本,并且在最后一个br之后没有文本节点,那么我的xslt也将创建空p。