0

このファイルがあれば

<config someAttribute="sA" otherAttribute="oA">
<parameter name="p1" yetAnotherAttribute="yAA">
    Something
</parameter>
<parameter name="p2" yetAnotherAttribute="yAA2">
    Something else
</parameter>
<parameter name="p3" yetAnotherAttribute="yAA3">
    Something more
</parameter>
<parameter name="p4" yetAnotherAttribute="yAA4">
    Yet more 
</parameter>
</config>

そして、私はこのようなものが欲しい

<configs>
<config someAttribute="sA" otherAttribute="oA">
    <parameter name="p1" yetAnotherAttribute="yAA">
        Something
    </parameter>
    <parameter name="p2" yetAnotherAttribute="yAA2">
        Something else
    </parameter>
</config>
<config someAttribute="sA" otherAttribute="oA">
    <parameter name="p3" yetAnotherAttribute="yAA3">
        Something more
    </parameter>
    <parameter name="p4" yetAnotherAttribute="yAA4">
        Yet more 
    </parameter>
</config>
</configs>

xsltファイルについて教えてください。また、xslt を学習するのに役立つリソースがどこにあるか教えていただけますか (基本的なトピックから高度なトピックまで、w3schools 以外で)。

より意味のあるファイルを提供することにしました

このファイルを変換したい

<config width="100" height="200">
<parameter name="account number" country="UK">
    12345678901234567890123456
</parameter>
<parameter name="client code" codeType="xa">
    UK0112
</parameter>
<parameter name="email-address" accepts="yes">
    john.sparrow@rex.co.uk
</parameter>
<parameter name="postal-code" country="UK">
    W1A 1HQ 
</parameter>
</config>

このファイルに

<configs>
<config width="100" height="200">
    <parameter name="account number" country="UK">
        12345678901234567890123456
    </parameter>
    <parameter name="client code" codeType="xa">
        UK0112
    </parameter>
</config>
<config width="100" height="200">
    <parameter name="email-address" accepts="yes">
        john.sparrow@rex.co.uk
    </parameter>
    <parameter name="postal-code" country="UK">
        W1A 1HQ 
    </parameter>    
</config>
</configs>

よろしく、MY

4

1 に答える 1

0

「口座番号」と「クライアント番号」が最初のグループに入り、「メールアドレス」と「郵便番号」が2番目のグループに入る以外に、ここには統一規則がないように見えるので、私ができる最善のこと推奨されるのは、このハードコーディングされたアプローチです。

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

  <xsl:template match="/*">
    <configs>
      <xsl:call-template name="Group">
        <xsl:with-param name="children"
                        select="*[@name = 'account number' or
                                  @name = 'client code']" />
      </xsl:call-template>
      <xsl:call-template name="Group">
        <xsl:with-param name="children"
                        select="*[@name = 'email-address' or
                                  @name = 'postal-code']" />
      </xsl:call-template>
    </configs>
  </xsl:template>

  <xsl:template name="Group">
    <xsl:param name="children" />

    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates select="$children" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

サンプル入力で実行すると、結果は次のようになります。

<configs>
  <config width="100" height="200">
    <parameter name="account number" country="UK">
      12345678901234567890123456
    </parameter>
    <parameter name="client code" codeType="xa">
      UK0112
    </parameter>
  </config>
  <config width="100" height="200">
    <parameter name="email-address" accepts="yes">
      john.sparrow@rex.co.uk
    </parameter>
    <parameter name="postal-code" country="UK">
      W1A 1HQ
    </parameter>
  </config>
</configs>

または、ソース データでの順序に応じて項目を 2 つ (または任意の数) のグループに分割する場合は、次のようにします。

<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:variable name="groupSize" select="2" />

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

  <xsl:template match="/*">
    <configs>
      <xsl:apply-templates select="parameter[position() mod $groupSize = 1]"
                           mode="group" />
    </configs>
  </xsl:template>

  <xsl:template match="*" mode="group">
    <config>
      <xsl:apply-templates select ="../@*" />
      <xsl:apply-templates 
        select=". | following-sibling::*[position() &lt; $groupSize]"/>
    </config>
  </xsl:template>
</xsl:stylesheet>

サンプル入力で実行すると、上記と同じ結果が得られます。

于 2013-04-23T12:31:51.570 に答える