2016-08-31 3 views
1

Ich habe ein Problem mit der Konvertierung von XML mit XSLT in ein anderes XML.XML mit XSLT ändern verschiedene Werte

ich diesen Kodex vom Management System:

<?xml version="1.0" standalone="yes"?> 
    <DocumentElement> 
     <article> 
      <ordernumber>100002</ordernumber> 
      <mainnumber>100002</mainnumber> 
      <ItmsGrpNam>Lager</ItmsGrpNam> 
      <name>7006CYDU/GM</name> 
      <suppliername>Nachi</suppliername> 
      <active>1</active> 
      <Price>0.000000</Price> 
      <propertyGroupName>1A</propertyGroupName> 
      <propertyOptionName>1B</propertyOptionName> 
      <propertyValueName>1C</propertyValueName> 
      <propertyGroupName1>2A</propertyGroupName1> 
      <propertyOptionName1>2B</propertyOptionName1> 
      <propertyValueName1>2C</propertyValueName1> 
      <propertyGroupName2>3A</propertyGroupName2> 
      <propertyOptionName2>3B</propertyOptionName2> 
      <propertyValueName2>3C</propertyValueName2> 
     </article> 
    </DocumentElement> 

Aber ich brauche dieses Format:

<propertyValue> 
    <propertyGroupName>1A</propertyGroupName> 
    <propertyValueName>1B</propertyValueName> 
    <propertyOptionName>1C</propertyOptionName> 
</propertyValue> 
<propertyValue> 
    <propertyGroupName>2A</propertyGroupName> 
    <propertyValueName>2B</propertyValueName> 
    <propertyOptionName>2C</propertyOptionName> 
</propertyValue> 
<propertyValue> 
    <propertyGroupName>3A</propertyGroupName> 
    <propertyValueName>3B</propertyValueName> 
    <propertyOptionName>3C</propertyOptionName> 
</propertyValue> 

Mein XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<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:template match="/"> 
    <Root> 
    <articles> 
     <xsl:for-each select="DocumentElement/article"> 
     <article> 
      <ordernumber><xsl:value-of select="ordernumber"/></ordernumber> 
      <mainnumber><xsl:value-of select="mainnumber"/></mainnumber> 
      <name><xsl:value-of select="name"/></name> 
      <supplier><xsl:value-of select="suppliername"/></supplier> 
      <tax>19</tax> 
      <prices> 
      <price> 
       <price><xsl:value-of select="Price"/></price> 
      </price> 
      </prices> 
      <active><xsl:value-of select="active"/></active> 
      <category> 
      <categories> 
      <xsl:choose> 
       <xsl:when test="ItmsGrpNam='Lager'">39</xsl:when> 
       <xsl:when test="ItmsGrpNam='Transistor'">40</xsl:when> 
       <xsl:when test="ItmsGrpNam='Dichtring'">41</xsl:when> 
       <xsl:when test="ItmsGrpNam='Schütz'">42</xsl:when> 
       </xsl:choose> 
      </categories> 
     </category> 
     <propertyValue> 
      <propertyGroupName><xsl:value-of select="propertyGroupName"/> </propertyGroupName> 
      <propertyOptionName><xsl:value-of select="propertyOptionName"/></propertyOptionName> 
      <propertyValueName><xsl:value-of select="propertyValueName"/></propertyValueName> 
      </propertyValue> 
     </article> 
     </xsl:for-each> 
    </articles> 
</Root> 
</xsl:template> 
</xsl:stylesheet> 

Das gibt mir natürlich nur eine Eigenschaft Block zurück:

<propertyValue> 
      <propertyGroupName>1A</propertyGroupName> 
      <propertyOptionName>1B</propertyOptionName> 
      <propertyValueName>1C</propertyValueName> 
</propertyValue> 

Ich mag diese Ausgabe:

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
    <articles> 
     <article> 
      <ordernumber>100002</ordernumber> 
      <mainnumber>100002</mainnumber> 
      <name>7006CYDU/GM</name> 
      <supplier>Nachi</supplier> 
      <tax>19</tax> 
      <prices> 
       <price> 
        <price>0.000000</price> 
       </price> 
      </prices> 
      <active>1</active> 
      <category> 
       <categories>39</categories> 
      </category> 
      <propertyValue> 
       <propertyGroupName>1A</propertyGroupName> 
       <propertyOptionName>1B</propertyOptionName> 
       <propertyValueName>1C</propertyValueName> 
      </propertyValue> 
      <propertyValue> 
       <propertyGroupName>2A</propertyGroupName> 
       <propertyOptionName>2B</propertyOptionName> 
       <propertyValueName>2C</propertyValueName> 
      </propertyValue> 
      <propertyValue> 
       <propertyGroupName>3A</propertyGroupName> 
       <propertyOptionName>3B</propertyOptionName> 
       <propertyValueName>3C</propertyValueName> 
      </propertyValue> 
     </article> 
    </articles> 
</Root> 

Ist es möglich, eine Abfrage zu machen dieses Format von allen anderen propertyGroupName1, propertyGroupName2 zu erhalten [...]?

chrisen

+0

** 1 ** Wird immer genau drei Gruppen von Eigenschaften sein oder kann es eine beliebige Nummer sein? - ** 2. ** Bitte posten Sie die ** genaue ** Ausgabe (in voller Länge), die Sie erhalten möchten. –

+0

Nein, manchmal gibt es 2 Gruppen, manchmal 20 – chrisen

+0

Der vollständige Ausgabecode wird zur Hauptfrage bearbeitet – chrisen

Antwort

0

würde ich vorschlagen, Sie versuchen es auf diese Weise:.

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="*"/> 

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

<xsl:template match="/DocumentElement"> 
    <Root> 
     <articles> 
      <xsl:apply-templates/> 
     </articles> 
    </Root> 
</xsl:template> 

<xsl:template match="article"> 
    <xsl:copy> 
     <xsl:apply-templates select="ordernumber | mainnumber | name | suppliername"/> 
     <tax>19</tax> 
     <xsl:apply-templates select="Price"/> 
     <xsl:apply-templates select="active"/> 
     <xsl:apply-templates select="ItmsGrpNam"/> 
     <xsl:for-each select="*[starts-with(name(),'propertyGroupName')]"> 
      <propertyValue> 
       <propertyGroupName> 
        <xsl:value-of select="."/> 
       </propertyGroupName> 
       <propertyOptionName> 
        <xsl:value-of select="following-sibling::*[starts-with(name(),'propertyOptionName')][1]"/>    
       </propertyOptionName> 
       <propertyValueName> 
        <xsl:value-of select="following-sibling::*[starts-with(name(),'propertyValueName')][1]"/>        
       </propertyValueName> 
      </propertyValue> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="suppliername"> 
    <supplier> 
     <xsl:value-of select="."/> 
    </supplier> 
</xsl:template> 

<xsl:template match="Price"> 
    <prices> 
     <price> 
      <price> 
       <xsl:value-of select="."/> 
      </price> 
     </price> 
    </prices> 
</xsl:template> 

<xsl:template match="ItmsGrpNam"> 
    <category> 
     <categories> 
      <xsl:choose> 
       <xsl:when test=".='Lager'">39</xsl:when> 
       <xsl:when test=".='Transistor'">40</xsl:when> 
       <xsl:when test=".='Dichtring'">41</xsl:when> 
       <xsl:when test=".='Schütz'">42</xsl:when> 
      </xsl:choose> 
     </categories> 
    </category> 
</xsl:template> 

</xsl:stylesheet>