XSLT 命名空间无法读取 ns0:

XSLT Namespace Unable to read ns0:

我是 XML 和 XSLT 转换的完全新手。我已经创建了一个 XSLT 文件,它可以正确翻译客户给我的 XML 文件,但只有当我删除 ns0 行时它才会这样做——因为我无法控制我收到的文件,我需要能够要读取保留 ns0 行的文件,我查看了几个已回答的问题,这些问题似乎与同一问题有关,但我是个新手,我很难理解这些答案如何适用于我的问题, 详情如下:-

XML 文件开头为

<?xml version="1.0" encoding="UTF-8"?>

<ns0:MyRecord xmlns:ns0="https://some/kind/of/text">

并以 ns0:MyRecord

结尾

我已经编写了一个 XSLT(如下所示)来转换 XML,它只有在我删除

<?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" indent="yes"/>
<xsl:template match="/">

<ImportJobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <description>Jobs Import Test</description>
  <copyright>© A Software Company Ltd 2015</copyright>
  <Exceptions></Exceptions>
  <DataItems>
    <xsl:for-each select="MyRecord/ImportJobs/DataItems/Job">
      <Job>
        <CustomerAlpha>
          <xsl:text>AnAccountNo</xsl:text>
        </CustomerAlpha>
        <SiteAlpha>
          <xsl:text></xsl:text>
        </SiteAlpha>
        <EquipNo>
          <xsl:text></xsl:text>
        </EquipNo>
        <Authority>
          <xsl:text></xsl:text>
        </Authority>
        <Reference>
          <xsl:text></xsl:text>
        </Reference>
    <UserRef2>
          <xsl:value-of select="JobNumber"/>
        </UserRef2>
    </xsl:for-each>
  </DataItems>

</ImportJobs>

</xsl:template>
</xsl:stylesheet>

我想我应该添加 xmlns:ns0="https://some/kind/of/text" 但即使我添加了也不允许 XSLT 查看数据

无论我尝试什么,我只会得到

<?xml version="1.0" encoding="utf-8"?>
<ImportJobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <description>Jobs Import Test</description>
  <copyright>© A Software Company Ltd 2015</copyright>
  <Exceptions />
  <DataItems />
</ImportJobs>

您的指令:

<xsl:for-each select="MyRecord/ImportJobs/DataItems/Job">

select没什么,因为 MyRecord 元素在命名空间中。为了 select 它,您需要在样式表中声明相同的命名空间,为其分配一个前缀并在 select 表达式中使用该前缀 - 例如:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="https://some/kind/of/text"
exclude-result-prefixes="ns0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
    <ImportJobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <!-- ... -->
        <DataItems>
            <xsl:for-each select="ns0:MyRecord/ImportJobs/DataItems/Job">
                <!-- ... -->
            </xsl:for-each>
        </DataItems>
    </ImportJobs>
</xsl:template>

</xsl:stylesheet>

这是假设后代节点 - ImportJobsDataItemsJob - 不在命名空间中 - 否则它们也需要前缀。