【问题标题】:XSL can't read into a xml with the tag "Document xmlns="urn:iso:st"XSL 无法读入带有标签“Document xmlns="urn:iso:st" 的 xml
【发布时间】:2014-02-19 14:40:27
【问题描述】:

我需要帮助才能读入具有此定义的 xml 文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CstmrCdtTrfInitn>
        <GrpHdr>
            <NbOfTxs>3</NbOfTxs>

问题是当 xml 有 Document xmlns 时我无法读取节点...(我测试删除此行并且我可以读取节点)

我的 xsl 是这样的:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="yes"/>
       <xsl:template match='GrpHdr'>
           <NbOfTxs><xsl:value-of select="NbOfTxs"/></NbOfTxs>
       </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt tags xslt-1.0 document


    【解决方案1】:

    输入 XML 中的元素具有默认命名空间。您还需要在 XSLT 样式表中声明此命名空间,并在 prefix 任何要匹配的输入元素中声明。

    如果您想输出NbOfTxs 元素及其内容,则无需匹配GrpHdr 元素。

    输入

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <CstmrCdtTrfInitn>
            <GrpHdr>
                <NbOfTxs>3</NbOfTxs>
            </GrpHdr>
        </CstmrCdtTrfInitn>
    </Document>
    

    样式表

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:nsa="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    
       <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="yes"/>
    
       <xsl:template match='nsa:NbOfTxs'>
          <xsl:copy>
             <xsl:value-of select="."/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    输出

    请注意,NbOfTxs 元素在输出中仍然有其命名空间(您没有说是否要保留它)。

    <NbOfTxs xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">3</NbOfTxs>
    

    【讨论】:

      【解决方案2】:

      为 XSLT 中的命名空间声明一个命名空间前缀,然后使用该前缀进行选择: 在这里查看类似的问题

      How to 'select' from XML with namespaces?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        • 2013-06-07
        相关资源
        最近更新 更多