2016-08-31 7 views
1

XSLTを使用してXMLを別のXMLに変換する際に問題があります。XSLTの異なるXMLを変更する値

私は管理システムから、このコードを取得する:

<?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> 

しかし、私はこのフォーマットが必要になります。

<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> 

マイ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> 

もちろん、これは私に一つだけの特性を提供しますブロックバック:

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

私は、この出力したい:[...]

<?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> 

は、他のすべてのpropertyGroupName1、propertyGroupName2からこのフォーマットを取得するクエリを作成することが可能ですか?

chrisen

+0

** 1 **常にプロパティの正確3グループがあるか、そこにすることができます任意の数字ですか? - ** 2。**取得したい**完全な**出力を(完全に)投稿してください。 –

+0

いいえ時々2つのグループがあり、時には20 – chrisen

+0

メインの質問にフル出力コードが編集されています – chrisen

答えて

0

私はあなたがそれをこのようにしてみてくださいお勧めします:。

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> 
関連する問題