1

XSLT 1.0 を使用して、子要素名に基づいて XML 要素をフラット化しようとしています。

ソース XML:

<Contact>
  <ContactPurpose>
    <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
  </ContactPurpose>
  <ContactPurpose>
    <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
  </ContactPurpose>
</Contact>

次の XML に変換する必要があります。

<Contact>
  <ContactPurpose>O</ContactPurpose>
  <ContactPurpose>Call</ContactPurpose>
</Contact>

ロジックは次のとおりです。

IF 子要素名が「PurposeAsPlainText」 THEN 送信先の Other に「O」を設定

ELSEIF 子要素名は「PurposeAsEnum」 THEN ソース値を宛先にコピー

編集 1 : 解決策のいずれも xml を平坦化していないため、より明確にすることができます。改訂されたソースと宛先の XML を参照してください。

EDIT 2 : これは、私がテストしていた XML です。以下の 2 つの変換ソリューションは、元の xml では実際に機能しますが、.NET 4.0 XslCompiledTransform を使用してテストしていた修正された xml では機能しません。または、新しい質問を作成する必要がありますか?

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PatientRecord>
    <Demographics>
      <Contact>
        <ContactPurpose>
          <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
        </ContactPurpose>
        <ContactPurpose>
          <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
        </ContactPurpose>
      </Contact>
    </Demographics>
  </PatientRecord>
</MyDS>
4

3 に答える 3

1

これは簡単で短い方法で行うことができます (明示的な条件なし) :

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

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

 <xsl:template match="ContactPurpose/x:PurposeAsPlainText/text()">0</xsl:template>

 <xsl:template match="ContactPurpose/*"><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>

次の XML ドキュメントに適用した場合 (関心のある両方のケースを組み込むために拡張されています):

<Contact>
    <ContactPurpose>
        <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
    </ContactPurpose>
    <ContactPurpose>
        <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
    </ContactPurpose>
</Contact>

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

<Contact>
   <ContactPurpose>0</ContactPurpose>
   <ContactPurpose>Call</ContactPurpose>
</Contact>

説明:

アイデンティティ ルールをオーバーライドし、テンプレートや一致パターンを適切に使用する。

更新: OP は XML ドキュメントを変更しました。これは現在、デフォルトの名前空間にあります。

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <PatientRecord>
        <Demographics>
            <Contact>
                <ContactPurpose>
                    <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
                </ContactPurpose>
                <ContactPurpose>
                    <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
                </ContactPurpose>
            </Contact>
        </Demographics>
    </PatientRecord>
</MyDS>

したがって、必要な結果を生成するわずかに変更された変換を次に示します

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

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

     <xsl:template match="c:ContactPurpose/x:PurposeAsPlainText/text()">0</xsl:template>

     <xsl:template match="c:ContactPurpose/*"><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>

この変換が新しい XML ドキュメント (上で最も近い) に適用されると、新しく必要な正しい結果が生成されます。

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <PatientRecord>
      <Demographics>
         <Contact>
            <ContactPurpose>0</ContactPurpose>
            <ContactPurpose>Call</ContactPurpose>
         </Contact>
      </Demographics>
   </PatientRecord>
</MyDS>
于 2012-03-06T02:49:05.160 に答える
1
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cds="cds_dt" exclude-result-prefixes="cds">
<!-- identity transform - just copy things that don't have a better rule -->
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<!- a rule for what needs changing -->
<xsl:template match="ContactPurpose[cds:PurposeAsPlainText] ">
    <ContactPurpose>O</ContactPurpose>
</xsl:template>
</xsl:stylesheet>
于 2012-03-05T19:23:57.173 に答える
1

更新:変更された XML ソース ドキュメントに合わせて回答を修正しました。

説明はあまり明確ではありませんが、あなたがやろうとしていることは次のとおりです。

<xsl:stylesheet version="1.0" xmlns:cds_dt="cds_dt" xmlns:cds="cds"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="cds:ContactPurpose">
   <xsl:copy>
      <xsl:choose>
         <!-- when there is a child element PurposeAsPlainText
            in the cds_dt namespace: -->
         <xsl:when test="cds_dt:PurposeAsPlainText">0</xsl:when>
         <!-- I'm guessing that PurposeAsEnum is also supposed to be
            in the cds_dt namespace. -->
         <xsl:otherwise>
            <xsl:value-of select="cds_dt:PurposeAsEnum" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>
于 2012-03-05T20:42:30.150 に答える