2016-05-12 10 views
-1

Ich bin neu bei XSLT und ich weiß nicht, wie ich das Folgende erreichen soll.Hinzufügen eines neuen Elements basierend auf einer Bedingung mit XSLT an einem bestimmten Ort

Unten ist ein Teil eines Dokuments, das von einem anderen Programm erstellt wurde. Über XSLT möchte ich ein zusätzliches Element an der Position basierend auf einer Bedingung hinzufügen.

 <?xml version="1.0" encoding="utf-8"?> 
     <policies> 
     <policy> 
      <policyKey> 
       <policyNbr>004567</policyNbr> 
       <policyEffectiveDt>2014-11-14</policyEffectiveDt> 
       <policyID>54545</policyID> 
       <policyFormCd> 
        <code>669</code> 
       </policyFormCd> 
      </policyKey> 
      <transactionSplitTrans> 
       <ContractRole>ABC</ContractRole> 
       <code>SCBP</code> 
       <test>123</test> 
      </transactionSplitTrans> 
      <transactionSplitTrans> 
       <ContractRole>DEF</ContractRole> 
       <code>SCBP</code> 
       <test>123</test> 
      </transactionSplitTrans> 
      <Copies> 
       <Copy> 
        <Type>Original</Type> 
        <RecipientRole>ABC</RecipientRole> 
       </Copy> 
       <Copy> 
        <Type>Duplicate</Type> 
        <RecipientRole>XYZ</RecipientRole> 
       </Copy> 
      </Copies> 
     </policy> 
    </policies> 

Ich versuche ContractRole Tag inside transactionSplitTrans mit RecipientRole Tag innerhalb Kopien Tag.And wenn der Wert übereinstimmt, möchte ich hinzufügen einen neuen Tag mit dem Namen Indikator mit dem Wert Y unter dem ContractRole Tag.And auch vergleichen Geben Sie Tag (unter dem Indikator-Tag) vom Kopiertag ein. Andernfalls fügen Sie ein neues Tag-Kennzeichen unterhalb des ContractRole-Tags mit dem Wert N und auch Type-Tag (unter dem Indikator-Tag) aus dem Copies-Tag hinzu. Entfernen Sie dann das Copies-Tag aus dem Output.

Erwartete Ausgabe:

<?xml version="1.0" encoding="utf-8"?> 
    <policies> 
     <policy> 
     <policyKey> 
    <policyNbr>004567</policyNbr> 
    <policyEffectiveDt>2014-11-14</policyEffectiveDt> 
    <policyID>54545</policyID> 
    <policyFormCd> 
    <code>669</code> 
    </policyFormCd> 
    </policyKey> 
    <transactionSplitTrans> 
    <ContractRole>ABC</ContractRole> 
    <indicator>Y</indicator> 
    <Type>Original</Type>  
    <code>SCBP</code> 
    <test>123</test> 
    </transactionSplitTrans> 
    <transactionSplitTrans> 
    <ContractRole>DEF</ContractRole> 
    <indicator>N</indicator> 
    <Type>Duplicate</Type> 
    <code>SCBP</code> 
    <test>123</test> 
    </transactionSplitTrans> 
    </policy> 
     </policies> 

Mein XSLT:

<xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

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

    <xsl:template match="transactionSplitTrans"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
     </xsl:copy> 
     <xsl:variable name="thisaccess" select="//ContractRole" /> 
     <xsl:choose> 
      <xsl:when test="//Copies/Copy[RecipientRole=$thisaccess]"> 
      <Indicator>Y</Indicator> 
      <Type><xsl:value-of select="//Copies/Copy/Type"/></Type> 
     </xsl:when> 
     <xsl:otherwise> 
     <Indicator>N</Indicator> 
     <Type><xsl:value-of select="//Copies/Copy/Type"/></Type> 
     </xsl:otherwise> 
     </xsl:choose> 

    </xsl:template> 

</xsl:stylesheet> 
+0

Ich bin verwirrt. Meinst du, dass der erste 'transactionSplitTrans'-Knoten dem ersten' Copy'-Knoten entspricht? –

+0

"* Fügen Sie eine neue Tag-Anzeige unter dem ContractRole-Tag mit dem Wert N und auch Type-Tag (unter dem Indikator-Tag) aus dem Copies-Tag hinzu *" Sie können 'Type' nicht aus' Copies' nehmen, wenn der 'Indikator' ist "N", es gibt keine entsprechende 'Kopie'. –

Antwort

0

AFAICT, die Sie wollen, wie etwas zu tun ist:

XSLT 1,0

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

<xsl:key name="copy" match="Copy" use="RecipientRole" /> 

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

<xsl:template match="ContractRole"> 
    <xsl:copy-of select="."/> 
    <xsl:variable name="copy" select="key('copy', .)" /> 
    <indicator> 
     <xsl:choose> 
      <xsl:when test="$copy">Y</xsl:when> 
      <xsl:otherwise>N</xsl:otherwise> 
     </xsl:choose> 
    </indicator> 
    <xsl:copy-of select="$copy/Type"/> 
</xsl:template> 

<xsl:template match="Copies"/> 

</xsl:stylesheet> 

zu Ihrem Beispiel Eingang angelegt, wird das Ergebnis sein:

<?xml version="1.0" encoding="UTF-8"?> 
<policies> 
    <policy> 
     <policyKey> 
     <policyNbr>004567</policyNbr> 
     <policyEffectiveDt>2014-11-14</policyEffectiveDt> 
     <policyID>54545</policyID> 
     <policyFormCd> 
      <code>669</code> 
     </policyFormCd> 
     </policyKey> 
     <transactionSplitTrans> 
     <ContractRole>ABC</ContractRole> 
     <indicator>Y</indicator> 
     <Type>Original</Type> 
     <code>SCBP</code> 
     <test>123</test> 
     </transactionSplitTrans> 
     <transactionSplitTrans> 
     <ContractRole>DEF</ContractRole> 
     <indicator>N</indicator> 
     <code>SCBP</code> 
     <test>123</test> 
     </transactionSplitTrans> 
    </policy> 
</policies> 

WICHTIG: Dies unterscheidet sich von Ihrer erwartete Ausgabe: die zweite transactionSplitTrans, wo die ContractRole „DEF“ ist, hat nicht eine Type Element, weil ich nicht weiß, woher es kommen soll.

Verwandte Themen