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

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

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的示例:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns="urn:enter.soap.force.com"
  xmlns:sf="urn:sobject.enter.soap.force.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <queryMoreResponse>
      <result>
        <done>false</done>
        <records xsi:type="sf:Case">
          <sf:Id>6896SDRGrt868</sf:Id>
          <sf:PolicyNumber>445353566</sf:PolicyNumber>
           <sf:Case_Responses_GCC__r>
             <records xsi:type="sf:Response_GCC__c">
               <sf:Id xsi:nil="true"/>
               <sf:Question_GCC__c>Available to question?</sf:Question_GCC__c>
               <sf:Response_GCC__c>Yes</sf:Response_GCC__c>
             </records>
             <records xsi:type="sf:Response_GCC__c">
              <sf:Id xsi:nil="true"/>
              <sf:Question_GCC__c>Relationship</sf:Question_GCC__c>
              <sf:Response_GCC__c>Self</sf:Response_GCC__c>
             </records>
           </sf:Case_Responses_GCC__r>
        </records>
        <records xsi:type="sf:Case">
          <sf:Id>5003L005UCcfVVS</sf:Id>
          <sf:PolicyNumber>87768978</sf:PolicyNumber>
           <sf:Case_Responses_GCC__r>
             <records xsi:type="sf:Response_GCC__c">
               <sf:Id xsi:nil="true"/>
               <sf:Question_GCC__c>Available to question?</sf:Question_GCC__c>
               <sf:Response_GCC__c>No</sf:Response_GCC__c>
             </records>
             <records xsi:type="sf:Response_GCC__c">
              <sf:Id xsi:nil="true"/>
              <sf:Question_GCC__c>Relationship</sf:Question_GCC__c>
              <sf:Response_GCC__c>Father</sf:Response_GCC__c>
             </records>
           </sf:Case_Responses_GCC__r>
        </records>
      </result>
    </queryMoreResponse>
  </soapenv:Body>
</soapenv:Envelope>

以下是我的XSLT示例:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xpath-default-namespace="urn:enter.soap.force.com"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:urn="urn:enter.soap.force.com"
  xmlns:sf="urn:sobject.enter.soap.force.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsi sf urn">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="/">
        <report>
           <reportid>
             <xsl:value-of select="//records/sf:Id"/>
           </reportid>
           <policynumber>
             <xsl:value-of select="//records/sf:PolicyNumber"/>
           </policynumber>
        </report>
    </xsl:template>
</xsl:stylesheet>

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

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

<?xml version="1.0" encoding="UTF-8"?>
<lic lang="en">
   <report>
      <reportversion>1</reportversion>
      <reportid>6896SDRGrt868</reportid>
      <policynum>445353566</policynum>
      <primarysourcecountry>IN</primarysourcecountry>
      <client>
         <summary>
				Available to question?: Yes
				Relationship: Self
         </summary>
      </client>
   </report>
   <report>
      <reportversion>1</reportversion>
      <reportid>5003L005UCcfVVS</reportid>
      <policynum>87768978</policynum>
      <primarysourcecountry>IN</primarysourcecountry>
      <client>
         <summary>
	       Available to question?: No
	       Relationship: Father	    
         </summary>
      </client>
   </report>
</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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
  xmlns=&quot;urn:enter.soap.force.com&quot;
  xmlns:sf=&quot;urn:sobject.enter.soap.force.com&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  &lt;soapenv:Body&gt;
    &lt;queryMoreResponse&gt;
      &lt;result&gt;
        &lt;done&gt;false&lt;/done&gt;
        &lt;records xsi:type=&quot;sf:Case&quot;&gt;
          &lt;sf:Id&gt;6896SDRGrt868&lt;/sf:Id&gt;
          &lt;sf:PolicyNumber&gt;445353566&lt;/sf:PolicyNumber&gt;
           &lt;sf:Case_Responses_GCC__r&gt;
             &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
               &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
               &lt;sf:Question_GCC__c&gt;Available to question?&lt;/sf:Question_GCC__c&gt;
               &lt;sf:Response_GCC__c&gt;Yes&lt;/sf:Response_GCC__c&gt;
             &lt;/records&gt;
             &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
              &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
              &lt;sf:Question_GCC__c&gt;Relationship&lt;/sf:Question_GCC__c&gt;
              &lt;sf:Response_GCC__c&gt;Self&lt;/sf:Response_GCC__c&gt;
             &lt;/records&gt;
           &lt;/sf:Case_Responses_GCC__r&gt;
        &lt;/records&gt;
        &lt;records xsi:type=&quot;sf:Case&quot;&gt;
          &lt;sf:Id&gt;5003L005UCcfVVS&lt;/sf:Id&gt;
          &lt;sf:PolicyNumber&gt;87768978&lt;/sf:PolicyNumber&gt;
           &lt;sf:Case_Responses_GCC__r&gt;
             &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
               &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
               &lt;sf:Question_GCC__c&gt;Available to question?&lt;/sf:Question_GCC__c&gt;
               &lt;sf:Response_GCC__c&gt;No&lt;/sf:Response_GCC__c&gt;
             &lt;/records&gt;
             &lt;records xsi:type=&quot;sf:Response_GCC__c&quot;&gt;
              &lt;sf:Id xsi:nil=&quot;true&quot;/&gt;
              &lt;sf:Question_GCC__c&gt;Relationship&lt;/sf:Question_GCC__c&gt;
              &lt;sf:Response_GCC__c&gt;Father&lt;/sf:Response_GCC__c&gt;
             &lt;/records&gt;
           &lt;/sf:Case_Responses_GCC__r&gt;
        &lt;/records&gt;
      &lt;/result&gt;
    &lt;/queryMoreResponse&gt;
  &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;

