Exclude attribute from a specific xml element using xslt

后端 未结 3 1447
长情又很酷
长情又很酷 2020-12-09 17:30

I am new in xslt. I have the following problem. I need within an xml, to remove a specific attribute (theAttributein the example) from a specific element (e.g.

相关标签:
3条回答
  • 2020-12-09 17:56

    What is not working? Do you want the same content, just without the @theAtribute?

    If so, make sure your stylesheet has the empty template for @theAtribute, but also has an identity template that copies everything else into the output:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <!--empty template suppresses this attribute-->
        <xsl:template match="@theAtribute" />
        <!--identity template copies everything forward by default-->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    If you only want to suppress certain @theAtribute, then you can make the match criteria more specific. For instance, if you only wanted to remove that attribute from the div who's @id="qaz", then you could use this template:

    <xsl:template match="@theAtribute[../@id='qaz']" />
    

    or this template:

    <xsl:template match="*[@id='qaz']/@theAtribute" />
    

    If you want to remove @theAttribute from all div elements, then change the match expression to:

    <xsl:template match="div/@theAtribute" />
    
    0 讨论(0)
  • 2020-12-09 18:02
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    
            <xsl:output method="xml" encoding="UTF-8" indent="yes" />
            <xsl:param name="status"/>
    
            <!--If attribute is present change it -->
            <xsl:template match="@status" >
                <xsl:attribute name="status" select="$status"/>
            </xsl:template>
            <!-- If attribute is not present add it al last position -->
            <xsl:template match="row[ not( @status ) ]" >
                <xsl:copy>
                    <xsl:apply-templates select="@*"/>
                    <!-- put attribute in the last position -->
                    <xsl:attribute name="status" select="$status"/>
                    <xsl:apply-templates select="node()"/>
                </xsl:copy>
            </xsl:template>
    
            <!--identity template copies everything forward by default-->
            <xsl:template match="@*|node()">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:template>
    
        </xsl:stylesheet>
    

    I have a xml like:

    <row  attribute1="value" >some stuff</row> 
    

    and I'm adding at end of list of attributes a status from an outside value.

    \saxonee\bin\Transform.exe -xsl:my_script.xsl -s:rows.xml status="completed"

    <row current_run="2019-07-29 19:00" status="completed">
    
    0 讨论(0)
  • 2020-12-09 18:03

    inside the select, you can exclude (or include,) the attribute, using the name function.

    For example, <xsl:copy-of select="@*[name(.)!='theAtribute']|node()" />

    0 讨论(0)
提交回复
热议问题