【问题标题】:Removing the XML definition from XML从 XML 中删除 XML 定义
【发布时间】:2020-05-17 16:56:36
【问题描述】:

我需要禁用输出转义并删除 XML 定义的出现,即 xml version="1.0" encoding="UTF-8"?>

输入数据:

<ns1:outSystemWSResponse xmlns:ns1='http://abcd.co.za'
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
    <ns1:out>&#60;?xml version="1.0" encoding = "UTF-8"?&#62;&#60;PQR&#62;
        &#60;STU&#62;
        &#60;TEST1&#62;Pen&#60;/TEST1&#62;
        &#60;TEST2&#62;Table&#60;/TEST2&#62;
        &#60;/STU&#62;
        &#60;/PQR&#62;</ns1:out>
</ns1:outSystemWSResponse>

预期输出

<?xml version="1.0" encoding="UTF-8"?>
<ns1:outSystemWSResponse xmlns:ns1="http://abcd.co.za"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ns1:out>
        <PQR>
            <STU>
                <TEST1>Pen</TEST1>
                <TEST2>Table</TEST2>
            </STU>
        </PQR>
    </ns1:out>
</ns1:outSystemWSResponse>

我尝试使用以下 XSLT,但不确定如何删除 xml version="1.0" encoding="UTF-8"?>。请帮忙

<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:strip-space elements="*"/>

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

<xsl:template match="ns:out" xmlns:ns="http://abcd.co.za">
    <xsl:copy>
        <xsl:value-of select="." disable-output-escaping="yes"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    我同意 Martin Honnen 的观点,即最好的解决方案是将转义文本转换为 XML,然后使用适当的 XML 解析器对其进行解析。

    如果您仅限于 XSLT 1.0 或 2.0,这意味着仅将 ns1:out 的内容输出到另一个文件并禁用转义,然后使用另一个样式表处理生成的文件。

    不过,在紧要关头你可以这样做:

    <xsl:template match="ns:out" xmlns:ns="http://abcd.co.za">
        <xsl:copy>
            <xsl:value-of select="substring-after(., '?>')" disable-output-escaping="yes"/>
        </xsl:copy>
    </xsl:template>
    

    【讨论】:

    • 如何为输出 XML 中的所有元素添加命名空间前缀?
    • 这是一个单独的问题 - 它已经得到了回答(我想不止一次)。例如:stackoverflow.com/a/56471643/3016153 或:stackoverflow.com/a/59096005/3016153
    • 忘了说,非常感谢上面的 XSLT。它按我的预期工作。现在我只需要在这个 XSLT 的输出中添加命名空间前缀。请帮助我实现那部分。例如:我需要 而不是
    • @Nitin 如果按预期工作,请关闭此问题。如果您无法自己解决其他不相关的问题,请发布一个新问题。
    • 你去!无论如何谢谢:)
    【解决方案2】:

    在这个问题中,您需要 serialize 函数的对应物,即 parse-xml 函数,它也可用于 XPath 3 和 XSLT 3:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
    
      <xsl:mode on-no-match="shallow-copy"/>
    
      <xsl:template match="ns1:out" xmlns:ns1="http://abcd.co.za">
          <xsl:copy>
              <xsl:apply-templates select="parse-xml(.)"/>
          </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/a9GPfX

    或者检查是否有特定于处理器的扩展来解析 XML。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-05
      • 2016-12-07
      • 2014-10-05
      • 1970-01-01
      • 2016-01-30
      • 2011-08-02
      • 2016-02-18
      • 2010-09-28
      相关资源
      最近更新 更多