конвертировать xml в другой формат xml, используя xslt - PullRequest
0 голосов
/ 28 января 2020

Я очень плохо знаком с xsl и на этапе обучения мне нужна помощь по преобразованию XML в другую структуру XML, так как я пробовал много способов, но не смог найти решение, которое я ищу

Вот мой ввод xml

<Output>
  <refcur>
    <OVERALLSTATUS>TRUE</OVERALLSTATUS>
    <MODULE>ABC</MODULE>
    <TYPENAME>ABCType1</TYPENAME>
    <FROMSTATE>State3</FROMSTATE>
    <TOSTATE></TOSTATE>
    <EXCEPTION></EXCEPTION>
  </refcur>
   <refcur>
     <OVERALLSTATUS>TRUE</OVERALLSTATUS>
     <MODULE>ABC</MODULE>
     <TYPENAME>ABCType1</TYPENAME>
     <FROMSTATE>State1</FROMSTATE>
     <TOSTATE>State2;State3</TOSTATE>
     <EXCEPTION></EXCEPTION>
  </refcur>
  <refcur>
    <OVERALLSTATUS>TRUE</OVERALLSTATUS>
    <MODULE>ABC</MODULE>
    <TYPENAME>ABCType2</TYPENAME>
    <FROMSTATE>State3</FROMSTATE>
    <TOSTATE></TOSTATE>
    <EXCEPTION></EXCEPTION>
  </refcur>
  <refcur>
    <OVERALLSTATUS>TRUE</OVERALLSTATUS>
    <MODULE>ABC</MODULE>
    <TYPENAME>ABCType2</TYPENAME>
     <FROMSTATE>State1</FROMSTATE>
     <TOSTATE>State2;State3</TOSTATE>
    <EXCEPTION></EXCEPTION>
  </refcur>
  <refcur>
    <OVERALLSTATUS>TRUE</OVERALLSTATUS>
    <MODULE>ABC</MODULE>
    <TYPENAME>ABCType3</TYPENAME>
    <FROMSTATE>State3</FROMSTATE>
    <TOSTATE></TOSTATE>
    <EXCEPTION></EXCEPTION>
  </refcur>
  <refcur>
    <OVERALLSTATUS>TRUE</OVERALLSTATUS>
    <MODULE>ABC</MODULE>
    <TYPENAME>ABCType3</TYPENAME>
     <FROMSTATE>State1</FROMSTATE>
     <TOSTATE>State2;State3</TOSTATE>
    <EXCEPTION></EXCEPTION>
  </refcur>
 </Output>

Вот формат, который мне нужен

<Output>
  <refcur>
    <Module>ABC</Module>
    <Transition>
      <TypeName>ABCType1</TypeName>
      <Location>
        <FromLocation>State3</FromLocation>
        <ToLocation></ToLocation>
        <Exception></Exception>
      </Location>
      <Location>
        <FromLocation>State1</FromLocation>
        <ToLocation>State2;State3</ToLocation>
        <Exception></Exception>
      </Location>
    </Transition>
    <Transition>
      <TypeName>ABCType2</TypeName>
      <Location>
        <FromLocation>State3</FromLocation>
        <ToLocation></ToLocation>
        <Exception></Exception>
      </Location>
      <Location>
        <FromLocation>State1</FromLocation>
        <ToLocation>State2;State3</ToLocation>
        <Exception></Exception>
      </Location>
    </Transition>
    <Transition>
      <TypeName>ABCType3</TypeName>
      <Location>
        <FromLocation>State3</FromLocation>
        <ToLocation></ToLocation>
        <Exception></Exception>
      </Location>
      <Location>
        <FromLocation>State1</FromLocation>
        <ToLocation>State2;State3</ToLocation>
        <Exception></Exception>
      </Location>
   <Transition>
  </refcur>
</Output>

