Xml 如何移动节点';使用XSL将文本内容添加到属性区域?

Xml 如何移动节点';使用XSL将文本内容添加到属性区域?,xml,parsing,xslt,Xml,Parsing,Xslt,我正在读取一个具有下一个结构的XML文件: <?xml version="1.0" standalone="yes"?> <DATA> <ROWS> <ROW ImString="string!" ImSmallint="5"> <NestedROW> <ROW ImInteger="1" ImString="sub record 1"/> <ROW ImInte

我正在读取一个具有下一个结构的XML文件:

<?xml version="1.0" standalone="yes"?>
<DATA>
  <ROWS>
    <ROW ImString="string!" ImSmallint="5">
      <NestedROW>
        <ROW ImInteger="1" ImString="sub record 1"/>
        <ROW ImInteger="2" ImString="sub record 2"/>
      </NestedROW>
      ImShortint="1" ImSingle="6"
    </ROW>
    <ROW ImString="Soy string!" ImSmallint="5">
      <NestedROW>
        <ROW ImInteger="1" ImString="Hi World!"/>
        <ROW ImInteger="2" ImString="Bye World!"/>
      </NestedROW>
      ImShortint="3" ImSingle="5"
    </ROW>
  </ROWS>
</DATA>

ImShortint=“1”ImSingle=“6”
ImShortint=“3”ImSingle=“5”
如您所见,节点行具有文本内容[ImShortint=“3”ImSingle=“5”],它应该位于ImSmallint属性之后,如预期结果所示

我尝试使用一些XSL合并或解析text()以生成和附加属性/值,但没有结果,实际上我有一个不完整的XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="*[text()]">
    <xsl:variable name="text">
      <xsl:apply-templates select="text()"/>
    </xsl:variable>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
    <!--
     Parse or merge text() here after last attribute
     -->
      <xsl:apply-templates select="*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

如何实现下一个XML输出

<?xml version="1.0" standalone="yes"?>
<DATA>
  <ROWS>
    <ROW ImString="string!" ImSmallint="5" ImShortint="1" ImSingle="6">
      <NestedROW>
        <ROW ImInteger="1" ImString="sub record 1"/>
        <ROW ImInteger="2" ImString="sub record 2"/>
      </NestedROW>
    </ROW>
    <ROW ImString="Soy string!" ImSmallint="5" ImShortint="3" ImSingle="5">
      <NestedROW>
        <ROW ImInteger="1" ImString="Hi World!"/>
        <ROW ImInteger="2" ImString="Bye World!"/>
      </NestedROW>
    </ROW>
  </ROWS>
</DATA>

编辑: XML可以包含任意数量的属性,属性名称未知。


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="ROW[ parent::ROWS]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:attribute name="ImShortint">
                <xsl:value-of select=" substring-before(substring-after(.,'&quot;'),'&quot; ')"/>
            </xsl:attribute>
            <xsl:attribute name="ImSingle">
                <xsl:value-of select=" substring-before(substring-after(.,' ImSingle=&quot;'),'&quot;')"/>
            </xsl:attribute>
            <xsl:apply-templates select="NestedROW"/>
        </xsl:copy>
    </xsl:template>
check it.
检查一下。
这样试试

XSLT1.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="ROW">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:call-template name="text2attributes">
            <xsl:with-param name="text" select="normalize-space(text())"/>
        </xsl:call-template>
        <xsl:apply-templates select="*"/>
    </xsl:copy>
</xsl:template>

<xsl:template name="text2attributes">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="' '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)"/>
        <xsl:if test="$token">
            <xsl:attribute name="{substring-before($token, '=&quot;')}">
                <xsl:value-of select="substring-before(substring-after($token, '=&quot;'), '&quot;')"/>
            </xsl:attribute>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="text2attributes">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>


您已将该问题解释为,输入始终按该顺序包含两个名为ImShortInt和ImSingle的属性。我的解释是,它可能包含任何属性,名称未知。你可能是对的,谁知道呢,但这只是说明编写一个好的需求语句有多么困难。顺便说一句,在XSLT2.0中,你可以编写
-指令的xsl:value是多余的。Michael Kay你是对的,XML可以包含任意数量的属性,而名称是未知的。