在PI XSLT映射中将xml字段转换为键值对。

问题描述 投票:0回答:2

在SAP PI中,我有来自休息服务(web配置器)的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>

为了处理这种 "通用",我想用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>

我找到了一篇文章,用另一种方式来描述它XSLT: 转换NameValue对并转换一个XML。

xslt sap xslt-1.0 sap-xi sap-pi
2个回答
1
投票

这是马丁告诉你的。

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

1
投票
<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>
© www.soinside.com 2019 - 2024. All rights reserved.