重命名节点并将其上移

时间:2019-11-07 20:55:16

标签: xslt

如果节点值是某个值,我想重命名节点并将其上移一个级别。

到目前为止,我写了这篇文章:

awk '{if(index($4""$5, "*") != 0){print}}' file

输入:

import datetime
from datetime import date
date1 = '2019-01-01'
date2 = '2019-01-30'
start = datetime.datetime.strptime(date1,'%Y-%m-%d')
end = datetime.datetime.strptime(date2,'%Y-%m-%d')
step = datetime.timedelta(days=1)
while start <= end:
    daterange = print(start.strftime('%Y%m%d'))
    start += step   
type(daterange)

输出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8"/>

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

    <xsl:template match="stock/images/image[main_image = 1]">
    <primary>
         <xsl:apply-templates />
    </primary>     
    </xsl:template>

</xsl:stylesheet>

这几乎很好,但是我想将<root> <stock> <code>Apple</code> <images> <image> <img_file>1.jpg</img_file> <main_image>1</main_image> </image> <image> <img_file>2.jpg</img_file> <main_image>0</main_image> </image> </images> </stock> </root> 节点上移一层,位于<root> <stock> <code>Apple</code> <images> <primary> <img_file>1.jpg</img_file> <main_image>1</main_image> </primary> <image> <img_file>2.jpg</img_file> <main_image>0</main_image> </image> </images> </stock> </root> 之外,例如:

<primary>

2 个答案:

答案 0 :(得分:0)

怎么样:

XSLT 1.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="stock">
    <xsl:copy>
        <xsl:apply-templates select="code"/>
        <primary>
            <xsl:apply-templates select="images/image[main_image = 1]/*"/>
        </primary>
        <images>
            <xsl:apply-templates select="images/image[not(main_image = 1)]"/>
        </images>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这假设主图像始终存在。否则,您需要这样做:

XSLT 1.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="stock">
    <xsl:copy>
        <xsl:apply-templates select="code"/>
        <xsl:apply-templates select="images/image[main_image = 1]" mode="primary"/>
        <images>
            <xsl:apply-templates select="images/image[not(main_image = 1)]"/>
        </images>
    </xsl:copy>
</xsl:template>

<xsl:template match="image" mode="primary">
    <primary>
        <xsl:apply-templates/>
    </primary>     
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

一种方法是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="stock[images[image[main_image = 1]]]">
        <xsl:copy>
            <xsl:apply-templates select="@* | *[not(self::images)]"/>
            <xsl:apply-templates select="images/image[main_image = 1]"/>
            <xsl:apply-templates select="images"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="images">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()[not(self::image[main_image = 1])]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="stock/images/image[main_image = 1]">
        <primary>
             <xsl:apply-templates />
        </primary>     
    </xsl:template>

</xsl:stylesheet>