0

我的原始xml如下:

<?xml version="1.0" encoding="iso-8859-1"?>
<n1:Report version="1.0" xmlns:n1="http://www.w3.org.com" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<sourceName Identification="xyz"/>
<sourcesys Identification="mycomp">
<Manager>
<ManagerNo>1023114455</ManagerNo>
<Address>Delhi,India</Address>
<Currency>
<CurrencyType>Rupee</CurrencyType>
</Currency>
</Manager>
<Manager>
<ManagerNo>236784455</ManagerNo>
<Address>California,USA</Address>
<Currency>
<CurrencyType>Dollar</CurrencyType>
</Currency>
</Manager>
</sourcesys>
</n1:Report>

现在我想将上面的 XML 转换为如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ManagerDetails href="http://www.w3.org/2001/XMLSchema-instance">
  <ManagerDetail>
    <ManagerNo>1023114455</ManagerNo>
    <Address>
      <PermenantAdd>California,USA</PermenantAdd>
    </Address>
    <CurrencyID>Rupee</CurrencyID>
  </ManagerDetail>
  <ManagerDetail>
    <ManagerNo>236784455</ManagerNo>
    <Address>
      <PermenantAdd>Delhi,India</PermenantAdd>
    </Address>
    <CurrencyID>Dollar</CurrencyID>
  </ManagerDetail>
</managerDetails>

我有 XSLT 来做这个转换,如下所述:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

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

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

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

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

   <xsl:template match="sourceName"/>

   <xsl:template match="Address/text()">
      <PermenantAdd>
         <xsl:value-of select="." />
      </PermenantAdd>
   </xsl:template>

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

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

但是当我在 XSLT 上运行时,它会给出如下输出:

<?xml version="1.0" encoding="UTF-8"?>
<ManagerDetails>
    <ManagerDetail>
        <ManagerNo xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:n1="http://www.w3.org.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1023114455</ManagerNo>
        <Address xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:n1="http://www.tietoenator.com/TRE/InTrans/2008-07-02" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <PermenantAdd>Delhi,India</PermenantAdd>
        </Address>
        <CurrencyID>Rupee</CurrencyID>
    </ManagerDetail>
    <ManagerDetail>
        <ManagerNo xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:n1="http://www.w3.org.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">236784455</ManagerNo>
        <Address xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:n1="http://www.w3.org.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <PermenantAdd>California,USA</PermenantAdd>
        </Address>
        <CurrencyID>Dollar</CurrencyID>
    </ManagerDetail>
</ManagerDetails>

请帮助我删除新节点中不需要的属性。

4

1 回答 1

1

改变:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

到:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:n1="http://www.w3.org.com" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="n1 fn xs xsi">

或者您可以添加以下模板:

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>
于 2013-09-30T12:12:21.917 回答