0

我有一个这样的 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<variablelist>
    <varinfo id="id-5">
        <vardef>
            <name>A</name>
            <init>
                <compoundinit type="composite">
                    <singleinit>(unsigned char)6</singleinit>
                    <singleinit>(unsigned char)0</singleinit>
                    <compoundinit type="array">
                        <singleinit>(short)400</singleinit>
                        <singleinit>(short)600</singleinit>
                        <singleinit>(short)800</singleinit>
                    </compoundinit>
                    <compoundinit type="array">
                        <singleinit>(short)32767</singleinit>
                        <singleinit>(short)32767</singleinit>
                        <singleinit>(short)32767</singleinit>
                    </compoundinit>
                </compoundinit>
            </init>
        </vardef>
    </varinfo>
    <varinfo id="id-10">
        <vardef>
            <name>B</name>
            <init>
                <compoundinit type="array">
                    <singleinit>(uint32 const volatile)0U</singleinit>
                    <singleinit>(uint32 const volatile)0U</singleinit>
                    <singleinit>(uint32 const volatile)0U</singleinit>
                    <singleinit>(uint32 const volatile)0U</singleinit>
                </compoundinit>
            </init>
        </vardef>
    </varinfo>
    <varinfo id="id-200">
        <vardef>
            <name>C</name>
            <init>
                <compoundinit type="composite">
                    <singleinit>(unsigned char)12</singleinit>
                    <singleinit>(unsigned char)12</singleinit>
                    <compoundinit type="array">
                        <singleinit>(short)900</singleinit>
                        <singleinit>(short)1000</singleinit>
                        <singleinit>(short)1240</singleinit>
                        <singleinit>(short)1750</singleinit>
                    </compoundinit>
                    <compoundinit type="array">
                        <singleinit>(short)512</singleinit>
                        <singleinit>(short)640</singleinit>
                        <singleinit>(short)853</singleinit>
                        <singleinit>(short)1067</singleinit>
                        <singleinit>(short)1280</singleinit>
                    </compoundinit>
                    <compoundinit type="array">
                        <compoundinit type="array">
                            <singleinit>(unsigned char)153</singleinit>
                            <singleinit>(unsigned char)153</singleinit>
                            <singleinit>(unsigned char)153</singleinit>
                            <singleinit>(unsigned char)153</singleinit>
                        </compoundinit>
                        <compoundinit type="array">
                            <singleinit>(unsigned char)153</singleinit>
                            <singleinit>(unsigned char)153</singleinit>
                            <singleinit>(unsigned char)153</singleinit>
                            <singleinit>(unsigned char)153</singleinit>
                        </compoundinit>
                    </compoundinit>
                </compoundinit>
            </init>
        </vardef>
    </varinfo>
</variablelist>

我想要做的是将变量的使用位相加。例如,变量 A 由 2 个“无符号字符”和 6 个“短”数据类型组成。让我们假设“short”等于 16 位,“char”等于 8 位。因此变量 A 有 112 位 (6*16 + 2*8)。

如果我想要以下输出,XSLT 文件会是什么样子:

<?xml version="1.0" encoding="UTF-8"?>
<variablelist>
    <varinfo>
            <name>A</name>
            <size>112</size>
    </varinfo>
    <varinfo>
            <name>B</name>
            <size>128</size>
    </varinfo>
    <varinfo>
            <name>C</name>
            <size>224</size>
    </varinfo>
</variablelist>

目前,我至少设法实现了以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<variablelist>
   <varinfo>
      <name>A</name>
      <size>(unsigned char)6(unsigned char)0(short)400(short)600(short)800(short)32767(short)32767(short)32767</size>
   </varinfo>
   <varinfo>
      <name>B</name>
      <size>(uint32 const volatile)0U(uint32 const volatile)0U(uint32 const volatile)0U(uint32 const volatile)0U</size>
   </varinfo>
   <varinfo>
      <name>C</name>
      <size>(unsigned char)12(unsigned char)12(short)900(short)1000(short)1240(short)1750(short)512(short)640(short)853(short)1067(short)1280(unsigned char)153(unsigned char)153(unsigned char)153(unsigned char)153(unsigned char)153(unsigned char)153(unsigned char)153(unsigned char)153</size>
   </varinfo>
</variablelist>

使用此转换代码:

<?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" indent="yes" />

    <xsl:template match="/">
        <variablelist>
            <xsl:apply-templates select="/variablelist"/>
        </variablelist>
    </xsl:template>

    <xsl:template match="variablelist">
        <xsl:for-each select="varinfo">
            <varinfo>
                <xsl:choose>
                    <xsl:when test="not(current()/vardef/init)">
                        <!-- ... -->
                    </xsl:when>

                    <xsl:when test="current()/vardef/init">
                        <xsl:choose>
                            <xsl:when test="current()/vardef/init/singleinit">
                                <!-- ... -->
                            </xsl:when>

                            <xsl:otherwise>
                                <name>
                                    <xsl:value-of select="current()/vardef/name/."/>
                                </name>
                                <size>
                                    <xsl:for-each select="current()/vardef/init//node()">
                                        <xsl:if test="name() = 'singleinit'">
                                            <xsl:value-of select="."/>
                                        </xsl:if>
                                    </xsl:for-each>
                                </size>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>

                    <xsl:otherwise>
                        <!-- ... -->
                    </xsl:otherwise>
                </xsl:choose>
            </varinfo>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

为了将字符串转换为我的数字,我会使用这样的东西:

<xsl:when test="contains((substring-before((current()/vardef/init/singleinit/.),')')),'char')"> 
    <xsl:value-of select="'8'"/> 
</xsl:when> 

有人知道如何解决我的问题吗?我通过变量或参数尝试了不同的方法,但到目前为止我还没有成功。

谢谢!

PS.:这里问的第一个问题,希望我正确地遵循了你的网络礼仪。有人知道更好的标题吗?

4

1 回答 1

1

尝试这个:

<?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" indent="yes"/>

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

    <xsl:template match="varinfo">
        <xsl:copy>
            <xsl:copy-of select="vardef/name"/>
            <size>
                <xsl:value-of select="8 *count( descendant::singleinit[ contains(.,'(unsigned char)') ] ) +
                            16 * count( descendant::singleinit[ contains(.,'(short)') ] ) +
                            32 *count( descendant::singleinit[ contains(.,'(uint32 const volatile)' ) ] )"/>
            </size>
        </xsl:copy>

    </xsl:template>
</xsl:stylesheet>

这将生成以下输出:

<?xml version="1.0"?>
<variablelist>
        <varinfo><name>A</name><size>112</size></varinfo>
        <varinfo><name>B</name><size>128</size></varinfo>
        <varinfo><name>C</name><size>224</size></varinfo>
</variablelist>

评论:根据您的要求:为了将字符串转换为我的数字,我会使用这样的东西(一些 xsl:当位大小时)。我不建议这样做。这只会让事情变得更复杂。可能的增强取决于您的最终解决方案:
- 将计算移至命名模板。
- 调整不区分有符号和无符号的包含操作。

于 2013-05-16T15:23:07.413 回答