1

XMLファイルを入力しました。以下のように。

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

これが私が欲しい出力XMLです

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

Googleで検索すると、次のような結果が得られXSL:Copyます。

4

2 に答える 2

2

試す ...

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

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

<xsl:template match="first|testing|checking" />

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

</xsl:stylesheet>
于 2012-07-31T13:14:34.823 に答える
1

この変換:

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

 <xsl:template match="import">
     <maindocument>
      <xsl:copy>
       <doctype><xsl:value-of select="substring-before(*, ' ')"/></doctype>
       <docint><xsl:value-of select="substring-after(*, ' ')"/></docint>
      </xsl:copy>
     </maindocument>
 </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>
    </testing>
    <import>
        <downloading>valuable text</downloading>
    </import>
</maindocument>

必要な正しい結果が生成されます。

<maindocument>
   <import>
      <doctype>valuable</doctype>
      <docint>text</docint>
   </import>
</maindocument>
于 2012-07-31T13:21:34.410 に答える