0

これで、次のような XML ファイルができました。

<DM Name="A DM"> 
  <DV  id="SQL:Select something from db" Name="DV 1"> 
    <Sample aid="SQL:Select something from db" /> 
  </DV> 
  <DV  id="SQL:Select something from db" Name="DV 2"> 
    <Sample aid="SQL:Select something from db" name ="DC"> 
      good 
    </Sample> 
  </DV> 
</DM> 

XSLT を使用して変換したいのですが、このタンプレットには、変換する DV を決定するパラメーターがあります。パラメーター ($dvIndex = 0) の場合、すべての要素と属性を保持し、属性を属性に変換するだけです。 "SQL:" で始まる値で、($dvindext > 0) の場合、特定の DV を変換するだけです (他の DV を削除します)。XSLT を以下のように記述しましたが、DM の属性が欠落しており、DM の属性をコピーする方法がわかりません。より良い解決策があるかどうかはわかりません。XML ファイル:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:user="urn:my-scripts"
>
  <xsl:output method="xml" indent="yes"/>
  <msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
     public string UpperCase(string value){
      return value.ToUpper();
     }
      ]]>
  </msxsl:script>

  <xsl:param name="dvIndex" select="2" />

  <xsl:template match="DM" >
    <xsl:copy>
      <xsl:choose>
        <xsl:when test="$dvIndex > 0">
          <xsl:apply-templates select="DV[$dvIndex]"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

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

  <!--[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]-->
  <xsl:template match="@*[user:UpperCase(substring(.,1,4))='SQL:']">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="'parsedSQL'"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

この質問は、私の質問 2# ( XSLT を使用して XML ファイルの属性のみを変換し、他のコンテンツを残すにはどうすればよいですか? )にも関連しています。

どうもありがとう!

4

2 に答える 2

1

このスタイルシート:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="dvIndex" select="2" />
    <xsl:template match="DM" >
        <xsl:copy>
            <xsl:apply-templates select="@*|DV[$dvIndex]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="'parsedSQL'"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

結果:

<DM Name="A DM">
<DV id="parsedSQL" Name="DV 2">
<Sample aid="parsedSQL" name="DC">
      good
    </Sample>
</DV>
</DM>

パラメータdvIndex付き0:

<DM Name="A DM"></DM>

: スクリプト作成は避けてください。これは標準ではありません。使用するたびにスクリプト エンジンを強制的にロードします。

編集DV: $dvIndex が 0 のときにすべて処理する場合、このスタイルシート:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:param name="dvIndex" select="2" />
    <xsl:template match="DM" >
        <xsl:copy>
            <xsl:apply-templates select="@*|DV[$dvIndex]|DV[not($dvIndex)]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]">
        <xsl:attribute name="{name()}">
            <xsl:value-of select="'parsedSQL'"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

$dvIndex が 0 の場合、出力:

<DM Name="A DM">
<DV id="parsedSQL" Name="DV 1">
<Sample aid="parsedSQL"></Sample>
</DV>
<DV id="parsedSQL" Name="DV 2">
<Sample aid="parsedSQL" name="DC">
      good
    </Sample>
</DV>
</DM>
于 2010-07-21T19:07:16.607 に答える
0

以下はおそらくあなたが必要とすることをします。<xsl:apply-templates select="@*" />属性をコピーするための追加に注意してください。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:user="urn:my-scripts">

  <xsl:output method="xml" indent="yes"/>
  <msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
     public string UpperCase(string value){
      return value.ToUpper();
     } ]]>
  </msxsl:script>

  <xsl:param name="dvIndex" select="0" />

  <xsl:template match="DM" >
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:choose>
        <xsl:when test="$dvIndex > 0">
          <xsl:apply-templates select="DV[$dvIndex]"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="DV"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

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

  <!--[starts-with(translate(substring(.,1,4),'SQL:','sql:'),'sql:')]-->
  <xsl:template match="@*[user:UpperCase(substring(.,1,4))='SQL:']">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="'parsedSQL'"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>
于 2010-07-21T17:59:58.673 に答える