syrkined: (Default)
[personal profile] syrkined
Есть xml вида:

<root>
  <node1>
    <child1>
      <f0>a</f0>
      <f1>b</f1>
     <f2>c</f2>
     <f3>d</f3>
    </child1>
  </node1>
  <node2>
    <child2>
      <f0>a</f0>
      <f1>b</f1>
     <f2>c</f2>
     <f3>d</f3>
    </child2>
    <child2>
      <f0>f</f0>
      <f1>g</f1>
     <f2>h</f2>
     <f3>i</f3>
    </child2>
  </node2>
</root>


Надо его преобразовать в
<root>
  <node1>
    <child1 attr2="b" attr1="a">
     <f2>c</f2>
     <f3>d</f3>
    </child1>
  </node1>
  <node2>
    <child2 attr2="b" attr1="a">
     <f2>c</f2>
     <f3>d</f3>
    </child2>
    <child2 attr2="g" attr1="f">
     <f2>h</f2>
     <f3>i</f3>
    </child2>
  </node2>
</root>

Помогите написать xsl или, может быть, существуют другие реализации. Куда стоит сделать кросспост?

Date: 2007-08-29 12:48 pm (UTC)
From: [identity profile] sentjao.livejournal.com
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="f1|f0">
<xsl:attribute name="f1">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:attribute name="f0">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="f3|f2">
<xsl:element name="{name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:apply-templates select="*[* or @*]|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Date: 2007-08-29 02:46 pm (UTC)
From: [identity profile] syrkined.livejournal.com
Огромное спасибо! :)

Date: 2007-08-29 02:48 pm (UTC)
From: [identity profile] sentjao.livejournal.com
Работает? Ну и кайф.

Date: 2007-08-29 02:50 pm (UTC)
From: [identity profile] sentjao.livejournal.com
Секунду! Там баг!

Date: 2007-08-29 02:58 pm (UTC)
From: [identity profile] sentjao.livejournal.com
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="f1">
<xsl:attribute name="f1">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="f0">
<xsl:attribute name="f0">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="f3|f2">
<xsl:element name="{name(.)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:apply-templates select="*[* or @*]|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Date: 2007-08-30 06:02 am (UTC)
From: [identity profile] syrkined.livejournal.com
Это уже мелочи. Еще раз спасибо.

Date: 2007-08-30 03:36 pm (UTC)
From: [identity profile] mogel156.livejournal.com
Бахтин отдыхает. Мои представления о диалоге в корне перевернулись. Трындец.

Date: 2007-08-31 06:18 am (UTC)
From: [identity profile] syrkined.livejournal.com
Век живи... :)

Profile

syrkined: (Default)
Dmitry Syrkin

December 2016

S M T W T F S
    123
45678910
11121314151617
18192021222324
252627 28293031

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 1st, 2025 01:06 am
Powered by Dreamwidth Studios