2

これは元の質問とは異なることは知っていますが、以下の望ましい結果を達成するために私が考えていた思考プロセスでした。

XML入力:

<section hangIndent="no" indent="arabic 1 digit"
isProposedAmendment="no" label="2A">
<title>AAA</title>
<body>
 BBB<subSection label="1">
  <body>
    <para hangIndent="yes" indent="loweralpha 1 character"
     isProposedAmendment="no" label="a">
      <body>
        CCC
      </body>
    </para>
    <para hangIndent="yes" indent="loweralpha 1 character"
      isProposedAmendment="no" label="b">
      <body>
        DDD
      </body>
    </para>
    <para hangIndent="yes" indent="loweralpha 1 character"
     isProposedAmendment="no" label="c">
      <body>
        EEE
      </body>
    </para>
  </body>
</subSection>
</body>
<annotation isProposedAmendment="no">
FFFFF
</annotation>
</section>

目的の出力:各ノードのラベルに基づいてxmlpath名を作成し、各エンドポイントに挿入します。

<nm:xmlpath name ="2A" />
<section hangIndent="no" indent="arabic 1 digit"
isProposedAmendment="no" label="2A">
<title>AAA</title>
<body>
 BBB
<nm:xmlpath name ="2A 1" /> 
<subSection label="1">
  <body>
    <nm:xmlpath name ="2A 1(a)" />
    <para hangIndent="yes" indent="loweralpha 1 character"
isProposedAmendment="no" label="a">
      <body>
        CCC
      </body>
    </para>
    <nm:xmlpath name ="2A 1(b)" />
    <para hangIndent="yes" indent="loweralpha 1 character"
  isProposedAmendment="no" label="b">
      <body>
        DDD
      </body>
    </para>
    <nm:xmlpath name ="2A 1(c)" />
    <para hangIndent="yes" indent="loweralpha 1 character"
  isProposedAmendment="no" label="c">
      <body>
        EEE
      </body>
    </para>
  </body>
</subSection>
</body>
<annotation isProposedAmendment="no">
FFFFF
</annotation>
</section>
4

2 に答える 2

0
<xsl:template match="/">
  <xsl:apply-templates select="//*"/>
</xsl:template>

<xsl:template match="*">
  <xsl:copy>
    <xsl:value-of select="text()[1]"/>
  </xsl:copy>
</xsl:template>

十分なはずです。

于 2013-03-15T12:03:28.163 に答える
0

基本的なアプローチは、属性を持つ任意の要素に一致するテンプレートを用意し、その要素の前に、親、祖父母などで見つけたすべての属性から構築されたものをlabel挿入することです。nm:xmlpathlabel

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:nm="urn:nm-namespace">

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

  <xsl:template match="*[@label]">
    <nm:xmlpath>
      <xsl:attribute name="name">
        <xsl:apply-templates select="." mode="xmlpath" />
      </xsl:attribute>
    </nm:xmlpath>
    <xsl:call-template name="identity" />
  </xsl:template>

  <!-- templates to construct the xmlpath, these should produce text nodes -->

  <!-- general case - path is the enclosing path (if any), followed by space,
       followed by our own @label -->
  <xsl:template match="*" mode="xmlpath">
    <xsl:if test="ancestor::*[@label]">
      <xsl:apply-templates select="ancestor::*[@label][1]" mode="xmlpath" />
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:value-of select="@label" />
  </xsl:template>

  <!-- special case for para - no space after enclosing path, and surround
       our label with () -->
  <xsl:template match="para" mode="xmlpath">
    <xsl:apply-templates select="ancestor::*[@label][1]" mode="xmlpath" />
    <xsl:value-of select="concat('(', @label, ')')" />
  </xsl:template>

</xsl:stylesheet>

これで整形式のXMLを生成するには、入力ドキュメントのルート要素に属性が含まれていてはなりませんlabel。属性がある場合は、そのnm:xmlpath前に前を挿入して、出力ドキュメントに2つのルートレベル要素を指定します。

于 2013-03-15T12:49:06.170 に答える