从XML文件中删除所有标记

Ham*_*han 4 php xml asp.net xslt

我有一个大的XML文件,我想剥离所有标签,只留下节点值.我希望每个节点值都在一个单独的行中.我怎样才能做到这一点?

我可以使用免费软件来执行此操作或使用PHP或ASP.NET代码.我也查看了XSLT选项.RegEX可能太过分了.我探索PHP选项看着simplexml_load_file(),strip_tags(),get_file_contents()但未能成功.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- a comment -->
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
                <address>
                         <city>Melbourne </city>
                         <zip>01803 </zip>
                </address>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>

</catalog>
Run Code Online (Sandbox Code Playgroud)

编辑:这是我尝试过的,除其他外.

<?php

$xml = simplexml_load_file('myxml.xml');
echo strip_tags($xml);

?>
Run Code Online (Sandbox Code Playgroud)

Con*_*eet 5

这应该是你做的:

<?php
$xml = file_get_contents('myxml.xml');
$xml = nl2br($xml);
echo strip_tags($xml,"<br>");
?>
Run Code Online (Sandbox Code Playgroud)

您缺少换行符的原因是因为在XML中,它存储为纯文本换行符,\n而在以HTML格式显示时,您必须具有明确的<br>换行符.因此,优秀的PHP人员nl2br()为您做了一个方便的功能.


Dim*_*hev 5

这是一个简短的 XSLT 解决方案

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

 <xsl:template match="text()">
  <br /><xsl:value-of select="concat(.,'&#xA;')"/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于提供的 XML 文档时(适用于任何XML 文档):

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <address>
            <city>Melbourne </city>
            <zip>01803 </zip>
        </address>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)

产生了想要的结果:

<br/>Empire Burlesque
<br/>Bob Dylan
<br/>USA
<br/>Columbia
<br/>10.90
<br/>Melbourne 
<br/>01803 
<br/>1985
<br/>Hide your heart
<br/>Bonnie Tyler
<br/>UK
<br/>CBS Records
<br/>9.90
<br/>1988
Run Code Online (Sandbox Code Playgroud)

并且在浏览器中显示为:


帝国滑稽表演
鲍勃·迪伦
美国
哥伦比亚
10.90
墨尔本
01803
1985
隐藏你的心
邦妮·泰勒
英国
CBS 唱片
9.90
1988