简体   繁体   English

为什么此XSLT无法生成预期的输出?

[英]Why does this XSLT not generate my expected output?

I'm trying to transform an XML to another XML, using XSLT. 我正在尝试使用XSLT将XML转换为另一个XML。 Here's my XML 这是我的XML

<?xml version="1.0" encoding = "iso-8859-1"?>
<Förskoleansökan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>

  <Förskolenamn>Dalabergs förskola</Förskolenamn>

  <Barn>

    <Förnamn>Nils</Förnamn>
    <Efternamn>Petersson</Efternamn>
    <Personnummer>1008021245</Personnummer>


    <Syskon>
      <Personnummer>0005052464</Personnummer>
      <Förnamn>Johan</Förnamn>
      <Efternamn>Petersson</Efternamn>

    </Syskon>

    <Övrigt>

      <Allergier>Inga allergier</Allergier>
      <AnnatÖmmandeSkäl></AnnatÖmmandeSkäl>


    </Övrigt>

  </Barn>

  <Vårdnadshavare>

     <Vårdnadshavare1 personnummer="7806032356">
            <Förnamn>Mia</Förnamn>
            <Efternamn>Petersson</Efternamn>
    </Vårdnadshavare1>

    <Vårdnadshavare2 personnummer="7806089645">
            <Förnamn>Jörgen</Förnamn>
            <Efternamn>Petersson</Efternamn>
       
    </Vårdnadshavare2>

  </Vårdnadshavare>


</Förskoleansökan>

I'm trying to transform that XML to this one 我正在尝试将该XML转换为此

<begaranominkomstuppgift>
    <person personnummer="7806032356">
      <fornamn>Mia</fornamn>
      <efternamn>Petersson</efternamn>
    </person>
    <person personnummer="7806089645">
      <fornamn>Jörgen</fornamn>
      <efternamn>Petersson</efternamn>
    </person>
</begaranominkomstuppgift>

Here's my XSL 这是我的XSL

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <begaraninkomstuppgift>
      <xsl:for-each select="Förskoleansökan/Vårdnadshavare">
        <person>
          <xsl:attribute name="personummer">
            <xsl:value-of select="personnummer"/>
          </xsl:attribute>
          <fornamn>
            <xsl:value-of select="Vårdnadshavare/Förnamn" />
          </fornamn>
          <efternamn>
            <xsl:value-of select="Vårdnadshavare/Efternamn" />
          </efternamn>
        </person>
      </xsl:for-each>
    </begaraninkomstuppgift>
  </xsl:template>
</xsl:stylesheet>

The transformation doesn't occur and my output is still the old XML. 转换不会发生,我的输出仍然是旧的XML。 What seems to be the issue here? 这里似乎是什么问题?

The XML is most certainly my work and basically i'm happy about the structure. XML当然是我的工作,并且基本上我对结构感到满意。 I structured the way it's easiest for me. 我设计了对我来说最简单的方法。 I would greatly appreciate it, if you could help me out here. 如果您能在这里帮助我,我将不胜感激。 I'm not expecting you to create a completely new stylesheet, but just point out the flaws in my current one. 我不希望您创建一个全新的样式表,只是指出当前样式表中的缺陷。

The primary goal of Stackoverflow is to teach people what they need to know, not to give them what they want. Stackoverflow的主要目标是教人们他们需要知道什么,而不是给他们他们想要的东西。

An XSLT transformation never exists on its own (exception: generic transformations like the identity transform), it only makes sense if it is related to a specific input XML document. XSLT转换永远不会独立存在(例外:通用转换,例如身份转换),只有与特定的输入XML文档相关时,它才有意义。 The input is what you start with. 输入就是您开始的内容。

Sometimes, you will find yourself in a situation where the input is defined by someone else (your customer, an external provider, your boss wants it this way etc.) and you have to cope with it, no matter if the XML format is sensible or not. 有时,您会发现自己处在其他人(您的客户,外部提供商,老板希望通过这种方式等)定义输入的情况下,无论XML格式是否合理,您都必须应对。或不。

But that's not the case for you, you are in charge of designing your own input document and choosing a good design will greatly simplify the task of creating an XSLT stylesheet to process the input (your ultimate goal). 但这不是您的情况,您负责设计自己的输入文档,选择一个好的设计将大大简化创建XSLT样式表以处理输入的任务(您的最终目标)。

Well i'm a beginner so that's why it looks really bad to you guys. 好吧,我是一个初学者,所以这对你们来说看起来真的很糟糕。 The important thing is that the XML is well-formed and does not contain any errors. 重要的是XML格式正确,并且不包含任何错误。

No, not really. 不,不是。 Well-formedness is a precondition for anything to be called XML in the first place, but it does not say anything about the usefulness of the design. 良好的格式首先是将任何东西称为XML的先决条件 ,但它并没有说明设计的有用性。


