简体   繁体   中英

How to copy just a part of an attribute to the new XML using XSLT 1.0

I need to copy just a part of an attribute to create a new XML, I have this as input:

<START> 
    <LIST>
        <PRODUCT name="ELEMENT1.HKL" code="12" >
            <SPEC>
                <available>yes</available>
            </SPEC>
        </PRODUCT>
        
        <PRODUCT name="ELEMENT2.HKL" code="14" >
            <SPEC>
                <available>yes</available>
            </SPEC>
        </PRODUCT>
        
    </LIST>
</START>

In the match element I need to copy the attribute name but just what is written before the dot,so I would have something like this:

<?xml version="1.0" encoding="UTF-8"?><START>
   <LIST>
       <PRODUCT delivery="done" name="ELEMENT1">
           <SPEC>
               <available>yes</available>
           </SPEC>
       </PRODUCT>
       <PRODUCT code="14" name="ELEMENT2.HKL">
           <SPEC>
               <available>yes</available>
           </SPEC>
       </PRODUCT>
   </LIST>
</START>

And this is the XSLT that Im trying to use:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" 
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

   <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>
   
   <xsl:template match="PRODUCT/@code[.='12']">
       <xsl:attribute name="delivery">done</xsl:attribute> 
       <xsl:attribute name="name">
           <xsl:value-of select="substring-before(., '.')"/>   
       </xsl:attribute> 
   </xsl:template>
</xsl:stylesheet>

But its not working, any idea on how I can make it works?.

Use substring-before(., '.') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM