xslt将原始xml转换为新的xml

时间:2018-02-05 19:24:34

标签: xml xslt

我需要将原始xml文档转换为另一个xml文档。在新的xml文档中,我不知道如何只吸引某些孩子,然后是几个孙子孙女。目前,我最终得到了TESTRECORD节点下的所有内容,这给了我太多的额外数据。

任何帮助都将不胜感激。

输入XML:

<TOP>
  <LEVEL1>
    <TESTRECORD>
      <ENTRYID>267432</ENTRYID>
      <LINEID>254734</LINEID>
      <SEQUENCE>80</SEQUENCE>
      <LINE>
        <ASSETNUM>123456</ASSETNUM>
        <LINEID>254734</LINEID>
        <METERNAME> HCOLOR </METERNAME>
        <ASSET>
          <ASSETID>388391</ASSETID>
          <ASSETUID>388417</ASSETUID>
          <DESCRIPTION>My Asset Description</DESCRIPTION>
          <SERVICEADDRESS>
            <ADDRESSCODE>5308030000</ADDRESSCODE>
            <STREETADDRESS>1234 Kings Highway</STREETADDRESS>
          </SERVICEADDRESS>
        </ASSET>
        <METER>
          <DESCRIPTION>Hair Color</DESCRIPTION>
          <DOMAINID>HCOLOR</DOMAINID>
          <METERNAME>HCOLOR</METERNAME>
        </METER>
      </LINE>
    </TESTRECORD >
    <TESTRECORD>
      <ENTRYID>555555</ENTRYID>
      <LINEID>222222</LINEID>
      <SEQUENCE>90</SEQUENCE>
      <LINE>
        <ASSETNUM>234567</ASSETNUM>
        <LINEID>222222</LINEID>
        <METERNAME> HCOLOR </METERNAME>
        <ASSET>
          <ASSETID>388391</ASSETID>
          <ASSETUID>388417</ASSETUID>
          <DESCRIPTION>My New Asset Description</DESCRIPTION>
          <SERVICEADDRESS>
            <ADDRESSCODE>5308030000</ADDRESSCODE>
            <STREETADDRESS>1234 Kings Highway</STREETADDRESS>
          </SERVICEADDRESS>
        </ASSET>
        <METER>
          <DESCRIPTION>Hair Color</DESCRIPTION>
          <DOMAINID>HCOLOR</DOMAINID>
          <METERNAME>HCOLOR</METERNAME>
        </METER>
      </LINE>
    </TESTRECORD >
  </LEVEL1>
</TOP>

期望的输出:

<jobDocument>
<![CDATA[
<TESTRECORD>
    <ENTRYID>267432</ENTRYID>
    <LINEID>254734</LINEID>
    <SEQUENCE>80</SEQUENCE>
    <ASSETNUM>123456</ASSETNUM>
    <METERNAME>HCOLOR</METERNAME>
    <ASSETID>388391</ASSETID>
    <ASSETUID>388417</ASSETUID>
    <DESCRIPTION>My Asset Description</DESCRIPTION>
    <STREETADDRESS>1234 Kings Highway</STREETADDRESS>
    <DESCRIPTION>Hair Color</DESCRIPTION>
    <DOMAINID>HCOLOR</DOMAINID>
</TESTRECORD>
<TESTRECORD>
    <ENTRYID>555555</ENTRYID>
    <LINEID>222222</LINEID>
    <SEQUENCE>90</SEQUENCE>
    <ASSETNUM>234567</ASSETNUM>
    <METERNAME>HCOLOR</METERNAME>
    <ASSETID>388391</ASSETID>
    <ASSETUID>388417</ASSETUID>
    <DESCRIPTION>My New Asset Description</DESCRIPTION>
    <STREETADDRESS>1234 Kings Highway</STREETADDRESS>
    <DESCRIPTION>Hair Color</DESCRIPTION>
    <DOMAINID>HCOLOR</DOMAINID>
</TESTRECORD>
]]>
</jobDocument>

当前尚未提供所需结果的XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform”  
  version="1.0">
<xsl:output method="xml" cdata-section-elements="" />

<xsl:template match="/">

<jobDocument>
  <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
    <xsl:apply-templates mode="copy"
             select=" descendant::TESTRECORD" />
  <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>

</jobDocument>
</xsl:template>

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

<xsl:template match="text()|comment()" mode="copy">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

以下是用于展平<TESTRECORD>下方所有元素的样式表。它确实包含了您在预期结果中未提及的一些元素,但总体而言,输出接近您的预期。如果没有进一步的标准,很难说出要包含哪些元素以及要排除哪些元素。将输出包装在CDATA部分会损坏输出的缩进 - 但不会损坏结构。但你应该了解模板。

*[count(descendant::*) > 0]匹配具有0个以上子元素的所有元素,即:所有具有子元素的元素。 *[self::TESTRECORD or count(descendant::*) = 0]匹配所有TESTRECORD本身或零子项的元素。这应该复制TESTRECORD元素。如果你有不同的名字,你应该把它放在这里。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" cdata-section-elements="" />
<xsl:strip-space elements="*" />

  <xsl:template match="/">
    <jobDocument>
      <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
      <xsl:apply-templates mode="copy" select=" descendant::TESTRECORD" />
      <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
    </jobDocument>
  </xsl:template>

  <xsl:template match="*[count(descendant::*) > 0]" mode="copy">
    <xsl:apply-templates select="node()" mode="copy" />
  </xsl:template>

  <xsl:template match="*[self::TESTRECORD or count(descendant::*) = 0]" mode="copy">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node()" mode="copy" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="text()|comment()" mode="copy">
    <xsl:copy/>
  </xsl:template>

</xsl:stylesheet>

输出(近似):

<?xml version="1.0"?>
<jobDocument><![CDATA[
    <TESTRECORD>
        <ENTRYID>267432</ENTRYID>
        <LINEID>254734</LINEID>
        <SEQUENCE>80</SEQUENCE>
        <ASSETNUM>123456</ASSETNUM>
        <LINEID>254734</LINEID>
        <METERNAME> HCOLOR </METERNAME>
        <ASSETID>388391</ASSETID>
        <ASSETUID>388417</ASSETUID>
        <DESCRIPTION>My Asset Description</DESCRIPTION>
        <ADDRESSCODE>5308030000</ADDRESSCODE>
        <STREETADDRESS>1234 Kings Highway</STREETADDRESS>
        <DESCRIPTION>Hair Color</DESCRIPTION>
        <DOMAINID>HCOLOR</DOMAINID>
        <METERNAME>HCOLOR</METERNAME>
    </TESTRECORD>
    <TESTRECORD>
        <ENTRYID>555555</ENTRYID>
        <LINEID>222222</LINEID>
        <SEQUENCE>90</SEQUENCE>
        <ASSETNUM>234567</ASSETNUM>
        <LINEID>222222</LINEID>
        <METERNAME> HCOLOR </METERNAME>
        <ASSETID>388391</ASSETID>
        <ASSETUID>388417</ASSETUID>
        <DESCRIPTION>My New Asset Description</DESCRIPTION>
        <ADDRESSCODE>5308030000</ADDRESSCODE>
        <STREETADDRESS>1234 Kings Highway</STREETADDRESS>
        <DESCRIPTION>Hair Color</DESCRIPTION>
        <DOMAINID>HCOLOR</DOMAINID>
        <METERNAME>HCOLOR</METERNAME>
    </TESTRECORD>]]>
</jobDocument>