XSLT样式表用于从提供的XML中获取id和number值。

huangapple go评论117阅读模式
英文:

Xslt stylesheet to get the id and number value from the provided XML

问题

我已经收到一个包含多个记录的SOAP XML文件,其中我需要提取Id、CaseNumber等数据。此外,这些记录在"sf:Case_Responses_GCC_r"标签中有多个"records"标签。我需要创建一个XSLT文件,可以帮助我将这个SOAP XML转换为带有这些值的普通XML。

以下是SOAP XML的示例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  3. xmlns="urn:enter.soap.force.com"
  4. xmlns:sf="urn:sobject.enter.soap.force.com"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <soapenv:Body>
  7. <queryMoreResponse>
  8. <result>
  9. <done>false</done>
  10. <records xsi:type="sf:Case">
  11. <sf:Id>6896SDRGrt868</sf:Id>
  12. <sf:PolicyNumber>445353566</sf:PolicyNumber>
  13. <sf:Case_Responses_GCC__r>
  14. <records xsi:type="sf:Response_GCC__c">
  15. <sf:Id xsi:nil="true"/>
  16. <sf:Question_GCC__c>Available to question?</sf:Question_GCC__c>
  17. <sf:Response_GCC__c>Yes</sf:Response_GCC__c>
  18. </records>
  19. <records xsi:type="sf:Response_GCC__c">
  20. <sf:Id xsi:nil="true"/>
  21. <sf:Question_GCC__c>Relationship</sf:Question_GCC__c>
  22. <sf:Response_GCC__c>Self</sf:Response_GCC__c>
  23. </records>
  24. </sf:Case_Responses_GCC__r>
  25. </records>
  26. <records xsi:type="sf:Case">
  27. <sf:Id>5003L005UCcfVVS</sf:Id>
  28. <sf:PolicyNumber>87768978</sf:PolicyNumber>
  29. <sf:Case_Responses_GCC__r>
  30. <records xsi:type="sf:Response_GCC__c">
  31. <sf:Id xsi:nil="true"/>
  32. <sf:Question_GCC__c>Available to question?</sf:Question_GCC__c>
  33. <sf:Response_GCC__c>No</sf:Response_GCC__c>
  34. </records>
  35. <records xsi:type="sf:Response_GCC__c">
  36. <sf:Id xsi:nil="true"/>
  37. <sf:Question_GCC__c>Relationship</sf:Question_GCC__c>
  38. <sf:Response_GCC__c>Father</sf:Response_GCC__c>
  39. </records>
  40. </sf:Case_Responses_GCC__r>
  41. </records>
  42. </result>
  43. </queryMoreResponse>
  44. </soapenv:Body>
  45. </soapenv:Envelope>

以下是我的XSLT示例:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet version="2.0" xpath-default-namespace="urn:enter.soap.force.com"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns:urn="urn:enter.soap.force.com"
  5. xmlns:sf="urn:sobject.enter.soap.force.com"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsi sf urn">
  7. <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
  8. <xsl:strip-space elements="*" />
  9. <xsl:template match="/">
  10. <report>
  11. <reportid>
  12. <xsl:value-of select="//records/sf:Id"/>
  13. </reportid>
  14. <policynumber>
  15. <xsl:value-of select="//records/sf:PolicyNumber"/>
  16. </policynumber>
  17. </report>
  18. </xsl:template>
  19. </xsl:stylesheet>

由于我对XSLT格式数据的理解有限,所以我只能提取一些内容,但这些内容没有标签,只是纯文本。

我需要将输出转换为以下XML格式,每个记录都在一个数组中,因为我将循环遍历它们并将其转换为JSON格式:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <lic lang="en">
  3. <report>
  4. <reportversion>1</reportversion>
  5. <reportid>6896SDRGrt868</reportid>
  6. <policynum>445353566</policynum>
  7. <primarysourcecountry>IN</primarysourcecountry>
  8. <client>
  9. <summary>
  10. Available to question?: Yes
  11. Relationship: Self
  12. </summary>
  13. </client>
  14. </report>
  15. <report>
  16. <reportversion>1</reportversion>
  17. <reportid>5003L005UCcfVVS</reportid>
  18. <policynum>87768978</policynum>
  19. <primarysourcecountry>IN</primarysourcecountry>
  20. <client>
  21. <summary>
  22. Available to question?: No
  23. Relationship: Father
  24. </summary>
  25. </client>
  26. </report>
  27. </lic>
英文:

I have been provided a Soap XML file which contains multiple records from where I need to fetch the data of Id,CaseNumber etc. Also this records has multiple "records" tag within "sf:Case_Responses_GCC_r" tag. I need to create an XSLT file which could help me in transforming this SOAP XML into the plain XML with those values.

