2

このような入力XMLがあり、XSL変換を使用して以下に示すような目的の出力が必要です。私はブログを調べてきましたが、子ノードだけでなくルート要素に一致する空のタグを削除する方法との関連性を見つけることができませんでした。

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
  <Revenue__c/>
  <Revenue__c/>
  <Revenue__c/>
  <Revenue__c>
    <Sales_Org_ID__c>IV</Sales_Org_ID__c>
    <Branch_ID__c>1</Branch_ID__c>
    <Branch_Name__c>TEST</Branch_Name__c>
    <Therapy_Code__c>TEST</Therapy_Code__c>
    <Therapy_Name__c>TEST</Therapy_Name__c>
    <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
    <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
    <Payor_Type_Name__c>TEST</Payor_Type_Name__c>
    <Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
    <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
    <Payor_ID__c>TEST</Payor_ID__c>
    <Payor_Name__c/>
    <Payor_Type_Code__c>TEST</Payor_Type_Code__c>
    <MDM_Account_EID__c>66600001</MDM_Account_EID__c>
    <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
    <Account__c>001a000001APU5OAAX</Account__c>
    <Contact__c>003a000001RL1EFAA1</Contact__c>
    <Revenue_ID__c>41</Revenue_ID__c>
    <Calendar_Year_Month__c>01</Calendar_Year_Month__c>
  </Revenue__c>
</objects>

これはまさに私が欲しいものです:

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
  <Revenue__c>
    <Sales_Org_ID__c>IV</Sales_Org_ID__c>
    <Branch_ID__c>1</Branch_ID__c>
    <Branch_Name__c>TEST</Branch_Name__c>
    <Therapy_Code__c>TEST</Therapy_Code__c>
    <Therapy_Name__c>TEST</Therapy_Name__c>
    <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
    <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
    <Payor_Type_Name__c>TEST</Payor_Type_Name__c>
    <Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
    <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
    <Payor_ID__c>TEST</Payor_ID__c>
    <Payor_Name__c/>
    <Payor_Type_Code__c>TEST</Payor_Type_Code__c>
    <MDM_Account_EID__c>66600001</MDM_Account_EID__c>
    <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
    <Account__c>001a000001APU5OAAX</Account__c>
    <Contact__c>003a000001RL1EFAA1</Contact__c>
    <Revenue_ID__c>41</Revenue_ID__c>
    <Calendar_Year_Month__c>01</Calendar_Year_Month__c>
  </Revenue__c>
</objects>

任意の提案をいただければ幸いです。

4

2 に答える 2

5

これはおそらく最も単純で最短のソリューションであり、同時に完全に「プッシュスタイル」であり、最も拡張可能で保守可能です。ハードコードされた要素名、リテラル結果要素、名前空間、なしxsl:copy-of

<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="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
 <xsl:template match="/*/*[not(node())]"/>
</xsl:stylesheet>

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

<objects xmlns="urn:sobject.partner.soap.sforce.com">
    <Revenue__c/>
    <Revenue__c/>
    <Revenue__c/>
    <Revenue__c>
        <Sales_Org_ID__c>IV</Sales_Org_ID__c>
        <Branch_ID__c>1</Branch_ID__c>
        <Branch_Name__c>TEST</Branch_Name__c>
        <Therapy_Code__c>TEST</Therapy_Code__c>
        <Therapy_Name__c>TEST</Therapy_Name__c>
        <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
        <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
        <Payor_Type_Name__c>TEST</Payor_Type_Name__c>
        <Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
        <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
        <Payor_ID__c>TEST</Payor_ID__c>
        <Payor_Name__c/>
        <Payor_Type_Code__c>TEST</Payor_Type_Code__c>
        <MDM_Account_EID__c>66600001</MDM_Account_EID__c>
        <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
        <Account__c>001a000001APU5OAAX</Account__c>
        <Contact__c>003a000001RL1EFAA1</Contact__c>
        <Revenue_ID__c>41</Revenue_ID__c>
        <Calendar_Year_Month__c>01</Calendar_Year_Month__c>
    </Revenue__c>
