Search code examples
xmlxsltoxygenxml

How do I bulk set XML attributes (that area already set)?


I have an xml file, root.xml:

    <root>
        <procedure
        topic-file="Procedure1"
        status="Undefined">
        <title> Procedure Number 1 </title>
    </procedure>
    <procedure
        topic-file="Procedure2"
        status="Undefined">
        <title> Procedure Number 2 </title>
    </procedure>
    <procedure
        topic-file="Procedure3"
        status="Undefined">
        <title> Procedure Number 3 </title>
    </procedure>
    <procedure
        topic-file="Procedure4"
        status="Undefined">
        <title> Procedure Number 4 </title>
    </procedure>
</root>

Note that I'm keeping track of 4 procedures. I want to change the status of 2 procedures in one shot. What I want to change is noted in this XML file, statusByTitle.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <statuses>
      <status topic-file="Procedure1">Complete</status>
      <status topic-file="Procedure3">Draft</status>
    </statuses>

I want to change the statuses of both procedures 1 and 3 as indicated in one shot, so I created this XSLT transform:

<xsl:stylesheet version="2.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="*"/>

    <!-- set up the key -->
    <xsl:key name="statusByTitle" match="status" use="/topic-file" />

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

    <!-- use the key to set the attribute of the correct procedure -->

      <xsl:template 
         match="item[key('statusByTitle', status,  document('statusByTitle.xml'))]/@status">
      <xsl:attribute name="status">
        <xsl:value-of select="key('statusByTitle', ../status, document('statusByTitle.xml'))" />
    </xsl:attribute>
</xsl:template>

I run this transform in Oxygen using Saxon-HE 9.5.1.7 and the output file is the same as the input file. I have stared at this for a while and can't find an error. Have I misunderstood keys somehow?


Solution

  • <xsl:key name="statusByTitle" match="status" use="/topic-file" /> should <xsl:key name="statusByTitle" match="status" use="@topic-file" />. And then you have the wrong element name item instead of procedure:

      <xsl:template 
             match="procedure[key('statusByTitle', @topic-file,  document('statusByTitle.xml'))]/@status">
          <xsl:attribute name="status" select="key('statusByTitle', ../@topic-file, document('statusByTitle.xml'))" />
    </xsl:template>
    

    All corrections together I end up with

    <xsl:stylesheet version="2.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:param name="status-url" select="'test2015080705.xml'"/>
        <xsl:param name="status-doc" select="doc($status-url)"/>
    
        <!-- set up the key -->
        <xsl:key name="statusByTitle" match="status" use="@topic-file" />
    
        <!-- identity transform -->
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <!-- use the key to set the attribute of the correct procedure -->
    
       <xsl:template 
             match="procedure[key('statusByTitle', @topic-file,  $status-doc)]/@status">
          <xsl:attribute name="status" select="key('statusByTitle', ../@topic-file, $status-doc)" />
       </xsl:template>
    
    </xsl:stylesheet>