The SOAP XML

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
  3. xmlns=&quot;urn:enter.soap.force.com&quot;
  4. xmlns:sf=&quot;urn:sobject.enter.soap.force.com&quot;
  5. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  6. &lt;soapenv:Body&gt;
  7. &lt;queryMoreResponse&gt;
  8. &lt;result&gt;
  9. &lt;done&gt;false&lt;/done&gt;
  10. &lt;records xsi:type=&quot;sf:Case&quot;&gt;
  11. &lt;sf:Id&gt;6896SDRGrt868&lt;/sf:Id&gt;
  12. &lt;sf:PolicyNumber&gt;445353566&lt;/sf:PolicyNumber&gt;
  13. &lt;sf:Case_Responses_GCC__r&gt;
  14. &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
  15. &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
  16. &lt;sf:Question_GCC__c&gt;Available to question?&lt;/sf:Question_GCC__c&gt;
  17. &lt;sf:Response_GCC__c&gt;Yes&lt;/sf:Response_GCC__c&gt;
  18. &lt;/records&gt;
  19. &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
  20. &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
  21. &lt;sf:Question_GCC__c&gt;Relationship&lt;/sf:Question_GCC__c&gt;
  22. &lt;sf:Response_GCC__c&gt;Self&lt;/sf:Response_GCC__c&gt;
  23. &lt;/records&gt;
  24. &lt;/sf:Case_Responses_GCC__r&gt;
  25. &lt;/records&gt;
  26. &lt;records xsi:type=&quot;sf:Case&quot;&gt;
  27. &lt;sf:Id&gt;5003L005UCcfVVS&lt;/sf:Id&gt;
  28. &lt;sf:PolicyNumber&gt;87768978&lt;/sf:PolicyNumber&gt;
  29. &lt;sf:Case_Responses_GCC__r&gt;
  30. &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
  31. &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
  32. &lt;sf:Question_GCC__c&gt;Available to question?&lt;/sf:Question_GCC__c&gt;
  33. &lt;sf:Response_GCC__c&gt;No&lt;/sf:Response_GCC__c&gt;
  34. &lt;/records&gt;
  35. &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
  36. &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
  37. &lt;sf:Question_GCC__c&gt;Relationship&lt;/sf:Question_GCC__c&gt;
  38. &lt;sf:Response_GCC__c&gt;Father&lt;/sf:Response_GCC__c&gt;
  39. &lt;/records&gt;
  40. &lt;/sf:Case_Responses_GCC__r&gt;
  41. &lt;/records&gt;
  42. &lt;/result&gt;
  43. &lt;/queryMoreResponse&gt;
  44. &lt;/soapenv:Body&gt;
  45. &lt;/soapenv:Envelope&gt;

My XSLT

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
  2. &lt;xsl:stylesheet version=&quot;2.0&quot; xpath-default-namespace=&quot;urn:enter.soap.force.com&quot;
  3. xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  4. xmlns:urn=&quot;urn:enter.soap.force.com&quot;
  5. xmlns:sf=&quot;urn:sobject.enter.soap.force.com&quot;
  6. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; exclude-result-prefixes=&quot;xsi sf urn&quot;&gt;
  7. &lt;xsl:output method=&quot;html&quot; doctype-public=&quot;XSLT-compat&quot; omit-xml-declaration=&quot;yes&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot; /&gt;
  8. &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  9. &lt;xsl:template match=&quot;/&quot;&gt;
  10. &lt;report&gt;
  11. &lt;reportid&gt;
  12. &lt;xsl:value-of select=&quot;//records/sf:Id&quot;/&gt;
  13. &lt;/reportid&gt;
  14. &lt;policynumber&gt;
  15. &lt;xsl:value-of select=&quot;//records/sf:PolicyNumber&quot;/&gt;
  16. &lt;/policynumber&gt;
  17. &lt;/report&gt;
  18. &lt;/xsl:template&gt;
  19. &lt;/xsl:stylesheet&gt;

As, I don't have much understanding of this XSLT format data, still I was able to extract something, but still that does not make sense as it does not have the tag with them just plain.

