简体   繁体   English

使用XSLT转换为XML

[英]Using XSLT to Transform into XML

Trying to figure out why this is not working. 试图弄清楚为什么它不起作用。 I have followed the instructions on W3Schools XSLT Docs and as well W3Schools XPath Docs and I keep on getting null for the "MeterNo" value of the "MeterInfo" tag. 我已经按照W3Schools XSLT文档以及W3Schools XPath文档中的说明进行操作,并且对于“ MeterInfo”标记的“ MeterNo”值,我一直保持为空。

XSLT XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
                exclude-result-prefixes="msxsl">

<xsl:template match="/">
  <MeterInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <MeterNo>
      <xsl:value-of select="Template[@name='MyTemplateName']/Field[@name='MyFieldName']"/>
    </MeterNo>
  </MeterInfo>
</xsl:template>

XML XML格式

<?xml version="1.0" encoding="utf-8"?>
<ProcessHostRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                    xmlns="http://www.utilitysolutions.cgi.com/UHIB-1_0">
   <DataArea>
      <Process xmlns="http://www.openapplications.org/oagis" />
      <HostRequest>
         <Template name="MyTemplateName">
            <Field name="MyFieldName">
               8768565
            </Field>
         </Template>
      </HostRequest>
   </DataArea>
</ProcessHostRequest>

My Transformation Response 我的转变回应

<?xml version="1.0" encoding="UTF-8"?>
<MeterInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <MeterNo/>
</MeterInfo>

Two errors: 两个错误:

First in your template with match pattern / the context node is the root node. 具有匹配模式的模板中的第一个/上下文节点是根节点。 The expression select="Template[..." would only return a non-empty result if the document node would have name Template but it is ProcessHostRequest . 如果文档节点的名称为Template但其名称为ProcessHostRequest ,则表达式select="Template[..."只会返回非空结果。

Therefore match for the descendant: select="//Template... . 因此,匹配后代: select="//Template...

Second the Template and Field elements are in namespace http://www.utilitysolutions.cgi.com/UHIB-1_0 . 其次, TemplateField元素位于名称空间http://www.utilitysolutions.cgi.com/UHIB-1_0 To select them you need to declare the same namespace in your XSLT: 要选择它们,您需要在XSLT中声明相同的名称空间:

<xsl:stylesheet version="1.0" 
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
            xmlns:c="http://www.utilitysolutions.cgi.com/UHIB-1_0"
            exclude-result-prefixes="msxsl c">

and use it accordingly 并据此使用

<xsl:value-of select="//c:Template[@name='MyTemplateName']/c:Field[@name='MyFieldName']"/>

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

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