</objects>

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

<objects xmlns="urn:sobject.partner.soap.sforce.com">
   <Revenue__c>
      <Sales_Org_ID__c>IV</Sales_Org_ID__c>
      <Branch_ID__c>1</Branch_ID__c>
      <Branch_Name__c>TEST</Branch_Name__c>
      <Therapy_Code__c>TEST</Therapy_Code__c>
      <Therapy_Name__c>TEST</Therapy_Name__c>
      <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
      <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
      <Payor_Type_Name__c>TEST</Payor_Type_Name__c>
      <Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
      <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
      <Payor_ID__c>TEST</Payor_ID__c>
      <Payor_Name__c/>
      <Payor_Type_Code__c>TEST</Payor_Type_Code__c>
      <MDM_Account_EID__c>66600001</MDM_Account_EID__c>
      <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
      <Account__c>001a000001APU5OAAX</Account__c>
      <Contact__c>003a000001RL1EFAA1</Contact__c>
      <Revenue_ID__c>41</Revenue_ID__c>
      <Calendar_Year_Month__c>01</Calendar_Year_Month__c>
   </Revenue__c>
</objects>

説明

  1. IDルールは、このテンプレートが実行対象として選択されているノードを「現状のまま」コピーします。

  2. 最上位要素の子であり、子を持たない要素のIDテンプレートをオーバーライドする単一のテンプレートがあります。このテンプレートには本文がなく(出力を生成しません)、一致したノードを効果的に「削除」します。

于 2012-10-04T11:34:03.050 に答える
1

これでうまくいくはずです。これはapply-templates、子を持つRevenueノードにのみ使用されcopy-of、空でないRevenueツリーをコピーするためにのみ使用されます。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="urn:sobject.partner.soap.sforce.com"
                exclude-result-prefixes="x">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

  <xsl:template match="/x:objects">
    <objects xmlns="urn:sobject.partner.soap.sforce.com">
      <xsl:apply-templates select="x:Revenue__c[*]" />
    </objects>
  </xsl:template>

  <xsl:template match="x:Revenue__c">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

出力

<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="urn:sobject.partner.soap.sforce.com">
  <Revenue__c>
    <Sales_Org_ID__c>IV</Sales_Org_ID__c>
    <Branch_ID__c>1</Branch_ID__c>
    <Branch_Name__c>TEST</Branch_Name__c>
    <Therapy_Code__c>TEST</Therapy_Code__c>
    <Therapy_Name__c>TEST</Therapy_Name__c>
    <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c>
    <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c>
    <Payor_Type_Name__c>TEST</Payor_Type_Name__c>
    <Calendar_Year_Number__c>2011</Calendar_Year_Number__c>
    <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c>
    <Payor_ID__c>TEST</Payor_ID__c>
    <Payor_Name__c />
    <Payor_Type_Code__c>TEST</Payor_Type_Code__c>
    <MDM_Account_EID__c>66600001</MDM_Account_EID__c>
    <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c>
    <Account__c>001a000001APU5OAAX</Account__c>
    <Contact__c>003a000001RL1EFAA1</Contact__c>
    <Revenue_ID__c>41</Revenue_ID__c>
    <Calendar_Year_Month__c>01</Calendar_Year_Month__c>
  </Revenue__c>
</objects>

編集-それは単純化することができます:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:x="urn:sobject.partner.soap.sforce.com"
                exclude-result-prefixes="x">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

  <xsl:template match="/x:objects">
    <objects xmlns="urn:sobject.partner.soap.sforce.com">
      <xsl:copy-of select="x:Revenue__c[*]" />
    </objects>
  </xsl:template>
</xsl:stylesheet>
于 2012-10-04T05:34:20.210 に答える