I need an output in XML like this each record in an array , as I will be looping over them and converting it to JSON format.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;lic lang=&quot;en&quot;&gt;
  3. &lt;report&gt;
  4. &lt;reportversion&gt;1&lt;/reportversion&gt;
  5. &lt;reportid&gt;6896SDRGrt868&lt;/reportid&gt;
  6. &lt;policynum&gt;445353566&lt;/policynum&gt;
  7. &lt;primarysourcecountry&gt;IN&lt;/primarysourcecountry&gt;
  8. &lt;client&gt;
  9. &lt;summary&gt;
  10. Available to question?: Yes
  11. Relationship: Self
  12. &lt;/summary&gt;
  13. &lt;/client&gt;
  14. &lt;/report&gt;
  15. &lt;report&gt;
  16. &lt;reportversion&gt;1&lt;/reportversion&gt;
  17. &lt;reportid&gt;5003L005UCcfVVS&lt;/reportid&gt;
  18. &lt;policynum&gt;87768978&lt;/policynum&gt;
  19. &lt;primarysourcecountry&gt;IN&lt;/primarysourcecountry&gt;
  20. &lt;client&gt;
  21. &lt;summary&gt;
  22. Available to question?: No
  23. Relationship: Father
  24. &lt;/summary&gt;
  25. &lt;/client&gt;
  26. &lt;/report&gt;
  27. &lt;/lic&gt;

答案1

得分: 0

尝试这样写:

  1. <xsl:stylesheet version="2.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  4. xmlns:sf="urn:sobject.enter.soap.force.com"
  5. xpath-default-namespace="urn:enter.soap.force.com"
  6. exclude-result-prefixes="#all">
  7. <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
  8. <xsl:template match="/soapenv:Envelope">
  9. <lic lang="en">
  10. <xsl:for-each select="soapenv:Body/queryMoreResponse/result/records">
  11. <report>
  12. <reportversion>1</reportversion>
  13. <reportid>
  14. <xsl:value-of select="sf:Id"/>
  15. </reportid>
  16. <policynum>
  17. <xsl:value-of select="sf:PolicyNumber"/>
  18. </policynum>
  19. <primarysourcecountry>IN</primarysourcecountry>
  20. <client>
  21. <summary>
  22. <xsl:for-each select="sf:Case_Responses_GCC__r/records">
  23. <xsl:value-of select="sf:Question_GCC__c"/>
  24. <xsl:text>: </xsl:text>
  25. <xsl:value-of select="sf:Response_GCC__c"/>
  26. <xsl:text>&#10;</xsl:text>
  27. </xsl:for-each>
  28. </summary>
  29. </client>
  30. </report>
  31. </xsl:for-each>
  32. </lic>
  33. </xsl:template>
  34. </xsl:stylesheet>
英文:

Try something like:

  1. &lt;xsl:stylesheet version=&quot;2.0&quot;
  2. xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  3. xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
  4. xmlns:sf=&quot;urn:sobject.enter.soap.force.com&quot;
  5. xpath-default-namespace=&quot;urn:enter.soap.force.com&quot;
  6. exclude-result-prefixes=&quot;#all&quot;&gt;
  7. &lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;utf-8&quot; indent=&quot;yes&quot;/&gt;
  8. &lt;xsl:template match=&quot;/soapenv:Envelope&quot;&gt;
  9. &lt;lic lang=&quot;en&quot;&gt;
  10. &lt;xsl:for-each select=&quot;soapenv:Body/queryMoreResponse/result/records&quot;&gt;
  11. &lt;report&gt;
  12. &lt;reportversion&gt;1&lt;/reportversion&gt;
  13. &lt;reportid&gt;
  14. &lt;xsl:value-of select=&quot;sf:Id&quot;/&gt;
  15. &lt;/reportid&gt;
  16. &lt;policynum&gt;
  17. &lt;xsl:value-of select=&quot;sf:PolicyNumber&quot;/&gt;
  18. &lt;/policynum&gt;
  19. &lt;primarysourcecountry&gt;IN&lt;/primarysourcecountry&gt;
  20. &lt;client&gt;
  21. &lt;summary&gt;
  22. &lt;xsl:for-each select=&quot;sf:Case_Responses_GCC__r/records&quot;&gt;
  23. &lt;xsl:value-of select=&quot;sf:Question_GCC__c&quot;/&gt;
  24. &lt;xsl:text&gt;: &lt;/xsl:text&gt;
  25. &lt;xsl:value-of select=&quot;sf:Response_GCC__c&quot;/&gt;
  26. &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
  27. &lt;/xsl:for-each&gt;
  28. &lt;/summary&gt;
  29. &lt;/client&gt;
  30. &lt;/report&gt;
  31. &lt;/xsl:for-each&gt;
  32. &lt;/lic&gt;
  33. &lt;/xsl:template&gt;
  34. &lt;/xsl:stylesheet&gt;

答案2

得分: 0

