我的输入XML:
<Book>
<Chapter>
<Pages value="20" />
<Note1Code value="1" />
<Note1Description value="This is just for reference" />
<Note1Level value="Avg" />
<Note2Code value="2" />
<Note2Description value="This is high important note" />
<Note2Level value="Imp" />
</Chapter>
</Book>
我想转换成:
<Book>
<Chapter>
<Pages value="20" />
<Note>
<Code>1</Code>
<Description>This is just for reference</Description>
<Level>Avg</Level>
</Note
<Note>
<Code>2</Code>
<Description>This is high important note</Description>
<Level>Imp</Level>
</Note
</Chapter>
</Book>
这是我的XSLT:
<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="*[substring(name(), 1, 4) = 'Note' and substring(name(),6) = 'Code']">
<xsl:variable name="pdIdx" select="substring(name(), 5, 1)"/>
<xsl:variable name="pdNode" select="concat('Note',$pdIdx,'Description/@value')"/>
<xsl:variable name="pd" select="$pdNode"/>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="$pdNode"/>
</Description>
</xsl:template>
</xsl:stylesheet>
我试图动态创建节点名称,然后尝试获取select属性中的值。但是,它不起作用。输出为:description标记使用节点名称而不是其值
<Book>
<Chapter>
<Pages value="20"/>
<Code>1</Code>
<Description>Note1Description/@value</Description>
<Node1Description value="This is just for reference"/>
<Note1Level value="Avg"/>
<Code>2</Code>
<Description>Note2Description/@value</Description>
<Node2Description value="This is high important note"/>
<Note2Level value="Imp"/>
</Chapter>
</Book>
最佳答案
这看起来有些混乱,但是我相信可以做到:
<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="*"/>
<xsl:key name="elements" match="*" use="name()"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Note')
and
substring(name(),string-length(name())-3) = 'Code'
and
number(substring-before(substring-after(name(),'Note'),'Code'))]">
<xsl:variable name="Num" select="number(substring-before(substring-after(name(),'Note'),'Code'))"/>
<Note>
<Code><xsl:value-of select="key('elements', concat('Note',$Num,'Code'))/@value"/></Code>
<Description><xsl:value-of select="key('elements', concat('Note',$Num,'Description'))/@value"/></Description>
<Level><xsl:value-of select="key('elements', concat('Note',$Num,'Level'))/@value"/></Level>
</Note>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Note')
and
((substring(name(),string-length(name())-10) = 'Description'
and
number(substring-before(substring-after(name(),'Note'),'Description')))
or (substring(name(),string-length(name())-4) = 'Level'
and
number(substring-before(substring-after(name(),'Note'),'Level'))))
]"/>
</xsl:stylesheet>
关于xml - 将创建的运行时节点名称传递给xsl:value-of select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341824/