XSLT - 将值转换为标签名称,并组合在一起

XSLT - converting value to a tag name, and grouping together

我不确定是否可行,但我想使用 XSLT 转换以下内容

来源

  <?xml version="1.0" encoding="UTF-8"?>
    <Root>
       <MATNR>3006921</MATNR>
       <MAKTX>VANSH,DE,GOLD G.WHITE 6x900ML</MAKTX>
       <VKORG>AT03</VKORG>
       <VKORG>CH05</VKORG>
       <VKORG>DE14</VKORG>
       <PMATN>
          <AttributeName>AT03</AttributeName>
          <AttributeValue>AT-GU4</AttributeValue>
       </PMATN>
       <PMATN>
          <AttributeName>CH05</AttributeName>
          <AttributeValue>CH-I2Z</AttributeValue>
       </PMATN>
       <PMATN>
          <AttributeName>DE14</AttributeName>
          <AttributeValue>DE-B6K</AttributeValue>
       </PMATN>
       <DWERK>
          <AttributeName>AT03</AttributeName>
          <AttributeValue>AT-GU4</AttributeValue>
       </DWERK>
       <DWERK>
          <AttributeName>CH05</AttributeName>
          <AttributeValue>CH-I2Z</AttributeValue>
       </DWERK>
       <DWERK>
          <AttributeName>DE14</AttributeName>
          <AttributeValue>DE-B6K</AttributeValue>
       </DWERK>
       <EAN11>
          <AttributeName>CAR</AttributeName>
          <AttributeValue>04002448087588</AttributeValue>
       </EAN11>
       <EAN11>
          <AttributeName>EA</AttributeName>
          <AttributeValue>4002448077619</AttributeValue>
       </EAN11>
       <VOLUME>
          <AttributeName>CAR</AttributeName>
          <AttributeValue>17460.432</AttributeValue>
       </VOLUME>
       <VOLUME>
          <AttributeName>EA</AttributeName>
          <AttributeValue>2450.448</AttributeValue>
       </VOLUME>
       <VOLUME>
          <AttributeName>UN</AttributeName>
          <AttributeValue>2450.448</AttributeValue>
       </VOLUME>
       <VOLUME>
          <AttributeName>ZST</AttributeName>
          <AttributeValue>0</AttributeValue>
       </VOLUME>
    </Root>

结果

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <MATNR>3006921</MATNR>
   <MAKTX>VANSH,DE,GOLD G.WHITE 6x900ML</MAKTX>
   <VKORG>AT03</VKORG>
   <VKORG>CH05</VKORG>
   <VKORG>DE14</VKORG>
   <PMATN>
      <AT03>AT-GU4</AT03>
      <CH05>CH-I2Z</CH05>
      <DE14>DE-B6K</DE14>
      <DE14>DE-B6K</DE14>
   </PMATN>
   <DWERK>
      <AT03>AT-GU4</AT03>
      <CH05>CH-I2Z</CH05>
      <DE14>DE-B6K</DE14>
      <DE14>DE-B6K</DE14>
   </DWERK>
   <EAN11>
      <CAR>04002448087588</CAR>
      <EA>4002448077619</EA>
   </EAN11>
   <VOLUME>
      <CAR>17460.432</CAR>
      <EA>2450.448</EA>
      <UN>2450.448</UN>
      <ZST>0</ZST>
   </VOLUME>
</Root>

所以对于 PMATN、DWERK、EAN11、VOLUME,我希望 AttributeName 值成为标签名称,其值取自相关的 AttributeValue。全部分组在一对共同的标签下。

分组和转换步骤是可行的

  <xsl:template match="Root">
      <xsl:copy>
          <xsl:apply-templates select="*[not(AttributeName)]"/>
          <xsl:for-each-group select="*[AttributeName]" group-by="node-name()">
              <xsl:copy>
                  <xsl:apply-templates select="current-group()"/>
              </xsl:copy>
          </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="*[AttributeName]">
      <xsl:element name="{AttributeName}">
          <xsl:value-of select="AttributeValue"/>
      </xsl:element>
  </xsl:template>

通过身份转换处理其余部分。

https://xsltfiddle.liberty-development.net/gWEaSuU 是 XSLT 3 做的。对于 XSLT 2 处理器,您需要拼出身份转换而不是使用 xsl:mode:

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