xml - 将创建的运行时节点名称传递给xsl:value-of select

标签 xml xslt xpath

我的输入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/

相关文章:

XSLT 根据元素值删除重复节点

xml - 按日期排序 xml 时出现问题<xsl :sort select =""/>

android - 需要通过接受 Android 中的所有证书来通过 https 访问 xml 文件

java - JAXB - 解码 OutOfMemory : Java Heap Space

xml - 使用 xslt 排序对给定顺序进行排序

xslt - 在xslt中,选择具有id的项目,该值在变量中

xml - XPath 子串

java - 通过 xpath w.r.t 到其他元素查找元素

python - 如果找不到数据,如何使 XPath 在 Python 中返回 'None'?

java - 如何动态更改ListView中项目的大小?