在 PI XSLT 映射中将 xml 字段转换为 key/value 对
Transform xml fields to key/value pairs in PI XSLT mapping
在 SAP PI 中,我有来自休息服务(网络配置器)的 xml 个文件,这些文件的字段可能因产品而异。例如,产品 A 有颜色、高度和宽度,产品 B 有颜色、高度、宽度和深度。
传入示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Products>
<Product>
<Color>Black</Color>
<Height>2000</Height>
<Width>1000</Width>
</Product>
</Products>
</Order>
为了处理这个问题 'generic' 我想通过 1.0 XSL 转换将字段转换为某种键/值对结构。
需要示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Products>
<Product>
<Var>
<VarName>Color</VarName>
<VarValue>Black</VarValue>
</Var>
<Var>
<VarName>Height</VarName>
<VarValue>2000</VarValue>
</Var>
<Var>
<VarName>Width</VarName>
<VarValue>1000</VarValue>
</Var>
</Product>
</Products>
</Order>
我找到了一篇相反描述的文章
这是马丁告诉你的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="Product/*">
<Var>
<VarName>
<xsl:value-of select="name()"/>
</VarName>
<VarValue>
<xsl:value-of select="."/>
</VarValue>
</Var>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Products">
<xsl:copy>
<xsl:for-each select="Product">
<Product>
<xsl:for-each select="./*">
<Var>
<VarName><xsl:value-of select="local-name()"/></VarName>
<VarValue><xsl:value-of select="."/></VarValue>
</Var>
</xsl:for-each>
</Product>
</xsl:for-each>
</xsl:copy>
</xsl:template>
在 SAP PI 中,我有来自休息服务(网络配置器)的 xml 个文件,这些文件的字段可能因产品而异。例如,产品 A 有颜色、高度和宽度,产品 B 有颜色、高度、宽度和深度。
传入示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Products>
<Product>
<Color>Black</Color>
<Height>2000</Height>
<Width>1000</Width>
</Product>
</Products>
</Order>
为了处理这个问题 'generic' 我想通过 1.0 XSL 转换将字段转换为某种键/值对结构。
需要示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Products>
<Product>
<Var>
<VarName>Color</VarName>
<VarValue>Black</VarValue>
</Var>
<Var>
<VarName>Height</VarName>
<VarValue>2000</VarValue>
</Var>
<Var>
<VarName>Width</VarName>
<VarValue>1000</VarValue>
</Var>
</Product>
</Products>
</Order>
我找到了一篇相反描述的文章
这是马丁告诉你的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="Product/*">
<Var>
<VarName>
<xsl:value-of select="name()"/>
</VarName>
<VarValue>
<xsl:value-of select="."/>
</VarValue>
</Var>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Products">
<xsl:copy>
<xsl:for-each select="Product">
<Product>
<xsl:for-each select="./*">
<Var>
<VarName><xsl:value-of select="local-name()"/></VarName>
<VarValue><xsl:value-of select="."/></VarValue>
</Var>
</xsl:for-each>
</Product>
</xsl:for-each>
</xsl:copy>
</xsl:template>