我有这样的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个“unsigned char”和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:这是第一个问题,希望我能正确地遵循你的网络礼节。并且有人想要一个更好的头衔吗?答案 0 :(得分: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:用于位大小)。
我不推荐。这只会让事情变得更复杂。根据您的最终解决方案可能的增强功能:
- 将计算移动到命名模板中
- 调整无法区分有符号和无符号的包含操作。