Xml 如何在XSLT中删除整个文档中相同属性的子集

Xml 如何在XSLT中删除整个文档中相同属性的子集,xml,xslt,Xml,Xslt,在XSLT处理过程中,我需要在整个文档中删除属性的子集。在下面的XML示例中,我想删除@id属性,因为它只包含数字。对于@id的其余部分,它们不会更改 <?xml-stylesheet type="text/xsl" href="recursion2.xsl" ?> <list> <book id="B1"> <label>1</label> <title id="1">A Good

在XSLT处理过程中,我需要在整个文档中删除属性的子集。在下面的XML示例中,我想删除
@id
属性,因为它只包含数字。对于
@id
的其余部分,它们不会更改

<?xml-stylesheet type="text/xsl" href="recursion2.xsl" ?>

<list>
    <book id="B1">
        <label>1</label>
        <title id="1">A Good Story</title>
        <author>James Soul</author>
    </book>
    <book id="B2">
        <label>2</label>
        <title id="21">The Perfect Storm</title>
        <author>Laura Smith</author>
    </book>
    <journal id="J1">
        <label>3</label>
        <citation id="3">Tom Lane. The smart computation method. 2003;23(5):123-128.</citation>
    </journal>
    <journal id="J2">
        <label>4</label>
        <citation id="122">Luna Shen. The identification of new gene, SMACT4. 2010;10(2):23-38. </citation>
    </journal>
</list>
根据的提示,我找到了我想要的解决方案:

<xsl:template match="@id[string(number(substring(.,1,1))) !='NaN']" /> 

此代码将删除所有以数字值开头的
@id
,这对于我的需要已经足够了。原来的帖子里有很多很好的解释

根据的提示,我找到了我想要的解决方案:

<xsl:template match="@id[string(number(substring(.,1,1))) !='NaN']" /> 


此代码将删除所有以数字值开头的
@id
,这对于我的需要已经足够了。原来的帖子里有很多很好的解释

您可以尝试使用以下模板,仅当值不是数字时,该模板才会处理
@id

<xsl:template match="@id">
    <xsl:if test="not(number(.))">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:if>
</xsl:template>

完整XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

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

    <xsl:template match="@id">
        <xsl:if test="not(number(.))">
            <xsl:copy>
                <xsl:apply-templates />
            </xsl:copy>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出

<list>
    <book id="B1">
        <label>1</label>
        <title>A Good Story</title>
        <author>James Soul</author>
    </book>
    <book id="B2">
        <label>2</label>
        <title>The Perfect Storm</title>
        <author>Laura Smith</author>
    </book>
    <journal id="J1">
        <label>3</label>
        <citation>Tom Lane. The smart computation method. 2003;23(5):123-128.</citation>
    </journal>
    <journal id="J2">
        <label>4</label>
        <citation>Luna Shen. The identification of new gene, SMACT4. 2010;10(2):23-38. </citation>
    </journal>
</list>

1.
好故事
詹姆斯·苏尔
2.
完美风暴
劳拉史密斯
3.
汤姆·莱恩。智能计算方法。2003;23(5):123-128.
4.
沈露娜。新基因SMACT4的鉴定。2010;10(2):23-38. 

您可以尝试使用以下模板,仅当值不是数字时,该模板才会处理
@id

<xsl:template match="@id">
    <xsl:if test="not(number(.))">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:if>
</xsl:template>

完整XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

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

    <xsl:template match="@id">
        <xsl:if test="not(number(.))">
            <xsl:copy>
                <xsl:apply-templates />
            </xsl:copy>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出

<list>
    <book id="B1">
        <label>1</label>
        <title>A Good Story</title>
        <author>James Soul</author>
    </book>
    <book id="B2">
        <label>2</label>
        <title>The Perfect Storm</title>
        <author>Laura Smith</author>
    </book>
    <journal id="J1">
        <label>3</label>
        <citation>Tom Lane. The smart computation method. 2003;23(5):123-128.</citation>
    </journal>
    <journal id="J2">
        <label>4</label>
        <citation>Luna Shen. The identification of new gene, SMACT4. 2010;10(2):23-38. </citation>
    </journal>
</list>

1.
好故事
詹姆斯·苏尔
2.
完美风暴
劳拉史密斯
3.
汤姆·莱恩。智能计算方法。2003;23(5):123-128.
4.
沈露娜。新基因SMACT4的鉴定。2010;10(2):23-38. 

我也尝试了这段代码
,它没有任何作用我也尝试了这段代码
,它没有任何作用如果你在
id
属性上进行匹配,就像你做的那样,在谓词中你可以简单地使用
来处理匹配的属性,而不是
子字符串(../@id,1,1)
你只需要
子字符串(,1,1)
。马丁,你完全正确。我将更新上面的代码。如果您在
id
属性上进行匹配,您可以在谓词中简单地使用
来处理匹配的属性,而不是
子字符串(../@id,1,1)
您只需要
子字符串(,1,1)
。Martin,您完全正确。我会更新上面的代码。