使用XSLT

时间:2017-09-30 01:31:17

标签: xml xslt

我正在尝试使用以下XSLT来转换xml文档的内容。但是,我的一个元素是使用以前的命名空间复制的,而不是所需的。见下文。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:cd="http://schemas.datacontract.org/2004/07/CMachine" xmlns="http://schemas.datacontract.org/2004/07/CMachine.DataContracts" exclude-result-prefixes="cd">
  <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="cd:ArrayOfMachine">
    <Inventory>
      <Schema>2018</Schema>
      <Machines>
        <xsl:apply-templates select="@*|node()" />
      </Machines>
    </Inventory>
  </xsl:template>
</xsl:stylesheet>

输入:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMachine xmlns="http://schemas.datacontract.org/2004/07/CMachine" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Machine>
    <Price>120000</Price>
    <Properties>
      <Axes>XYZ</Axes>
    </Properties>
  </Machine>
</ArrayOfMachine>

输出:

<?xml version="1.0" encoding="utf-8"?>
<Inventory xmlns="http://schemas.datacontract.org/2004/07/CMachine.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Schema>2018</Schema>
  <Machines>
    <Machine xmlns="http://schemas.datacontract.org/2004/07/CMachine">
      <Price>120000</Price>
      <Properties>
        <Axes>XYZ</Axes>
      </Properties>
    </Machine>
  </Machines>
</Inventory>

期望的输出:

<?xml version="1.0" encoding="utf-8"?>
<Inventory xmlns="http://schemas.datacontract.org/2004/07/CMachine.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Schema>2018</Schema>
  <Machines>
    <Machine>
      <Price>120000</Price>
      <Properties>
        <Axes>XYZ</Axes>
      </Properties>
    </Machine>
  </Machines>
</Inventory>

XSLT需要进行哪些调整才能解决此问题? 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

当您使用xsl:copy时,它将复制节点及其命名空间。如果要生成具有相同local-name()的元素并删除命名空间,则需要创建新元素。您可以创建模板以匹配所有元素并生成通用(无命名空间)元素,然后使常规标识模板仅匹配属性,text(),comment()和processing-instruction()节点:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:cd="http://schemas.datacontract.org/2004/07/CMachine" xmlns="http://schemas.datacontract.org/2004/07/CMachine.DataContracts" exclude-result-prefixes="cd">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:copy/>
  </xsl:template>

  <xsl:template match="cd:ArrayOfMachine">
    <Inventory>
      <Schema>2018</Schema>
      <Machines>
        <xsl:apply-templates select="@*|node()" />
      </Machines>
    </Inventory>
  </xsl:template>
</xsl:stylesheet>
相关问题