删除节点和具有位置值的 ID 属性
Remove a node and an ID attribute with position value
我有一个 XML 文件:
<ROWS>
<ROW type="x" id="1">
<Test>text</Test>
</ROW>
<ROW type="x" id="1">
<Test>text</Test>
</ROW>
<ROW type="y" id="2">
<Test>text</Test>
</ROW>
<ROW type="x" id="3">
<Test>text</Test>
</ROW>
</ROWS>
我必须删除属性类型为 "y" 且属性 ID 值应按递增顺序排列的行:
<ROWS>
<ROW type="x" id="1">
<Test>text</Test>
</ROW>
<ROW type="x" id="2">
<Test>text</Test>
</ROW>
<ROW type="x" id="3">
<Test>text</Test>
</ROW>
</ROWS>
我尝试使用以下 XSLT:
<xsl:template match="ROWS/ROW[not(@type = 'y')]">
<xsl:variable name="RowID">
<xsl:number/>
</xsl:variable>
<ROW id="{$RowID}" type="{@type}">
<xsl:apply-templates/>
</ROW>
</xsl:template>
<xsl:template match="ROW[@type='y']"/>
但是没有用,我也用了优先功能,但是没有成功。
有人可以帮忙吗?最重要的部分是 id 值,即使在删除任何类型的行后它也应该是递增的,例如在这种情况下它是 "y".
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/ROWS">
<xsl:copy>
<xsl:apply-templates select="ROW[@type!='y']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROW">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="id">
<!-- here were have only proper elements in context, that's why we can use position() -->
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have to remove the row with attribute type "y" and attribute id
value should be in in increment order
我建议你这样试试:
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="*"/>
<xsl:template match="/ROWS">
<xsl:copy>
<xsl:apply-templates select="ROW[not(@type='y')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROW">
<ROW type="{@type}" id="{position()}">
<xsl:copy-of select="node()"/>
</ROW>
</xsl:template>
</xsl:stylesheet>
我有一个 XML 文件:
<ROWS>
<ROW type="x" id="1">
<Test>text</Test>
</ROW>
<ROW type="x" id="1">
<Test>text</Test>
</ROW>
<ROW type="y" id="2">
<Test>text</Test>
</ROW>
<ROW type="x" id="3">
<Test>text</Test>
</ROW>
</ROWS>
我必须删除属性类型为 "y" 且属性 ID 值应按递增顺序排列的行:
<ROWS>
<ROW type="x" id="1">
<Test>text</Test>
</ROW>
<ROW type="x" id="2">
<Test>text</Test>
</ROW>
<ROW type="x" id="3">
<Test>text</Test>
</ROW>
</ROWS>
我尝试使用以下 XSLT:
<xsl:template match="ROWS/ROW[not(@type = 'y')]">
<xsl:variable name="RowID">
<xsl:number/>
</xsl:variable>
<ROW id="{$RowID}" type="{@type}">
<xsl:apply-templates/>
</ROW>
</xsl:template>
<xsl:template match="ROW[@type='y']"/>
但是没有用,我也用了优先功能,但是没有成功。
有人可以帮忙吗?最重要的部分是 id 值,即使在删除任何类型的行后它也应该是递增的,例如在这种情况下它是 "y".
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/ROWS">
<xsl:copy>
<xsl:apply-templates select="ROW[@type!='y']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROW">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="id">
<!-- here were have only proper elements in context, that's why we can use position() -->
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:copy-of select="*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have to remove the row with attribute type "y" and attribute id value should be in in increment order
我建议你这样试试:
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="*"/>
<xsl:template match="/ROWS">
<xsl:copy>
<xsl:apply-templates select="ROW[not(@type='y')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ROW">
<ROW type="{@type}" id="{position()}">
<xsl:copy-of select="node()"/>
</ROW>
</xsl:template>
</xsl:stylesheet>