My XSLT

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;xsl:stylesheet version=&quot;2.0&quot; xpath-default-namespace=&quot;urn:enter.soap.force.com&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
  xmlns:urn=&quot;urn:enter.soap.force.com&quot;
  xmlns:sf=&quot;urn:sobject.enter.soap.force.com&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; exclude-result-prefixes=&quot;xsi sf urn&quot;&gt;
    &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;
    &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
    &lt;xsl:template match=&quot;/&quot;&gt;
        &lt;report&gt;
           &lt;reportid&gt;
             &lt;xsl:value-of select=&quot;//records/sf:Id&quot;/&gt;
           &lt;/reportid&gt;
           &lt;policynumber&gt;
             &lt;xsl:value-of select=&quot;//records/sf:PolicyNumber&quot;/&gt;
           &lt;/policynumber&gt;
        &lt;/report&gt;
    &lt;/xsl:template&gt;
&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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;lic lang=&quot;en&quot;&gt;
   &lt;report&gt;
      &lt;reportversion&gt;1&lt;/reportversion&gt;
      &lt;reportid&gt;6896SDRGrt868&lt;/reportid&gt;
      &lt;policynum&gt;445353566&lt;/policynum&gt;
      &lt;primarysourcecountry&gt;IN&lt;/primarysourcecountry&gt;
      &lt;client&gt;
         &lt;summary&gt;
				Available to question?: Yes
				Relationship: Self
         &lt;/summary&gt;
      &lt;/client&gt;
   &lt;/report&gt;
   &lt;report&gt;
      &lt;reportversion&gt;1&lt;/reportversion&gt;
      &lt;reportid&gt;5003L005UCcfVVS&lt;/reportid&gt;
      &lt;policynum&gt;87768978&lt;/policynum&gt;
      &lt;primarysourcecountry&gt;IN&lt;/primarysourcecountry&gt;
      &lt;client&gt;
         &lt;summary&gt;
	       Available to question?: No
	       Relationship: Father	    
         &lt;/summary&gt;
      &lt;/client&gt;
   &lt;/report&gt;
&lt;/lic&gt;

答案1

得分: 0

尝试这样写:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sf="urn:sobject.enter.soap.force.com"
xpath-default-namespace="urn:enter.soap.force.com"
exclude-result-prefixes="#all">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/soapenv:Envelope">
    <lic lang="en">
        <xsl:for-each select="soapenv:Body/queryMoreResponse/result/records">
            <report>
                <reportversion>1</reportversion>
                <reportid>
                    <xsl:value-of select="sf:Id"/>
                </reportid>
                <policynum>
                    <xsl:value-of select="sf:PolicyNumber"/>
                </policynum>
                <primarysourcecountry>IN</primarysourcecountry>
                <client>
                    <summary>
                        <xsl:for-each select="sf:Case_Responses_GCC__r/records">
                            <xsl:value-of select="sf:Question_GCC__c"/>
                            <xsl:text>: </xsl:text>
                            <xsl:value-of select="sf:Response_GCC__c"/>
                            <xsl:text>&#10;</xsl:text>
                        </xsl:for-each>
                    </summary>
                </client>
            </report>
        </xsl:for-each>
    </lic>
</xsl:template>

</xsl:stylesheet>
英文:

Try something like:

&lt;xsl:stylesheet version=&quot;2.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;
xmlns:sf=&quot;urn:sobject.enter.soap.force.com&quot;
xpath-default-namespace=&quot;urn:enter.soap.force.com&quot;
exclude-result-prefixes=&quot;#all&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;utf-8&quot; indent=&quot;yes&quot;/&gt;

