XSLT - replace the value in one node with the value from it, preceding the marriage

I am trying to write an XSLT transform that replaces a value in one node with the value that it precedes the sibling. Then return the whole document :)

Find occurrences

<package:packageDownloadLocator>http://myunwantedurl</package:packageDownloadLocator>

and replace it with the text value of the outgoing brother

<package:packagePreviewLocator>http://myurl.com</package:packagePreviewLocator>

Thanks for any help or suggestions.

Note. The node to be found does not always exist in every entry.

From this:

    <?xml version="1.0"?>
<SRW:searchRetrieveResponse xmlns:SRW="http://www.loc.gov/zing/srw/" xmlns:DIAG="http://www.loc.gov/zing/srw/diagnostics" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:package="info:srw/extension/13/package-v1.0">
<SRW:records>
<SRW:record>
   <SRW:recordData>
    <dc:dc>
     <dc:title xml:lang="en">Opportunities for involvement for service users and carers at the Open University in Scotland</dc:title>
     <dc:description xml:lang="en">Booklet explaining the different ways service users or carers can assist</dc:description>
     <dc:publisher>Open University</dc:publisher>
     <dc:format>application/pdf</dc:format>
     <dc:type>Narrative Text</dc:type>
     <dc:rights xml:lang="en">Copyright Open University, 2009</dc:rights>
     <dc:subject>health and health care, health care, nursing</dc:subject>
    </dc:dc>
   </SRW:recordData>
   <SRW:extraRecordData>
    <package:packagePreviewLocator>http://myurl.com</package:packagePreviewLocator>
    <package:packageDownloadLocator>http://myunwantedurl</package:packageDownloadLocator>
    <record:record xmlns:record="http://srw.o-r-g.org/schemas/rec/1.0/">
     <record:lastModified>2009-09-29</record:lastModified>
     <record:created>2009-09-29</record:created>
    </record:record>
   </SRW:extraRecordData>
  </SRW:record>
 </SRW:records>
</SRW:searchRetrieveResponse>

For this:

<?xml version="1.0"?>
<SRW:searchRetrieveResponse xmlns:SRW="http://www.loc.gov/zing/srw/" xmlns:DIAG="http://www.loc.gov/zing/srw/diagnostics" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:package="info:srw/extension/13/package-v1.0">
<SRW:records>
<SRW:record>
   <SRW:recordData>
    <dc:dc>
     <dc:title xml:lang="en">Opportunities for involvement for service users and carers at the Open University in Scotland</dc:title>
     <dc:description xml:lang="en">Booklet explaining the different ways service users or carers can assist</dc:description>
     <dc:publisher>Open University</dc:publisher>
     <dc:format>application/pdf</dc:format>
     <dc:type>Narrative Text</dc:type>
     <dc:rights xml:lang="en">Copyright Open University, 2009</dc:rights>
     <dc:subject>health and health care, health care, nursing</dc:subject>
    </dc:dc>
   </SRW:recordData>
   <SRW:extraRecordData>
    <package:packagePreviewLocator>http://myurl.com</package:packagePreviewLocator>
    <package:packageDownloadLocator>http://myurl.com</package:packageDownloadLocator>
    <record:record xmlns:record="http://srw.o-r-g.org/schemas/rec/1.0/">
     <record:lastModified>2009-09-29</record:lastModified>
     <record:created>2009-09-29</record:created>
    </record:record>
   </SRW:extraRecordData>
  </SRW:record>
 </SRW:records>
</SRW:searchRetrieveResponse>
+3
source share
2 answers

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:package="info:srw/extension/13/package-v1.0" >
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="package:packageDownloadLocator/text()">
  <xsl:value-of select="../preceding-sibling::*[1]/text()"/>
 </xsl:template>
</xsl:stylesheet>

when applied to the provided XML document, creates the desired result .

Please note :

  • node .

  • , node.

+5

" " (Google, ). , - , , . , - ( , ).

0

Source: https://habr.com/ru/post/1753876/


All Articles