2016-08-25 1 views
0

Betrachten Sie die folgende Eingabe xml Achtung:XSLT Erstellen von verschachtelten Elementen unter den bestehenden Kontext während Namespaces

<b:PropertyInfo id="N91" xmlns:b="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> 
    <b:CommlPropertyInfo id="N92" LocationRef="LOCATION1"> 
     <b:SubjectInsuranceCd id="SubjectInsuranceCd-1">BLDG</b:SubjectInsuranceCd> 
     <b:CommlCoverage id="N95"> 
      <b:CoverageCd id="N96">BLDG</b:CoverageCd> 
      <b:Limit id="N97"> 
       <b:FormatInteger id="N98">250000</b:FormatInteger> 
       <b:ValuationCd id="N99">RC</b:ValuationCd> 
      </b:Limit> 
     </b:CommlCoverage> 
     <b:CommlCoverage id="N100"> 
      <b:CoverageCd id="N101">INFL</b:CoverageCd> 
      <b:Limit id="N102"> 
       <b:FormatPct id="N103">100</b:FormatPct> 
      </b:Limit> 
     </b:CommlCoverage> 
    </b:CommlPropertyInfo> 
</b:PropertyInfo 

Wo die gewünschte Ausgabe ist:

<b:PropertyInfo id="N91" xmlns:b="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> 
    <b:CommlPropertyInfo id="N92" LocationRef="LOCATION1"> 
     <b:SubjectInsuranceCd id="SubjectInsuranceCd-1">BLDG</b:SubjectInsuranceCd> 
     <b:CommlCoverage id="N95"> 
      <b:CoverageCd id="N96">BLDG</b:CoverageCd> 
      <b:Limit id="N97"> 
       <b:FormatInteger id="N98">250000</b:FormatInteger> 
       <b:ValuationCd id="N99">RC</b:ValuationCd> 
      </b:Limit> 
      <CommlCoverageSupplement xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> 
       <CoverageSubCd>INFL</CoverageSubCd> 
       <AnnualIncrease>100</AnnualIncrease> 
      </CommlCoverageSupplement> 
     </b:CommlCoverage> 
     <b:CommlCoverage id="N100"> 
      <b:CoverageCd id="N101">INFL</b:CoverageCd> 
      <b:Limit id="N102"> 
       <b:FormatPct id="N103">100</b:FormatPct> 
      </b:Limit> 
     </b:CommlCoverage> 
    </b:CommlPropertyInfo> 
</b:PropertyInfo> 

Ich habe eine xslt, die fast genau das tut, was ich will :

Meine Sorge mit dieser Transformation ist es verwendet rohe Textelemente, um neue Elemente zu erstellen ts - die nicht die richtigen Namespaces haben und daher das von ihr erzeugte XML falsch ist. Aus meiner Lektüre ist mein Verständnis, es ist "schlechte Praxis", Rohtext zu verwenden, um XML-Elemente zu erstellen. Mein Verständnis ist die Verwendung von Rohtext auf diese Weise sollte verwendet werden, wenn Sie XML in ein anderes Format, wie HTML umwandeln.

Also habe ich versucht, die <xsl:element> zu verwenden, um dies zu beheben, aber ich verwende es falsch. Wie ich es geschrieben habe, ist der XSLT-Prozessor lässt mich wissen, dass es ungültig Format ist eine <xsl:element> innerhalb eines anderen verschachtelt haben:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xpath-default-namespace="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> 
    <xsl:strip-space elements="*" /> 

    <!-- element template that copies over elements --> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <!-- "other" template to copy the rest of the nodes --> 
    <xsl:template match="comment() | processing-instruction()"> 
     <xsl:copy /> 
    </xsl:template> 

    <xsl:template 
     match="PropertyInfo/CommlPropertyInfo/CommlCoverage[CoverageCd='BLDG']"> 
     <xsl:variable name="increase"> 
      <xsl:value-of select="../CommlCoverage[CoverageCd='INFL']/Limit/FormatPct" /> 
     </xsl:variable> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
      <xsl:element name="CommlCoverageSupplement" namespace="{namespace-uri()}"> 
       <xsl:element name="CoverageSubCd" namespace="{namespace-uri()}">INFL</xsl:element> 
       <xsl:element name="AnnualIncrease" namespace="{namespace-uri()}"> 
        <xsl:value-of select="$increase" /> 
       </xsl:element> 
      </xsl:element> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

Was ist die richtige XSLT Art und Weise neue verschachtelte Elemente wie diese, während immer noch Namensraum respektiert zu schaffen?

EDIT: Es stellt sich heraus, dass dies nur ein Editor-Fehler war. Sie können tatsächlich verschachtelte xsl erstellen: element-Elemente und die Antwort ist eigentlich in der Frage wieder gepostet hier für Klarheit:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xpath-default-namespace="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> 
    <xsl:strip-space elements="*" /> 

    <!-- element template that copies over elements --> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <!-- "other" template to copy the rest of the nodes --> 
    <xsl:template match="comment() | processing-instruction()"> 
     <xsl:copy /> 
    </xsl:template> 

    <xsl:template 
     match="PropertyInfo/CommlPropertyInfo/CommlCoverage[CoverageCd='BLDG']"> 
     <xsl:variable name="increase"> 
      <xsl:value-of select="../CommlCoverage[CoverageCd='INFL']/Limit/FormatPct" /> 
     </xsl:variable> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
      <xsl:element name="CommlCoverageSupplement" namespace="{namespace-uri()}"> 
       <xsl:element name="CoverageSubCd" namespace="{namespace-uri()}">INFL</xsl:element> 
       <xsl:element name="AnnualIncrease" namespace="{namespace-uri()}"> 
        <xsl:value-of select="$increase" /> 
       </xsl:element> 
      </xsl:element> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

„* Ich habe eine xslt, die fast genau das tut, was ich will * "Was ist das ** genaue ** Ergebnis, das du bekommen möchtest? –

+0

Aktualisiert, um eine Probe xml der gewünschten Ausgabe aufzunehmen. Ich sollte beachten, dass jedes namespace semantische Äquivalent (mit Präfixen oder was auch immer) auch annehmbar ist. Alles, wo die Struktur, die ich bereitgestellt habe, wird erstellt, während sie zu dem Standard-Namespace gehört, den xslt angibt. – Russ

+0

Die Ausgabe, die Sie anzeigen, ist kein wohlgeformtes XML, da das Präfix 'b:' nicht an einen Namespace gebunden ist. Ihre Eingabe hat denselben Fehler. –

Antwort

0

ich beileve, dass, wenn Sie die folgende Namespace-Deklaration hinzu:

xmlns:b="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/" 

zu das xsl:stylesheet Element, und fügen Sie den b: Präfix zu den wörtlichen Ergebniselementen:

<b:CommlCoverageSupplement> 
     <b:CoverageSubCd>INFL</b:CoverageSubCd> 
     <b:AnnualIncrease> 
      <xsl:value-of select="$increase" /> 
     </b:AnnualIncrease> 
    </b:CommlCoverageSupplement> 

Sie die Ausgabe erhalten, die Sie benötigen.


Wenn - aus unerfindlichen Gründen - Sie wollen nicht die zusätzlichen Elemente, die den b: Präfix haben (aber noch im gleichen Namensraum sein), können Sie einfach tun:

 <CommlCoverageSupplement xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1/xml/"> 
     <CoverageSubCd>INFL</CoverageSubCd> 
     <AnnualIncrease> 
      <xsl:value-of select="$increase" /> 
     </AnnualIncrease> 
    </CommlCoverageSupplement> 
Verwandte Themen