Now let's have a look at your design. 现在让我们看一下您的设计。 First of all, the <?xml-stylesheet processing instruction should be at the beginning of the file, right after the XML declaration: 首先, <?xml-stylesheet处理指令应位于文件的开头,紧随XML声明之后:

Next, looking at the contents of Barn : 接下来,查看Barn的内容:

<Barn>
    <Förnamn>Nils</Förnamn>
    <Efternamn>Petersson</Efternamn>
    <Personnummer>1008021245</Personnummer>
    <Syskon>
        <Personnummer>0005052464</Personnummer>
        <Förnamn>Johan</Förnamn>
        <Efternamn>Petersson</Efternamn>
    </Syskon>
    <Övrigt>
        <Allergier>Inga allergier</Allergier>
        <AnnatÖmmandeSkäl/>
    </Övrigt>
</Barn>

The three same elements occur as direct children of Barn and also inside Syskon , but the order is different, which does not make sense. 这三个相同的元素Syskon作为Barn直接子代出现,也出现在Syskon ,但是顺序不同,这没有意义。 The order of elements is significant in XML. 元素的顺序在XML中很重要。

Above, the person number is represented as an element, but later in the document, the person number is stored as an attribute, which will certainly cause confusion. 上面,人员编号表示为元素,但是在文档的后面,人员编号存储为属性,这肯定会引起混乱。

Inside Vårdnadshavare , there are the child elements Vårdnadshavare1 and Vårdnadshavare2 . Vårdnadshavare内部,有子元素Vårdnadshavare1Vårdnadshavare2 Element names should never be designed in this way because it complicates automatic processing (because each element name is unique) and because the number is actually the position of the element, which is obvious. 绝对不能以这种方式设计元素名称,因为它会使自动处理变得复杂(因为每个元素名称都是唯一的),并且因为数字实际上是元素的位置 ,这是显而易见的。

Things that are the same should have the same name, without a number. 相同的事物应具有相同的名称,没有数字。 If you need the number in it, you could have an attribute like 如果您需要其中的数字,则可以使用如下属性

<Vårdnadshavare n="1">

but, as I said, it is almost certainly superfluous. 但是,正如我所说,这几乎是多余的。

Please note : On many aspects of the design I cannot comment because the element names are in a language I do not understand. 请注意 :在设计的许多方面,我无法评论,因为元素名称使用的语言我不懂。 And it is hard to propose an improved XML design because of this. 因此,很难提出一种改进的XML设计。 If you plan to ask further XSLT questions, you might get better help if you translate the content. 如果您打算提出其他XSLT问题,则翻译内容可能会获得更好的帮助。

why is it not working? 为什么它不起作用?

It is working - it's just not working the way you expect. 正在工作-只是没有按您期望的方式工作。

The main reason for this is that you create a person for each Förskoleansökan/Vårdnadshavare . 这样做的主要原因是您为每个Förskoleansökan/Vårdnadshavare创建一个人。 There is only one Vårdnadshavare element in your XML, so only one person is created. XML中只有一个Vårdnadshavare元素,因此只能创建一个人。

To get the two persons you want, you need to create a person for each child node of Vårdnadshavare . 要获得所需的两个人,您需要为Vårdnadshavare每个子节点创建一个人。 You have named these Vårdnadshavare1 and Vårdnadshavare2 , which makes it difficult to select them by name. 您已将这些Vårdnadshavare1Vårdnadshavare2命名,这使得很难通过名称选择它们。 If we assume that they are the only children of Vårdnadshavare , we could do: 如果我们假设他们的独生子女 Vårdnadshavare ,我们可以这样做:

<xsl:for-each select="Förskoleansökan/Vårdnadshavare/*">
    <person>
    ...

Your next mistake is that you use: 下一个错误是您使用:

<xsl:value-of select="personnummer"/>

instead of: 代替:

 <xsl:value-of select="@personnummer"/>

Finally, Förnamn and Efternamn are direct children of Vårdnadshavare1 / Vårdnadshavare2 - so an expression like Vårdnadshavare/Förnamn does not select anything. 最后, FörnamnEfternamnVårdnadshavare1 / Vårdnadshavare2直接Vårdnadshavare2 -因此,像Vårdnadshavare/Förnamn这样的表达式不会选择任何内容。

Try this corrected stylesheet: 尝试以下更正的样式表:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <begaraninkomstuppgift>
      <xsl:for-each select="Förskoleansökan/Vårdnadshavare/*">
        <person>
          <xsl:attribute name="personummer">
            <xsl:value-of select="@personnummer"/>
          </xsl:attribute>
          <fornamn>
            <xsl:value-of select="Förnamn" />
          </fornamn>
          <efternamn>
            <xsl:value-of select="Efternamn" />
          </efternamn>
        </person>
      </xsl:for-each>
    </begaraninkomstuppgift>
  </xsl:template>
</xsl:stylesheet>

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

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