0

我有以下xml

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <lst name="somename">
        <node1></node1>
        <node2></node2>
    </lst>
    <result name="somename" count="5">
        <doc>
            <str name="NodeA">ValueA</str>
            <str name="NodeB">ValueB</str>
            <str name="NodeC">ValueC</str>
        </doc>
        <doc>
            <str name="NodeA">ValueD</str>
            <str name="NodeB">ValueE</str>
            <str name="NodeC">ValueF</str>
        </doc>
    </result>
</response>

我想转换成

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <doc>
        <NodeA>ValueA</NodeA>
        <NodeB>ValueB</NodeB>
        <NodeC>ValueC</NodeC>
    </doc>
    <doc>
        <NodeA>ValueD</NodeA>
        <NodeB>ValueE</NodeB>
        <NodeC>ValueF</NodeC>
    </doc>
</response>

如您所见,lst 节点已完全删除,属性值现在已成为节点。

首先,我使用此 xslt 代码删除了 lst 节点。

<?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="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="lst"/>
</xsl:stylesheet>

这给了我这个

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <result name="somename" count="5">
        <doc>
            <str name="NodeA">ValueA</str>
            <str name="NodeB">ValueB</str>
            <str name="NodeC">ValueC</str>
        </doc>
        <doc>
            <str name="NodeA">ValueD</str>
            <str name="NodeB">ValueE</str>
            <str name="NodeC">ValueF</str>
        </doc>
    </result>
</response>

然后使用链接中的这个 xslt [链接] Convert attribute value into element

<?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="response/result/doc">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="token">
        <xsl:element name="{@name}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

但这没有帮助。它给了我这个。

<?xml version="1.0" encoding="utf-8"?>
<doc>ValueAValueBValueC</doc>
<doc>ValueDValueEValueF</doc>

请帮助我完成将属性值转换为节点的第二部分。是否有可能让一个 xslt 做这两件事?

4

3 回答 3

1

您可以在一个非常接近您当前的 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:strip-space elements="*" />

    <!-- copy everything as-is apart from exceptions below -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <!-- delete lst -->
    <xsl:template match="lst"/>

    <!-- strip out the result element but still include its children -->
    <xsl:template match="result">
      <xsl:apply-templates />
    </xsl:template>

    <!-- convert str name="X" to X -->
    <xsl:template match="str">
      <xsl:element name="{@name}">
        <xsl:apply-templates />
      </xsl:element>
    </xsl:template>
</xsl:stylesheet>
于 2013-03-15T14:20:48.560 回答
0

你可以以此为基础

XSL如何复制除称为注释的元素之外的所有元素

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0">

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

    <!-- copy xsd:annotation and children, but don't copy the attributes -->
   <xsl:template match="xsd:annotation">

        <xsd:annotation>
            <xsl:copy-of select="*"/>
        </xsd:annotation>

    </xsl:template> 

</xsl:stylesheet>
于 2013-03-15T14:04:58.483 回答
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:template match="node()">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="lst"/>

  <xsl:template match="str[@name]">
    <xsl:element name="{@name}">
      <xsl:apply-templates  />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

在您的示例输入上运行时,这会产生:

<response>
  <result>
    <doc>
      <NodeA>ValueA</NodeA>
      <NodeB>ValueB</NodeB>
      <NodeC>ValueC</NodeC>
    </doc>
    <doc>
      <NodeA>ValueD</NodeA>
      <NodeB>ValueE</NodeB>
      <NodeC>ValueF</NodeC>
    </doc>
  </result>
</response>
于 2013-03-15T14:25:39.447 回答