我遇到了使用XSLT将XML转换为另一种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 :(得分: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>