How to transform xml to requested one

空扰寡人 提交于 2020-01-25 03:33:09

问题


I have an xml which I must transform it to another structure of xml. I tried many ways but no success. I need to use xslt 1.0. I asked a question but the format is changed after I asked.

input:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonBody>
    <Person>
        <D>Name</D>
        <D>Surname</D>
        <D>Id</D>
    </Person>
    <PersonValues>
        <D>Michael</D>
        <D>Jackson</D>
        <D>01</D>
    </PersonValues>
    <PersonValues>
        <D>James</D>
        <D>Bond</D>
        <D>007</D>
    </PersonValues>
    <PersonValues>
        <D>Kobe</D>
        <D>Bryant</D>
        <D>24</D>
    </PersonValues>
</PersonBody>

Required output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonBody>
    <Persons>
        <Person>
            <Name>Michael</Name>
            <Surname>Jackson</Surname>
            <Id>1</Id>
        </Person>
        <Person>
            <Name>James</Name>
            <Surname>Bond</Surname>
            <Id>007</Id>
        </Person>
        <Person>
            <Name>Kobe</Name>
            <Surname>Bryant</Surname>
            <Id>24</Id>
        </Person>
    </Persons>
</PersonBody>

回答1:


This should do it:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kColumnName" match="Person/*"
           use="count(preceding-sibling::*)" />

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

  <xsl:template match="/*">
    <xsl:copy>
      <Persons>
        <xsl:apply-templates select="PersonValues" />
      </Persons>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="PersonValues">
    <Person>
      <xsl:apply-templates select="*" />
    </Person>
  </xsl:template>

  <xsl:template match="PersonValues/*">
    <xsl:element name="{key('kColumnName', position() - 1)}">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

When run on your sample input, the result is:

<PersonBody>
  <Persons>
    <Person>
      <Name>Michael</Name>
      <Surname>Jackson</Surname>
      <Id>01</Id>
    </Person>
    <Person>
      <Name>James</Name>
      <Surname>Bond</Surname>
      <Id>007</Id>
    </Person>
    <Person>
      <Name>Kobe</Name>
      <Surname>Bryant</Surname>
      <Id>24</Id>
    </Person>
  </Persons>
</PersonBody>


来源:https://stackoverflow.com/questions/16192234/how-to-transform-xml-to-requested-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!