XSLT Document()函数结果包含不需要的xmlns =“”

时间:2014-09-26 23:16:39

标签: xml xslt

XSLT大师,

我有一个XSLT,在输出中包含不需要的 xmlns =“”。我已经多次在这个网站上看到这个问题/问题,但是我似乎无法得到任何能够解决我正在做的事情的答案。我必须警告你,我是XSLT的新手,并不是很擅长。

我的XSLT从调用它的应用程序中收集一组数据,然后还使用document()来获取其他数据。这一切都很好,我只需要摆脱 xmlns =“”

document()调用的XML是

<?xml version="1.0" encoding="UTF-8"?><BiExport xmlns="">
<ExportData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Phone>8885551212</Phone>
<ClientRefCmtSuffix/>
<UDFData>
<DictionaryEntry>
<Key xsi:type="xsd:string">MyCompany Completed Date</Key>
<Value xsi:type="xsd:string">06/24/2013</Value>
</DictionaryEntry>
<DictionaryEntry>
<Key xsi:type="xsd:string">Rush</Key>
<Value xsi:type="xsd:string">0</Value>
</DictionaryEntry>
<DictionaryEntry>
<Key xsi:type="xsd:string">Suffix</Key>
<Value xsi:type="xsd:string">0000000000</Value>
</DictionaryEntry>
</UDFData>
<ReportCreateDate>2014-09-26T15:45:48.83952-07:00</ReportCreateDate>
</ExportData>
</BiExport>

是的,我知道它有<BiExport xmlns="">,我不能改变它,即使我在应用XSLT之前删除它也没有任何区别。

我的XSLT

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bean="http://www.MyCompany.com/eventgenerator/beans">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />

  <xsl:variable name="ReportCreateDate" select="bean:FileEvent/bean:Context[@Id='XPathStep']/bean:Item[@Name='ReportCreateDate']/@Value"/>
  <xsl:variable name="PDFFile" select="concat($PDFFilename, '.', $PDFFileExt)"/>
  <xsl:variable name="InputFile" select="bean:FileEvent/bean:Context[@Id='XPathStep']/bean:Item[@Name='InputFile']/@Value"/>
  <xsl:variable name="PDFFilename" select="bean:FileEvent/bean:Context[@Id='XSLTStep']/bean:Item[@Name='PDFFilename']/@Value"/>
  <xsl:variable name="InputFilePath" select="bean:FileEvent/bean:Context[@Id='ArchiveMessageStep']/bean:Item[@Name='ArchiveDirectory']/@Value"/>
  <xsl:variable name="InputFileString" select="concat($InputFilePath, '\', $PDFFilename, '.xml')"/>

  <xsl:template name="UDFs" match="document($InputFileString)/BiExport/ExportData/UDFData/DictionaryEntry">
    <xsl:param name="pKey" select="." />
    <Item name="{$pKey}">
      <xsl:value-of select="../Value"/>
    </Item>
  </xsl:template>

  <xsl:template match="/">
    <MDXPackage version="1.0" xmlns="http://www.MyCompany.com/schemas">
      <Payload>
        <PayloadContext>

          <xsl:apply-templates select="document($InputFileString)/BiExport/ExportData/UDFData/DictionaryEntry/Key" />

          <xsl:choose>
            <xsl:when test="string($ReportCreateDate)">
              <Item name="ReportCreateDate">
                <xsl:value-of select="$ReportCreateDate"/>
              </Item>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
          </xsl:choose>

          <xsl:choose>
            <xsl:when test="string($PDFFile)">
              <Item name="PDFFile">
                <xsl:value-of select="$PDFFile"/>
              </Item>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
          </xsl:choose>
          <!-- more of the same follows-->

        </PayloadContext>
      </Payload>
    </MDXPackage>
  </xsl:template>
</xsl:stylesheet>

输出

<MDXPackage xmlns="http://www.mitchell.com/schemas" version="1.0">
<Payload>
<PayloadContext>
<Item xmlns="" name="MyCompany Completed Date">06/24/2013</Item>
<Item xmlns="" name="Rush">0</Item>
<Item xmlns="" name="Suffix">0000000000</Item>
<Item name="ReportCreateDate">2014-09-26T13:53:28.3831684-07:00</Item>
<Item name="ClaimantPhone">8885551212</Item>
</PayloadContext>
</Payload>
</MDXPackage>

提前感谢您理解为什么我会获得空命名空间声明以及如何摆脱它。

DL

1 个答案:

答案 0 :(得分:4)

  

这一切都很好,我只需要摆脱xmlns =&#34;&#34;

这不是摆脱xmlns =&#34;&#34;。他们有充分的理由。原因是父元素MDXPackagePayloadPayloadContext都在名称空间("http://www.mitchell.com/schemas")和{{1}不是。

如果要删除无命名空间绑定,则必须将Items放在与其祖先相同的命名空间中。 可能意味着改变这一点:

Item

为:

  <Item name="{$pKey}">
    <xsl:value-of select="../Value"/>
   </Item> 

请注意,这是化妆品的变化;它实际上改变了元素的名称。