简体   繁体   English

使用XSLT 2.0解析xml字符串

[英]parse xml string with XSLT 2.0

I have a XML with an XML string inside 我有一个带有XML字符串的XML

 <?xml version="1.0" encoding="UTF-8"?><body xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway" xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
  <ns0:JMSTextBody_element>
    <ns1:value>&lt;delivery gduid="6395" sduid="2392"&gt;&#13;
    &lt;object type="issue" id="C0.ISS.1451" alt_id="BBG000BH7B07"&gt;&#13;
        &lt;attributes&gt;&#13;
            &lt;simple id="bb_global_id" changed="true"&gt;BBG000BH7B07&lt;/simple&gt;&#13;
            &lt;simple id="dv_source1" changed="true"&gt;BB&lt;/simple&gt;&#13;
            &lt;simple id="nominal_value" changed="true"&gt;0.1&lt;/simple&gt;&#13;
        &lt;/attributes&gt;&#13;
    &lt;/object&gt;&#13;
&lt;/delivery&gt;</ns1:value>
  </ns0:JMSTextBody_element>
</body>

I need to fetch the values from the inline XML reformat and create a new XML string 我需要从内联XML重新格式化中获取值并创建一个新的XML字符串

    <?xml version="1.0" encoding="UTF-8"?><body xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway" xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
  <ns0:JMSTextBody_element>
    <ns1:value>
 <ac_connect_type>issue</ac_connect_type><ac_connect_id>C0.ISS.1451</ac_connect_id><ac_connect_alt_id>BBG000BH7B07</ac_connect_alt_id><bb_global_id>BBG000BH7B07</bb_global_id>
 <dv_source1>BB</dv_source1><nominal_value>0.1</nominal_value></ns1:value>
  </ns0:JMSTextBody_element>
</body>

I'm having trouble to get to the elements in the inline XML 我在获取内联XML中的元素时遇到了麻烦

<xsl:apply-templates select="/delivery/object/attributes/bb_global_id" />

doesn't seem to work, obviously I'm missing something here Thanks 似乎不起作用,显然我在这里缺少了一些东西,谢谢

Assuming Saxon 9.6 and XSLT 3.0 you can use 假设您可以使用Saxon 9.6和XSLT 3.0

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema"
  exclude-result-prefixes="xs">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

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

<xsl:template match="ns1:value">
  <xsl:copy>
    <xsl:apply-templates select="parse-xml(.)//object"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="object">
  <xsl:apply-templates select="@* , attributes/simple"/>
</xsl:template>

<xsl:template match="object/@*">
  <xsl:element name="ac_connect_{local-name()}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

<xsl:template match="simple">
  <xsl:element name="{@id}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

which creates the output 创建输出

<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway"
      xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
   <ns0:JMSTextBody_element>
      <ns1:value>
         <ac_connect_type>issue</ac_connect_type>
         <ac_connect_id>C0.ISS.1451</ac_connect_id>
         <ac_connect_alt_id>BBG000BH7B07</ac_connect_alt_id>
         <bb_global_id>BBG000BH7B07</bb_global_id>
         <dv_source1>BB</dv_source1>
         <nominal_value>0.1</nominal_value>
      </ns1:value>
   </ns0:JMSTextBody_element>
</body>

obviously I'm missing something here 显然我在这里想念什么

It seems you're missing the fact that the escaped XML inside ns1:value is just a meaningless string, and cannot be addressed using XPath. 似乎您错过了以下事实: ns1:value中的转义XML只是一个无意义的字符串,无法使用XPath进行寻址。

The "correct" solution is to make the transformation in two passes. “正确”的解决方案是两次转换。 In the first pass, use: 在第一遍中,使用:

XSLT XSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.ibm.com/websphere/sibx/ServiceGateway" xmlns:ns1="http://com.ibm.websphere.jms.data.bindings/schema">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/body">
    <xsl:value-of select="ns0:JMSTextBody_element/ns1:value" disable-output-escaping="yes"/>
</xsl:template>

</xsl:stylesheet> 

Save the result of this transformation to a file, then apply another transformation, using another stylesheet, to that file. 将转换结果保存到文件中,然后使用另一个样式表将另一个转换应用于该文件。

Alternatively, you could try and extract the required information from the escaped string using string functions - which is awkward and error-prone. 或者,您可以尝试使用字符串函数从转义的字符串中提取所需的信息-这很麻烦且容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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