尝试一个 PowerShell 脚本:

  1. using assembly System.Xml.Linq
  2. $inputFilename = "c:\temp\test.xml"
  3. $outputFilename = "c:\temp\test1.xml"
  4. $doc = [System.Xml.Linq.XDocument]::Load($inputFilename)
  5. $ns = $doc.Root.GetDefaultNamespace()
  6. $nsSf = $doc.Root.GetNamespaceOfPrefix('sf')
  7. $xmlText = '<?xml version="1.0" encoding="UTF-8"?><lic lang="en"></lic>'
  8. $doc1 = [System.Xml.Linq.XDocument]::Parse($xmlText)
  9. $lic = $doc1.Root
  10. $result = $doc.Descendants($ns + "result")[0]
  11. $records = $result.Elements($ns + "records")
  12. foreach($record in $records)
  13. {
  14. $report = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('report'))
  15. $lic.Add($report)
  16. $reportVersion = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('reportversion'),1)
  17. $report.Add($reportVersion)
  18. $id = $record.Element($nsSf + 'Id').Value
  19. $reportId = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('reportid'),$id)
  20. $report.Add($xId)
  21. $policyNumber = $record.Element($nsSf + 'PolicyNumber').Value
  22. $xPolicynum = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('policynum'),$policyNumber)
  23. $report.Add($xPolicynum)
  24. $xPrimarysourcecountry = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('primarysourcecountry'),'IN')
  25. $report.Add($xprimarysourcecountry)
  26. $client = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('client'))
  27. $report.Add($client)
  28. $summary = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('summary'))
  29. $client.Add($summary)
  30. $writer = [System.IO.StringWriter]::new()
  31. $writer.WriteLine()
  32. $responses = $record.Descendants($ns + 'records')
  33. foreach($response in $responses)
  34. {
  35. $question = $response.Element($nsSf + 'Question_GCC__c').Value
  36. $answer = $response.Element($nsSf + 'Response_GCC__c').Value
  37. $writer.WriteLine($question + ': ' + $answer)
  38. }
  39. $innertext = $writer.ToString()
  40. $summary.AddFirst([string]$innertext)
  41. }
  42. $doc1.Save($outputFilename)

希望对你有帮助!

英文:

Try a powershell script

<!-- begin snippet: js hide: false console: true babel: false -->

  1. using assembly System.Xml.Linq
  2. $inputFilename = &quot;c:\temp\test.xml&quot;
  3. $outputFilename = &quot;c:\temp\test1.xml&quot;
  4. $doc = [System.Xml.Linq.XDocument]::Load($inputFilename)
  5. $ns = $doc.Root.GetDefaultNamespace()
  6. $nsSf = $doc.Root.GetNamespaceOfPrefix(&#39;sf&#39;)
  7. $xmlText = &#39;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;lic lang=&quot;en&quot;&gt;&lt;/lic&gt;&#39;
  8. $doc1 = [System.Xml.Linq.XDocument]::Parse($xmlText)
  9. $lic = $doc1.Root
  10. $result = $doc.Descendants($ns + &quot;result&quot;)[0]
  11. $records = $result.Elements($ns + &quot;records&quot;)
  12. foreach($record in $records)
  13. {
  14. $report = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;report&#39;))
  15. $lic.Add($report)
  16. $reportVersion = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;reportversion&#39;),1)
  17. $report.Add($reportVersion)
  18. $id = $record.Element($nsSf + &#39;Id&#39;).Value
  19. $reportId = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;reportid&#39;),$id)
  20. $report.Add($xId)
  21. $policyNumber = $record.Element($nsSf + &#39;PolicyNumber&#39;).Value
  22. $xPolicynum = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;policynum&#39;),$policyNumber)
  23. $report.Add($xPolicynum)
  24. $xPrimarysourcecountry = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;primarysourcecountry&#39;),&#39;IN&#39;)
  25. $report.Add($xprimarysourcecountry)
  26. $client = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;client&#39;))
  27. $report.Add($client)
  28. $summary = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;summary&#39;))
  29. $client.Add($summary)
  30. $writer = [System.IO.StringWriter]::new()
  31. $writer.WriteLine()
  32. $responses = $record.Descendants($ns + &#39;records&#39;)
  33. foreach($response in $responses)
  34. {
  35. $question = $response.Element($nsSf + &#39;Question_GCC__c&#39;).Value
  36. $answer = $response.Element($nsSf + &#39;Response_GCC__c&#39;).Value
  37. $writer.WriteLine($question + &#39;: &#39; + $answer)
  38. }
  39. $innertext = $writer.ToString()
  40. $summary.AddFirst([string]$innertext)
  41. }
  42. $doc1.Save($outputFilename)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月8日 21:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定