&lt;xsl:template match=&quot;/soapenv:Envelope&quot;&gt;
	&lt;lic lang=&quot;en&quot;&gt;
		&lt;xsl:for-each select=&quot;soapenv:Body/queryMoreResponse/result/records&quot;&gt;
			&lt;report&gt;
				&lt;reportversion&gt;1&lt;/reportversion&gt;
				&lt;reportid&gt;
					&lt;xsl:value-of select=&quot;sf:Id&quot;/&gt;
				&lt;/reportid&gt;
				&lt;policynum&gt;
					&lt;xsl:value-of select=&quot;sf:PolicyNumber&quot;/&gt;
				&lt;/policynum&gt;
				&lt;primarysourcecountry&gt;IN&lt;/primarysourcecountry&gt;
				&lt;client&gt;
					&lt;summary&gt;
						&lt;xsl:for-each select=&quot;sf:Case_Responses_GCC__r/records&quot;&gt;
							&lt;xsl:value-of select=&quot;sf:Question_GCC__c&quot;/&gt;
							&lt;xsl:text&gt;: &lt;/xsl:text&gt;
							&lt;xsl:value-of select=&quot;sf:Response_GCC__c&quot;/&gt;
							&lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
						&lt;/xsl:for-each&gt;
					&lt;/summary&gt;
				&lt;/client&gt;
			&lt;/report&gt;
		&lt;/xsl:for-each&gt;
	&lt;/lic&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

答案2

得分: 0

尝试一个 PowerShell 脚本:

using assembly System.Xml.Linq

$inputFilename = "c:\temp\test.xml"
$outputFilename = "c:\temp\test1.xml"

$doc = [System.Xml.Linq.XDocument]::Load($inputFilename)
$ns = $doc.Root.GetDefaultNamespace()
$nsSf = $doc.Root.GetNamespaceOfPrefix('sf')

$xmlText = '<?xml version="1.0" encoding="UTF-8"?><lic lang="en"></lic>'
$doc1 = [System.Xml.Linq.XDocument]::Parse($xmlText)
$lic = $doc1.Root

$result = $doc.Descendants($ns + "result")[0]
$records = $result.Elements($ns + "records")

foreach($record in $records)
{
   $report = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('report'))
   $lic.Add($report)

   $reportVersion = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('reportversion'),1)
   $report.Add($reportVersion)

   $id = $record.Element($nsSf + 'Id').Value
   $reportId = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('reportid'),$id)
   $report.Add($xId)

   $policyNumber = $record.Element($nsSf + 'PolicyNumber').Value
   $xPolicynum = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('policynum'),$policyNumber)
   $report.Add($xPolicynum)

   $xPrimarysourcecountry = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('primarysourcecountry'),'IN')
   $report.Add($xprimarysourcecountry)

   $client = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('client'))
   $report.Add($client)

   $summary = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get('summary'))
   $client.Add($summary)

   $writer = [System.IO.StringWriter]::new()
   $writer.WriteLine()
   $responses = $record.Descendants($ns + 'records')
   foreach($response in $responses)
   {
      $question = $response.Element($nsSf + 'Question_GCC__c').Value
      $answer = $response.Element($nsSf + 'Response_GCC__c').Value
      $writer.WriteLine($question + ': ' + $answer)
   }
   $innertext = $writer.ToString()
   $summary.AddFirst([string]$innertext)
}

$doc1.Save($outputFilename)

希望对你有帮助!

英文:

Try a powershell script

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

using assembly System.Xml.Linq

$inputFilename = &quot;c:\temp\test.xml&quot;
$outputFilename = &quot;c:\temp\test1.xml&quot;

$doc = [System.Xml.Linq.XDocument]::Load($inputFilename)
$ns = $doc.Root.GetDefaultNamespace()
$nsSf = $doc.Root.GetNamespaceOfPrefix(&#39;sf&#39;)

$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;
$doc1 = [System.Xml.Linq.XDocument]::Parse($xmlText)
$lic = $doc1.Root

$result = $doc.Descendants($ns + &quot;result&quot;)[0]
$records = $result.Elements($ns + &quot;records&quot;)

foreach($record in $records)
{
   $report = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;report&#39;))
   $lic.Add($report)

   $reportVersion = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;reportversion&#39;),1)
   $report.Add($reportVersion)

   $id = $record.Element($nsSf + &#39;Id&#39;).Value
   $reportId = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;reportid&#39;),$id)
   $report.Add($xId)

   $policyNumber = $record.Element($nsSf + &#39;PolicyNumber&#39;).Value
   $xPolicynum = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;policynum&#39;),$policyNumber)
   $report.Add($xPolicynum)

   $xPrimarysourcecountry = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;primarysourcecountry&#39;),&#39;IN&#39;)
   $report.Add($xprimarysourcecountry)

   $client = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;client&#39;))
   $report.Add($client)

   $summary = [System.Xml.Linq.XElement]::new([System.Xml.Linq.Xname]::get(&#39;summary&#39;))
   $client.Add($summary)

   $writer = [System.IO.StringWriter]::new()
   $writer.WriteLine()
   $responses = $record.Descendants($ns + &#39;records&#39;)
   foreach($response in $responses)
   {
      $question = $response.Element($nsSf + &#39;Question_GCC__c&#39;).Value
      $answer = $response.Element($nsSf + &#39;Response_GCC__c&#39;).Value
      $writer.WriteLine($question + &#39;: &#39; + $answer)
   }
   $innertext = $writer.ToString()
   $summary.AddFirst([string]$innertext)
}

$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:

确定