1

私は自分の問題にどのように取り組むのが最善かについてのガイダンスを探していました。

私は次のようなXMLドキュメントを持っていますが、より大規模です。

<NewDataSet>
  <Table Attri1="Attri1Val" Attri2="Attri2Val" Attri3="Attri3Val" Attri4="Attri4Val" Attri5="Attri5Val" Attri6="Attri6Val" Attri7="Attri7" />
</NewDataSet>

たとえば、特定の属性をテーブルノードからテーブルノード内の要素に移動する必要がありますAttri2Attri5、残りの属性はそのままにしておく必要があります。

これに取り組むための最良の方法は何でしょうか?データスケールは、表示されているものの約3〜4倍です。

編集:期待される出力:

<NewDataSet>
  <Table Attri1="Attri1Val" Attri3="Attri3Val" Attri4="Attri4Val" Attri6="Attri6Val" Attri7="Attri7">
    <Attri2>Attri2Val</Attri2>
    <Attri5>Attri5Val</Attri5>
  </Table>
</NewDataSet>

複雑さは実際には問題ではなく、データの規模とそれを処理するための最良の方法は何ですか。

4

2 に答える 2

1

使用する

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

<xsl:template match="Table">
  <xsl:copy>
    <xsl:apply-templates select="@*[not(name() = 'Attri2') and not(name() = 'Attri5')]"/>
     <xsl:apply-templates select="@Attri2 | @Attri5 | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Table/@Attri2 | Table/@Attri5">
 <xsl:element name="{name()}">
   <xsl:value-of select="."/>
 </xsl:element>
</xsl:template>

[編集]属性の名前の比較は少し醜いですが、おそらくあなたのサンプルには役立つでしょう。本当に必要なのは@* execpt (@Attri2, @Attri5)、XPath2.0だけです。XPath 1.0では、同等のものは

<xsl:template match="Table">
  <xsl:copy>
    <xsl:variable name="all-attributes" select="@*"/>
    <xsl:variable name="to-be-transformed" select="@Attri2 | @Attri5"/>
    <xsl:apply-templates select="$all-attributes[count(. | $to-be-transformed) != count($to-be-transformed)]"/>
     <xsl:apply-templates select="$to-be-transformed | node()"/>
  </xsl:copy>
</xsl:template>
于 2012-10-23T09:59:36.993 に答える
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:param name="pToTransform" select="'|Attri2|Attri5|'"/>

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

 <xsl:template match="Table">
  <xsl:copy>
      <xsl:apply-templates select=
         "@*[not(contains($pToTransform, concat('|',name(),'|')))] | node()"/>
      <xsl:apply-templates mode="makeElement"
        select="@*[contains($pToTransform, concat('|',name(),'|'))]"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@*" mode="makeElement">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

この変換が提供されたXMLドキュメントに適用される場合:

<NewDataSet>
    <Table Attri1="Attri1Val" Attri2="Attri2Val"
    Attri3="Attri3Val" Attri4="Attri4Val"
    Attri5="Attri5Val" Attri6="Attri6Val"
    Attri7="Attri7" />
</NewDataSet>

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

<NewDataSet>
   <Table Attri1="Attri1Val" Attri3="Attri3Val" Attri4="Attri4Val" Attri6="Attri6Val" Attri7="Attri7">
      <Attri2>Attri2Val</Attri2>
      <Attri5>Attri5Val</Attri5>
   </Table>
</NewDataSet>
于 2012-10-23T12:33:29.503 に答える