简体   繁体   English

XML到XML的转换:嵌套相似的节点

[英]XML to XML conversion: nesting similar nodes

I have the following xml file: 我有以下xml文件:

<ref>
<m-citation>
<string-name>
<surname>Carey</surname>, <given-names>J.W.</given-names>
</string-name>, <string-name>
<surname>Wigand</surname>, <given-names>M.</given-names>
</string-name>, <string-name>
<surname>Can</surname>, <given-names>S.J.</given-names>
</string-name>,
<year>2007</year>. <source>xyz</source>, <volume>1</volume>, <f>75</f>-<l>85</l>. 
</m-citation>
</ref>

Need to convert this to: 需要将其转换为:

<ref>
<m-citation>
<publisher>
<name>
<surname>Carey</surname><given-names>J.W.</given-names>
</name>, <name>
<surname>Wigand</surname><given-names>M.</given-names>
</name>, <name>
<surname>Can</surname><given-names>S.J.</given-names>
</name>,
</publisher> 
<year>2007</year>. <source>xyz</source>, <volume>1</volume>, <f>75</f>-<l>85</l>. 
</m-citation>
</ref>

Please note: 'string-name' changes to 'name', all 'string-name' elements are nested within 'publisher' element and comma after surname is not retained. 请注意:“字符串名称”更改为“名称”,所有“字符串名称”元素都嵌套在“发布者”元素中,并且在不保留姓氏之后使用逗号。 Tried different ways but couldn't get the exact output. 尝试了不同的方法,但无法获得确切的输出。 Help of any kind would be truly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

I have written the following code: 我写了以下代码:

<xsl:template match="m-citation">
<xsl:element name="m-citation">

<xsl:element name="publisher">
<xsl:for-each select="string-name[position() &lt;= last()]">
<xsl:element name="name">
<xsl:element name="surname">
<xsl:value-of select="surname"/>
</xsl:element>
<xsl:element name="given-names">
<xsl:value-of select="given-names"/>
</xsl:element>
</xsl:element><xsl:text>, </xsl:text>
</xsl:for-each>
</xsl:element>

<xsl:apply-templates/>

</xsl:element>
</xsl:template>


<xsl:template match="ref//year">
<xsl:element name="year">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="ref//source">
<xsl:element name="source">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

Similarly for elements volume, f and l. 对于元素体积f和l同样。

I am getting the following output: 我得到以下输出:

<ref>
<m-citation>
<publisher>
<name>
<surname>Carey</surname><given-names>J.W.</given-names>
</name>, <name>
<surname>Wigand</surname><given-names>M.</given-names>
</name>, <name>
<surname>Can</surname><given-names>S.J.</given-names>
</name>,
</publisher> ,,,
<year>2007</year>. <source>xyz</source>, <volume>1</volume>, <f>75</f>-<l>85</l>. 
</m-citation>
</ref>

as I am using apply-templates the commas after string-name (which are outside all child elements) get displayed (,,,). 因为我正在使用apply-templates,所以显示字符串名称(在所有子元素之外)后的逗号(、、、)。 Sorry for the errors if any in the above lines of code. 很抱歉上面的代码行中的错误。 Have cut-shortened it here and there. 在这里和那里切短。

With the next XSLT the element string-name will be translated into name and a publisher element will be added. 在下一个XSLT中,元素string-name将转换为name ,并将添加publisher元素。 All other elements and values are copied: 复制所有其他元素和值:

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

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

    <xsl:template match="m-citation">
        <xsl:copy>
            <xsl:apply-templates select="@*" />

            <publisher>
                <xsl:apply-templates select="string-name" />
            </publisher>

            <xsl:apply-templates select="node()[not(self::string-name)]" />
        </xsl:copy>
    </xsl:template>

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

This XSLT stylesheet: 这个XSLT样式表:

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

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

  <!-- At m-citation, put in the publisher element then apply templates to all string-name
       elements, and to all text nodes that have a string-name element after them.
       Outside the publisher element, apply templates to everything else. -->
  <xsl:template match="m-citation">
    <xsl:copy>
      <publisher>
        <xsl:apply-templates select="string-name | text()[count(following-sibling::string-name) &gt; 0]"/>
      </publisher>
      <xsl:apply-templates select="node()[not(self::string-name) and count(following-sibling::string-name) = 0]"/>
    </xsl:copy>
  </xsl:template>

  <!-- Replace string-name with name then apply-templates to its children. -->
  <xsl:template match="string-name">
    <name>
      <xsl:apply-templates select="@*|node()"/>
    </name>
  </xsl:template>

  <!-- Remove the first text node that comes after surname (a comma in your particular
       input. -->
  <xsl:template match="text()[preceding-sibling::*[1][self::surname]]"/>

</xsl:stylesheet>

when applied to the following input XML: 当应用于以下输入XML时:

<ref>
  <m-citation>
    <string-name>
      <surname>Carey</surname>, <given-names>J.W.</given-names>
    </string-name>, <string-name>
      <surname>Wigand</surname>, <given-names>M.</given-names>
    </string-name>, <string-name>
      <surname>Can</surname>, <given-names>S.J.</given-names>
    </string-name>
    <year>2007</year>. <source>xyz</source> <volume>1</volume> <f>75</f> <l>85</l>.
    </m-citation>
</ref>

produces the following output: 产生以下输出:

<ref>
  <m-citation><publisher>
    <name>
      <surname>Carey</surname><given-names>J.W.</given-names>
    </name>, <name>
      <surname>Wigand</surname><given-names>M.</given-names>
    </name>, <name>
      <surname>Can</surname><given-names>S.J.</given-names>
    </name></publisher>
    <source>xyz</source> <volume>1</volume> <f>75</f> <l>85</l>.
  </m-citation>
</ref>

which is not quite formatted as you want, but I think the content is all there correctly. 这是不是格式化,只要你想,但我认为内容是所有有正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM