2016-12-15 1 views
0

나는 Banxico Web Services를 사용하고 있는데 문자열을 반환합니다. 문자열에는 XML이 있습니다.String XML을 XML 노드로 변환하는 방법 XSLT

정말 필요한 부분은 XSLT 또는 XPath를 사용하여 다음 부분을 추출하는 것입니다.

XSLT 또는 Xpath를 사용하여 어떻게 할 수 있습니까?

페소 por dólar E.U.A. 억센 드 캄 비오 파라 solventar obligaciones denominadas EN moneda extranjera Fecha 드 determinación (FIX) " IDSERIE ="SF43718 "BANXICO_FREQ ="직경 "BANXICO_FIGURE_TYPE ="TipoCambio " BANXICO_UNIT_TYPE ="PesoxDoll ">

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Header> 
     <X-OPNET-Transaction-Trace:X-OPNET-Transaction-Trace soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xsi:type="soapenc:string" xmlns:X-OPNET-Transaction-Trace="http://opnet.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">pid=4468,requestid=4d0ce60a-c92f-441e-87b1-4c6f12b26574</X-OPNET-Transaction-Trace:X-OPNET-Transaction-Trace> 
    </soapenv:Header> 
    <soapenv:Body> 
     <ns1:tiposDeCambioBanxicoResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://ws.dgie.banxico.org.mx"> 
     <result xsi:type="xsd:string"><![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?> 
<CompactData xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message" 
xmlns:bm="http://www.banxico.org.mx/structure/key_families/dgie/sie/series/compact" 
xmlns:compact="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/compact" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message SDMXMessage.xsd 
http://www.banxico.org.mx/structure/key_families/dgie/sie/series/compact BANXICO_DGIE_SIE_Compact.xsd 
http://www.SDMX.org/resources/SDMXML/schemas/v1_0/compact SDMXCompactData.xsd" > 
    <Header> 
     <ID>TIPOSDECAMBIO</ID> 
     <Test>false</Test> 
     <Truncated>false</Truncated> 
     <Name xml:lang="sp">Tipos de Cambio</Name> 
     <Prepared>2016-12-15 12:26:37.766</Prepared> 
     <Sender id="BANXICO"> 
      <Name xml:lang="sp">Banco de México</Name> 
      <Contact> 
      <Name xml:lang="sp">Subgerencia de Desarrollo de Sistemas</Name> 
      <Telephone>(01 55)52372678</Telephone> 
      </Contact> 
     </Sender> 
     <DataSetAction>Update</DataSetAction> 
     <Extracted>2016-12-15 12:26:37.766</Extracted> 
    </Header> 
    <bm:DataSet> 
     <bm:SiblingGroup BANXICO_FREQ="Dia" TIME_FORMAT="P1D"/> 
     <bm:Series TITULO="Tipo de cambio pesos por dólar E.U.A. Tipo de cambio para solventar obligaciones denominadas en moneda extranjera Fecha de liquidación" IDSERIE="SF60653" BANXICO_FREQ="Dia" BANXICO_FIGURE_TYPE="TipoCambio" BANXICO_UNIT_TYPE="PesoxDoll"> 
      <bm:Obs TIME_PERIOD="2016-12-15" OBS_VALUE="20.2567"/> 
     </bm:Series> 
     <bm:Series TITULO="Tipo de cambio           Pesos por dólar E.U.A. Tipo de cambio para solventar obligaciones denominadas en moneda extranjera Fecha de determinación (FIX)" IDSERIE="SF43718" BANXICO_FREQ="Dia" BANXICO_FIGURE_TYPE="TipoCambio" BANXICO_UNIT_TYPE="PesoxDoll"> 
      <bm:Obs TIME_PERIOD="2016-12-15" OBS_VALUE="20.5973"/> 
     </bm:Series> 
     <bm:Series TITULO="Cotización de las divisas que conforman la canasta del DEG Respecto al peso mexicano Euro" IDSERIE="SF46410" BANXICO_FREQ="Dia" BANXICO_FIGURE_TYPE="TipoCambio" BANXICO_UNIT_TYPE="Peso"> 
      <bm:Obs TIME_PERIOD="2016-12-14" OBS_VALUE="21.535"/> 
     </bm:Series> 
     <bm:Series TITULO="Cotización de la divisa Respecto al peso mexicano Dólar Canadiense" IDSERIE="SF60632" BANXICO_FREQ="Dia" BANXICO_FIGURE_TYPE="TipoCambio" BANXICO_UNIT_TYPE="Peso"> 
      <bm:Obs TIME_PERIOD="2016-12-14" OBS_VALUE="15.4178"/> 
     </bm:Series> 
     <bm:Series TITULO="Cotización de las divisas que conforman la canasta del DEG Respecto al peso mexicano Yen japonés" IDSERIE="SF46406" BANXICO_FREQ="Dia" BANXICO_FIGURE_TYPE="TipoCambio" BANXICO_UNIT_TYPE="Peso"> 
      <bm:Obs TIME_PERIOD="2016-12-14" OBS_VALUE="0.176"/> 
     </bm:Series> 
     <bm:Series TITULO="Cotización de las divisas que conforman la canasta del DEG Respecto al peso mexicano Libra esterlina" IDSERIE="SF46407" BANXICO_FREQ="Dia" BANXICO_FIGURE_TYPE="TipoCambio" BANXICO_UNIT_TYPE="Peso"> 
      <bm:Obs TIME_PERIOD="2016-12-14" OBS_VALUE="25.5806"/> 
     </bm:Series> 
    </bm:DataSet> 
</CompactData>]]></result> 
     </ns1:tiposDeCambioBanxicoResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 