Это мой xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="groups" match="/Output/refcur" use="TYPENAME" />

  <xsl:template match="/Output">
    <xsl:apply-templates select="refcur[generate-id() = generate-id(key('groups', TYPENAME)[1])]"/>
  </xsl:template>
  <xsl:template match="refcur">
    <Output>
      <refcur>
        <Module>
          <xsl:value-of select="MODULE"/>
        </Module>     
          <Transition>
            <TypeName>
              <xsl:value-of select="TYPENAME"/></TypeName>
            <xsl:for-each select="key('groups', TYPENAME)">
            <Location>
              <FromLocation>
                <xsl:value-of select="FROMSTATE"/>
              </FromLocation>
              <ToLocation>
                <xsl:value-of select="TOSTATE"/>
              </ToLocation>
              <Exception>
                <xsl:value-of select="EXCEPTION"/>
              </Exception>
            </Location>
            </xsl:for-each>
          </Transition>
      </refcur>
    </Output>
  </xsl:template>
</xsl:stylesheet>

Но это преобразовать в приведенный ниже формат

<Output>
  <refcur>
    <Module>ABC</Module>
    <Transition>
      <TyeName>ABCType1</TypeName>
      <Location>
        <FromLocation>State3</FromLocation>
        <ToLocation></ToLocation>
        <Exception></Exception>
      </Location>
      <Location>
        <FromLocation>State1</FromLocation>
        <ToLocation>State2;State3</ToLocation>
        <Exception></Exception>
      </Location>
    </Transition>
  </refcur>
</Output>
<Output>
  <refcur>
    <Module>ABC</Module>
    <Transition>
      <TyeName>ABCType2</TypeName>
      <Location>
        <FromLocation>State3</FromLocation>
        <ToLocation></ToLocation>
        <Exception></Exception>
      </Location>
      <Location>
        <FromLocation>State1</FromLocation>
        <ToLocation>State2;State3</ToLocation>
        <Exception></Exception>
      </Location>
    </Transition>
  </refcur>
</Output>
<Output>
  <refcur>
    <Module>ABC</Module>
    <Transition>
      <TyeName>ABCType3</TypeName>
      <Location>
        <FromLocation>State3</FromLocation>
        <ToLocation></ToLocation>
        <Exception></Exception>
      </Location>
      <Location>
        <FromLocation>State1</FromLocation>
        <ToLocation>State2;State3</ToLocation>
        <Exception></Exception>
      </Location>
    </Transition>
  </refcur>
</Output>

Может кто-нибудь помочь мне исправить xsl?

Вот код C# для генерации вывода xml

static void Main(string[] args)
        {
            string baseName = @"C:\temp\XMLTOXML\XMLTOXML";
            XslCompiledTransform myXslTransform = new XslCompiledTransform();
            myXslTransform.Load(baseName + "\\transform.xslt");
            myXslTransform.Transform(baseName + "\\InputXml.xml", baseName + "\\OutputXml.xml");
        }

1 Ответ

0 голосов
/ 06 февраля 2020
     Here is what I was looking for:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2003/11/xpath-functions" xmlns:ms="urn:schemas- 
     microsoft-com:xslt"exclude-result-prefixes="xs">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/" name="DataSet">
    <Output>
    <refcur>
    <Module>
      <xsl:value-of select="Output/refcur/MODULE"></xsl:value-of>
    </Module>
    <xsl:for-each select="Output/refcur/TYPENAME[not(.=preceding::TYPENAME)]">
      <xsl:variable name ="TypeName" select ="."/>
      <Transition>
        <TypeName>
          <xsl:value-of select="."></xsl:value-of>
        </TypeName>
        <xsl:for-each select ="/Output/refcur">
          <xsl:if test ="TYPENAME = $TypeName">
            <Location>
              <FromLocation>
                <xsl:value-of select ="FROMSTATE"/>
              </FromLocation>
              <ToLocation>
                <xsl:value-of select ="TOSTATE"/>
              </ToLocation>
              <Exception>
                <xsl:value-of select ="EXCEPTION"/>
              </Exception>
            </Location>
          </xsl:if>
        </xsl:for-each>
      </Transition>
    </xsl:for-each>
  </refcur>
</Output>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...