0

この形式のサンプル XML があります。

<Records>
 <Record>
  <Name>Test</Name>
  <IDS>Test-1, Test-2, Test-3</IDS>
  <Type>Type1</Type>
 </Record>
 <Record>
  <Name>XML</Name>
  <IDS>Test-4, Test-5</IDS>
  <Type>XMLType</Type>
 </Record>
</Records>

出力 XML で明確に設定できるように、ID タグからカンマ区切りの値を抽出したいと考えています。この場合、全部で 5 つの ID があるため、5 つのレコードがあり、そのうちの 3 つが 1 つのグループにあり、2 つが別のグループにあります。

基本的に、私の出力は次のようになります。

<Records>
 <Record>
  <Name>Test</Name>
  <ID>Test-1</ID>
 <Record>
 <Record>
  <Name>Test</Name>
  <ID>Test-2</ID>
 </Record>
 ..
 ..
 <Record>
  <Name>XML</Name>
  <ID>Test-5</ID>
 </Record>
</Records>

この種の出力を生成するために値を分割するにはどうすればよいですか?

4

1 に答える 1

2

これは、「アイデンティティ変換」ベースの xslt-1.0 ソリューションです。
IDS の内容は、splitIDS への再帰呼び出しによって吐き出されます。Record/Type は、出力例に含まれていないため無視されます。
次のようなことを試してください:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" indent="yes"/>

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

    <!-- ignore IDS in Record -->
    <xsl:template match="Record/IDS" />

    <!-- ignore Type in Record -->
    <xsl:template match="Record/Type" />


    <xsl:template match="Record">
        <xsl:call-template name="splitIDS">
            <xsl:with-param name="ids" select="IDS" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="splitIDS">
        <xsl:param name="ids"/>
        <xsl:choose>
            <xsl:when test="contains($ids,',')">
                <xsl:call-template name="splitIDS">
                    <xsl:with-param name="ids" select="substring-before($ids,',')"/>
                </xsl:call-template>
                <xsl:call-template name="splitIDS">
                    <xsl:with-param name="ids" select="substring-after($ids,' ')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates />
                    <ID>
                        <xsl:value-of select="$ids"/>
                    </ID>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="Records">
        <xsl:copy>
            <xsl:apply-templates select="Record" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

次の出力が生成されます。

<?xml version="1.0"?>
<Records>
  <Record>
    <Name>Test</Name>
    <ID>Test-1</ID>
  </Record>
  <Record>
    <Name>Test</Name>
    <ID>Test-2</ID>
  </Record>
  <Record>
    <Name>Test</Name>
    <ID>Test-3</ID>
  </Record>
  <Record>
    <Name>XML</Name>
    <ID>Test-4</ID>
  </Record>
  <Record>
    <Name>XML</Name>
    <ID>Test-5</ID>
  </Record>
</Records>
于 2013-06-03T11:18:01.450 に答える