0

CSV に変換したい Dublin Core でエンコードされたメタデータ ファイルを使用しています。以下の出力に到達しようとしています

identifier1|||identifier2|||identifier3|||identifier4,datestamp1|||datestamp2|||2010-04-27T01:10:31Z,setspec1,title1|||title2,subject1|||subject2,baseURL|||xxxxx|||xxxxx

繰り返し可能な要素は 3 つのパイプ記号 (|||) で区切られ、要素はコンマ (,) で区切られていることに注意してください。

以下のスタイルシートにたどり着きましたが、次のことに苦労しています

(1) コンマでノードを区切ることができるように汎用テンプレートを定義するにはどうすればよいですか?

<xsl:template match="GENERIC MATCH">
  <xsl:apply-templates select="current()" />
  <xsl:if test="position() = last()">,</xsl:if>
</xsl:template>

以下を例として使用Input Fileすると、基本的に、GENERIC MATCH を使用してlevel 2、ノード (ヘッダー、メタデータなど) を動的に処理し、結果をコンマで区切ることができるようになります。

(2)要素が最後の子ノードであるかどうかを判断するにはどうすればよいですか?条件付きで後にカンマを含めることができますか?

<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:template match="/">
  <xsl:apply-templates select="record" />
</xsl:template>
<xsl:template match="record">
  <xsl:apply-templates select="//metadata/oai_dc:dc/dc:title|//metadata/oai_dc:dc/dc:subject" />
  <xsl:if test="not(metadata/oai_dc:dc/node()/position()=last())">#####</xsl:if>
</xsl:template>

<xsl:template match="dc:title">
  <xsl:value-of select="." />
  <xsl:if test="not(position() = last())">||</xsl:if>
</xsl:template>

<xsl:template match="dc:subject">
  <xsl:value-of select="." />
  <xsl:if test="not(position() = last())">||</xsl:if>
</xsl:template>

入力ファイル

<?xml version="1.0"?>
<record>
  <header>
    <identifier>identifier1</identifier>
    <datestamp>datastamp1</datestamp>
    <setSpec>setspec1</setSpec>
  </header>
  <metadata>
    <oai_dc:dc>
      <dc:title>title1</dc:title>
      <dc:title>title2</dc:title>
      <dc:creator>creator1</dc:creator>
      <dc:subject>subject1</dc:subject>
      <dc:subject>subject2</dc:subject>
    </oai_dc:dc>
  </metadata>
  <about>
    <provenance>
      <originDescription altered="false" harvestDate="2011-08-11T03:47:51Z">
        <baseURL>baseURL1</baseURL>
        <identifier>identifier3</identifier>
        <datestamp>datestamp2</datestamp>
        <metadataNamespace>xxxxx</metadataNamespace>
        <originDescription altered="false" harvestDate="2010-10-10T06:15:53Z">
          <baseURL>xxxxx</baseURL>
          <identifier>identifier4</identifier>
          <datestamp>2010-04-27T01:10:31Z</datestamp>
          <metadataNamespace>xxxxx</metadataNamespace>
        </originDescription>
      </originDescription>
    </provenance>
  </about>
</record>

xslt 1.0を使用して作業していxsltprocます。

4

1 に答える 1

1

これはどうですか:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes" omit-xml-declaration="yes"/>
  <!-- A key on all leaf nodes -->
  <!-- *[not(*)] matches any element that is a leaf node
       i.e. it has no child elements. Here, the elements' names are being
       used as the key value. -->
  <xsl:key name="kNodeType" match="*[not(*)]" use="local-name()"/>

  <xsl:template match="/">
    <!-- Use Muenchian grouping to apply the "group" template to the first of
         each leaf node with a distinct name. -->
    <xsl:apply-templates
      select="//*[not(*)][generate-id() = 
                          generate-id(key('kNodeType', local-name())[1])]"
      mode="group" />
  </xsl:template>

  <!-- This template will be used to match only the first item in each group,
       due to the grouping expression used in the previous template. -->
  <xsl:template match="*" mode="group">
    <!-- Skip the comma for the first group, output it for all others -->
    <xsl:if test="position() > 1">,</xsl:if>
    <!-- Apply the "item" template to all items in the same group as this element
         (i.e. those with the same name) -->
    <xsl:apply-templates select="key('kNodeType', local-name())" mode="item" />
  </xsl:template>

  <xsl:template match="*" mode="item">
    <!-- Skip the delimiter for the first item in each group;
         output it for all others -->
    <xsl:if test="position() > 1">|||</xsl:if>
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

サンプル入力で実行すると、次が生成されます。

identifier1|||identifier3|||identifier4,datastamp1|||datestamp2|||2010-04-27T01:10:31Z,setspec1,title1|||title2,creator1,subject1|||subject2,baseURL1|||xxxxx, xxxxx|||xxxxx

于 2013-01-30T14:55:50.107 に答える