그 응답으로

은 내가 XSD 스키마를 구축 할 수 있습니다.

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.banxico.org.mx/structure/key_families/dgie/sie/series/compact" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="DataSet"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="SiblingGroup"> 
      <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:string"> 
       <xs:attribute type="xs:string" name="BANXICO_FREQ"/> 
       <xs:attribute type="xs:duration" name="TIME_FORMAT"/> 
       </xs:extension> 
      </xs:simpleContent> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="Series" maxOccurs="unbounded" minOccurs="0"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="Obs"> 
       <xs:complexType> 
        <xs:simpleContent> 
        <xs:extension base="xs:string"> 
         <xs:attribute type="xs:date" name="TIME_PERIOD" use="optional"/> 
         <xs:attribute type="xs:float" name="OBS_VALUE" use="optional"/> 
        </xs:extension> 
        </xs:simpleContent> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      <xs:attribute type="xs:string" name="TITULO" use="optional"/> 
      <xs:attribute type="xs:string" name="IDSERIE" use="optional"/> 
      <xs:attribute type="xs:string" name="BANXICO_FREQ" use="optional"/> 
      <xs:attribute type="xs:string" name="BANXICO_FIGURE_TYPE" use="optional"/> 
      <xs:attribute type="xs:string" name="BANXICO_UNIT_TYPE" use="optional"/> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
+1

어떤 버전의 XSLT 또는 XPath를 사용할 수 있습니까? 내부 XML을 CDATA 섹션으로 이스케이프 처리하면 두 가지 구문 분석 단계가 필요합니다. 순수 XSLT 또는 XPath에서이를 수행하려면 XSLT/XPath 3.0 또는 XML을 구문 분석하기위한 확장을 지원하는 구현이 필요합니다. –

답변

2

당신이 원하는 정보를 추출하는 XSL 변환을 실행하려면, 당신은 생성 할 필요가 없습니다 스키마.

XPath 3 함수를 사용할 수있는 경우 XPath 3에는 문자열을 구문 분석하여 논리적 XML 트리로 변환하는 기능이 포함되어 있으므로 XPath 표현식을 사용하여 XPath 표현식을 실행할 수 있습니다.

XPath 3 함수를 사용할 수없는 경우 다음 두 단계로 처리해야합니다. <result> 텍스트 콘텐츠 (추가 처리하려는 XML 응답) 만 별도의 파일로 내보내고 그런 다음 해당 파일을로드하고 원하는 파일을 추출하기 위해 다른 변환을 실행하십시오. 여기

은,이 XSL은 다음과 같은 결과를 생성하는 입력으로 위의 XML을 사용하여 3.

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="3.0"> 

    <!-- Start at the document node/and set up the outermost element --> 
    <xsl:template match="/"> 
     <!-- Change this element name to whatever makes sense for you --> 
     <BanxicoReply> 
      <!-- The input file only has one "result", 
       and that's all we want, so grab it --> 
      <xsl:apply-templates select="//result"/> 
     </BanxicoReply> 
    </xsl:template> 

    <xsl:template match="result"> 
     <!-- Parse the CDATA content to turn it into XML --> 
     <xsl:variable name="contained-xml" select="parse-xml(./text())"/> 

     <!-- Do what you need to here. 
      This example just plucks out the one chunk 
      you identified at the top of your post. 

      Note the "*:Series" shorthand: this matches 
      any "Series" element in any namespace. This 
      is an admittedly sloppy shortcut, which could 
      be dangerous in other contexts - but here, in 
      this one file, it's safe, and this avoids the 
      need for us to explicitly declare the "bm:" 
      namespace prefix in this XSL. --> 
     <xsl:copy-of select="$contained-xml//*:Series[@IDSERIE='SF43718']"/> 
    </xsl:template> 

    <!-- Suppress output of all other elements --> 
    <xsl:template match="*"/> 

</xsl:stylesheet> 

XPath는을 사용하여 한 번에이 문제를 처리하는 방법에 대한 빠른 - 및 - 더러운 예입니다 (쉽게 인간에 대한 들여 쓰기 읽기 쉽도록) :

<?xml version="1.0" encoding="UTF-8"?> 
<BanxicoReply> 
    <bm:Series xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/message" 
     xmlns:bm="http://www.banxico.org.mx/structure/key_families/dgie/sie/series/compact" 
     xmlns:compact="http://www.SDMX.org/resources/SDMXML/schemas/v1_0/compact" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     TITULO="Tipo de cambio           Pesos por dólar E.U.A. Tipo de cambio para solventar obligaciones denominadas en moneda extranjera Fecha de determinación (FIX)" 
     IDSERIE="SF43718" 
     BANXICO_FREQ="Dia" 
     BANXICO_FIGURE_TYPE="TipoCambio" 
     BANXICO_UNIT_TYPE="PesoxDoll"> 
     <bm:Obs TIME_PERIOD="2016-12-15" OBS_VALUE="20.5973"/> 
    </bm:Series> 
</BanxicoReply> 
관련 문제