XSLT: How to change an attribute value during ?

后端 未结 8 471
南旧
南旧 2020-11-29 01:14

I have an XML document, and I want to change the values for one of the attributes.

First I copied everything from input to output using:



        
相关标签:
8条回答
  • 2020-11-29 02:10

    If your source XML document has its own namespace, you need to declare the namespace in your stylesheet, assign it a prefix, and use that prefix when referring to the elements of the source XML - for example:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml">
    
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
    
    <!-- identity transform -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- exception-->    
    <xsl:template match="xhtml:property/@type">
        <xsl:attribute name="type">
            <xsl:text>some new value</xsl:text> 
        </xsl:attribute>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Or, if you prefer:

    ...
    <!-- exception-->    
    <xsl:template match="@type[parent::xhtml:property]">
      <xsl:attribute name="type">
            <xsl:text>some new value</xsl:text> 
      </xsl:attribute>
    </xsl:template>
    ...
    

    ADDENDUM: In the highly unlikely case where the XML namespace is not known beforehand, you could do:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
    
    <!-- identity transform -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- exception -->
    <xsl:template match="*[local-name()='property']/@type">
        <xsl:attribute name="type">
            <xsl:text>some new value</xsl:text> 
        </xsl:attribute>
    </xsl:template>
    

    Of course, it's very difficult to imagine a scenario where you would know in advance that the source XML document contains an element named "property", with an attribute named "type" that needs replacing - but still not know the namespace of the document. I have added this mainly to show how your own solution could be streamlined.

    0 讨论(0)
  • 2020-11-29 02:12

    I also came across same issue and i solved it as follows:

    <!-- identity transform -->
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    
    <!-- copy property element while only changing its type attribute -->
    <xsl:template match="property">
      <xsl:copy>
        <xsl:attribute name="type">
          <xsl:value-of select="'your value here'"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*[not(local-name()='type')]|node()"/>
      </xsl:copy>
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题