使用元素名称获取子值

时间:2020-08-19 12:54:56

标签: xml xslt

我有这个xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:ExportCompanies xmlns="urn:be:fgov:ehealth:samws:v2:company:submit" xmlns:ns2="urn:be:fgov:ehealth:samws:v2:export">
    <ns2:Company actorNr="01716">
        <ns2:Data from="1960-01-01">
            <AuthorisationNr>1716H</AuthorisationNr>
            <VatNr countryCode="BE">0406316776</VatNr>
            <Denomination>Schenker</Denomination>
            <LegalForm>NV</LegalForm>
            <Building>Atlantic House</Building>
            <StreetName>Noorderlaan</StreetName>
            <StreetNum>147</StreetNum>
            <Postcode>2030</Postcode>
            <City>ANTWERPEN</City>
            <CountryCode>BE</CountryCode>
            <Phone>03/5436373</Phone>
            <Language>NL</Language>
        </ns2:Data>
    </ns2:Company>
</ns2:ExportCompanies>

有了这个xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="urn:be:fgov:ehealth:samws:v2:company:submit" 
xmlns:ns2="urn:be:fgov:ehealth:samws:v2:export" 

  <xsl:template match="/ns2:ExportCompanies">
    <xsl:for-each select="ns2:Company">
      <xsl:text>INSERT INTO COMPANY VALUES ('</xsl:text>
      <xsl:value-of select="@actorNr" />
      <xsl:text>','</xsl:text>
      <xsl:for-each select="ns2:Data">
        <xsl:sort select="translate(@from,'-','')" order="descending" data-type="number" />
        <xsl:if test="position() = 1">
          <xsl:value-of select="@from" />
          <xsl:text>','</xsl:text>
          <xsl:value-of select="@to" />
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="AuthorisationNr" /> <!--DON'T WORK-->
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[2]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[3]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[4]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[6]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[7]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[8]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[9]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[10]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[11]" />
          </xsl:call-template>
          <xsl:text>','</xsl:text>
          <xsl:call-template name="escape-apos">
            <xsl:with-param name="string" select="child::*[12]" />
          </xsl:call-template>
        </xsl:if>
      </xsl:for-each>
      <xsl:text>');</xsl:text>
    </xsl:for-each>

  </xsl:template>
</xsl:stylesheet>

目标是使用SQL insert为数据库表“ COMPANY”创建文件。

我真的不明白为什么在我的<xsl:for-each select="ns2:Data">中我不能简单地使用相对表达式来获取第一个孩子“ AuthorisationNr”的值!

如果我使用此child::*[1] xPath表达式,但有时xml中缺少一些子节点,那么我就可以得到他的值。

在发布此消息之前,我尝试了很多相对/绝对/轴表达,但是我认为我缺少一些东西。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

您的尝试不起作用的原因是AuthorisationNr元素在命名空间中,而其所有同级元素也在其中。

代替声明默认的名称空间:

xmlns="urn:be:fgov:ehealth:samws:v2:company:submit" 

在您的xsl:stylesheet起始标记中(如果您的输出是文本*,则无效),为它分配一些前缀,例如:

xmlns:ns3="urn:be:fgov:ehealth:samws:v2:company:submit" 

然后使用此前缀选择此命名空间中的元素-例如:

<xsl:call-template name="escape-apos">
    <xsl:with-param name="string" select="ns3:AuthorisationNr" /> 
</xsl:call-template>

(*)并且如果您的输出是文本,那么您应该在样式表的顶层这么说:

<xsl:output method="text"/>