2

私は以下のような入力xmlを持っています

<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>

ここでの出力は、以下のような同じ出力が必要です

<maindocument>
<import>
<doctype>Valuable</doctype>
<docint>text</docint>
</import>
</maindocument

私はxsltで上記のコードを試しています。絶対パスと相対パスの助けを借りて、私の質問はどのようにテスト要素に入り、そこからインポート要素を選択する必要があるかです。

案内してください

Karthicよろしく

4

2 に答える 2

2

適切な要素を出力に適切にコピーするのは簡単です。XSLT2.0を使用すると、downloading要素の内容を簡単に分割することもできます。

<xsl:output indent="yes"/>

<xsl:template match="maindocument">
  <xsl:copy>
    <xsl:apply-templates select="testing/import"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="import">
  <xsl:copy>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="import/downloading">
  <xsl:analyze-string select="." regex="(\w)(\w+)\s+(\w+)">
    <xsl:matching-substring>
      <doctype>
        <xsl:value-of select="concat(upper-case(regex-group(1)), regex-group(2))"/>
      </doctype>
      <docint>
        <xsl:value-of select="regex-group(3)"/>
      </docint>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

Saxon 9.4を使用すると、上記のスタイルシートは入力を変換します

<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>

の中へ

<maindocument>
   <import>
      <doctype>Valuable</doctype>
      <docint>text</docint>
   </import>
</maindocument>

の使用はanalyze-string例として意図されており、サンプルから、downloading要素が持つことができる入力の種類と、それをどのように処理するかは明確ではありません。したがって、上記が十分でない場合は、詳細を投稿してください。

于 2012-08-01T12:04:19.740 に答える
1
With the help of absolute path and relative path my question is how i will get into testing element then i have to select import element from there.

実際には、への完全なパスを指定して、子testingがあることを確認する必要はありません。import

XSLT処理モデル(組み込みテンプレート)は、テンプレートおよび適切なテンプレート一致パターンと組み合わせて、ナビゲーションを行います。

特定のノードに一致する明示的なテンプレートがない場合、組み込みのテンプレートが実行用に選択されます。組み込みテンプレートを適用した累積的な結果は、ドキュメントツリーがトラバースされ、テキストノードのみが出力されることです。

別の特定の方法で処理する必要があるノードに対してのみ、組み込みのテンプレートをオーバーライドするテンプレートを指定できます。組み込みテンプレートがノードの子にテンプレートを適用し、そのノードに一致する明示的な(ユーザー提供の)テンプレートがある場合、この明示的なテンプレートがそのノードの実行/処理用に選択されます。

この短いXSLT1.0変換

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <maindocument>
        <xsl:apply-templates/>
     </maindocument>
 </xsl:template>

 <xsl:template match="import/downloading">
  <import>
    <doctype><xsl:value-of select="substring-before(., ' ')"/></doctype>
    <docint><xsl:value-of select="substring-after(., ' ')"/></docint>
  </import>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

提供されたXMLドキュメントに適用した場合

<maindocument>
    <first>
        <testing>random text</testing>
        <checking>random test</checking>
    </first>
    <testing>
        <testing>sample</testing>
        <checking>welcome</checking>
        <import>
            <downloading>valuable text</downloading>
        </import>
    </testing>
</maindocument>

必要な正しい結果を生成します

<maindocument>
   <import>
      <doctype>valuable</doctype>
      <docint>text</docint>
   </import>
</maindocument>
于 2012-08-01T12:09:09.663 に答える