Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml xslt测试属性是否存在_Xml_Xslt - Fatal编程技术网

Xml xslt测试属性是否存在

Xml xslt测试属性是否存在,xml,xslt,Xml,Xslt,我有以下xslt。我注意到有时候元素名“tuple”有一个属性。我想删除该属性并将其添加为元素。我添加了一个测试来验证“tuple”是否有属性,但它返回一个空白的“ecatalogue”元素 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output

我有以下xslt。我注意到有时候元素名“tuple”有一个属性。我想删除该属性并将其添加为元素。我添加了一个测试来验证“tuple”是否有属性,但它返回一个空白的“ecatalogue”元素

<?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" version="1.0" encoding="UTF-8"
        indent="yes" />
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="atom">
        <xsl:element name="{@name}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    <xsl:template match="table">
        <xsl:element name="{@name}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    <!--this test doesn't work properly -->
    <xsl:template match="tuple">
      <xsl:choose>
        <xsl:when test="@name">
             <xsl:apply-templates />
        </xsl:when>
        <xsl:otherwise>
            <!-- nothing to do
            the node should stay the same
        -->
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    <!-- end test -->
</xsl:stylesheet>
结果我有这个模板上面

<ecatalogue>

</ecatalogue>

对模板匹配元组进行以下调整

如果一个元组有一个name属性,则该元组在写入时没有属性,该属性作为元素添加时没有任何值,因为不清楚它是否应该有任何值,并且复制了子节点:

<xsl:when test="@name">
  <tuple>
    <xsl:element name="{@name}"/>
    <xsl:apply-templates />
  </tuple>
</xsl:when>
输入XML的一部分,例如:

<tuple name="ObjManufacturerRef">
  <NamOrganisation>Unknown;:;Inconnu</NamOrganisation>
  <NamOrganisationAcronym>Unknown</NamOrganisationAcronym>
  <AddPhysCountry>Unknown</AddPhysCountry>
</tuple>
结果:

<tuple>
  <ObjManufacturerRef/>
  <NamOrganisation>Unknown;:;Inconnu</NamOrganisation>
  <NamOrganisationAcronym>Unknown</NamOrganisationAcronym>
  <AddPhysCountry>Unknown</AddPhysCountry>
</tuple>

保存的示例:使用添加的删除空白。

对模板匹配元组进行以下调整

如果一个元组有一个name属性,则该元组在写入时没有属性,该属性作为元素添加时没有任何值,因为不清楚它是否应该有任何值,并且复制了子节点:

<xsl:when test="@name">
  <tuple>
    <xsl:element name="{@name}"/>
    <xsl:apply-templates />
  </tuple>
</xsl:when>
输入XML的一部分,例如:

<tuple name="ObjManufacturerRef">
  <NamOrganisation>Unknown;:;Inconnu</NamOrganisation>
  <NamOrganisationAcronym>Unknown</NamOrganisationAcronym>
  <AddPhysCountry>Unknown</AddPhysCountry>
</tuple>
结果:

<tuple>
  <ObjManufacturerRef/>
  <NamOrganisation>Unknown;:;Inconnu</NamOrganisation>
  <NamOrganisationAcronym>Unknown</NamOrganisationAcronym>
  <AddPhysCountry>Unknown</AddPhysCountry>
</tuple>
保存的示例:使用添加的删除空白

我注意到有时候元素名“tuple”有一个属性。我 要删除该属性并将其添加为元素。我添加了一个测试 验证“tuple”是否具有属性

使用XSLT时,这绝对不是最好的方法。您希望转换属性,而不是其父元组元素-因此模板应直接匹配属性,例如:

<xsl:template match="tuple/@*">
    <xsl:element name="{name()}">
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>
此处不需要测试:如果属性存在,模板将匹配并处理它;否则,将根本不应用该模板

-

注意:以上假设您希望将属性转换为tuple的子元素,与另一个已经存在的子元素同级。您的帖子对此并不十分清楚

我注意到有时候元素名“tuple”有一个属性。我 要删除该属性并将其添加为元素。我添加了一个测试 验证“tuple”是否具有属性

使用XSLT时,这绝对不是最好的方法。您希望转换属性,而不是其父元组元素-因此模板应直接匹配属性,例如:

<xsl:template match="tuple/@*">
    <xsl:element name="{name()}">
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>
此处不需要测试:如果属性存在,模板将匹配并处理它;否则,将根本不应用该模板

-


注意:以上假设您要将属性转换为tuple的子元素,与另一个已经存在的子元素同级。您的帖子在这一点上不太清楚。

如果您提供了一个示例源XML文件,以及您希望它转换为什么,这将非常有帮助。谢谢Mitch!我的主要链接不起作用…@PapoucheGuinslyzinho属性将始终是@name,它不能有其他名称吗?@lingamurthy cs不必要。只是当我将原始xml转换为json时。它看起来乱七八糟的元组>表格>元组。。。所以我想用xslt将其转换为一个更简单的版本,我可以将其转换为json。名称>数据内容。matthias_h的解决方案很好,但如果你有其他解决方案,你仍然可以发布。谢谢如果您提供了一个示例源XML文件,以及希望它转换为什么,这将非常有用。谢谢Mitch!我的主要链接不起作用…@PapoucheGuinslyzinho属性将始终是@name,它不能有其他名称吗?@lingamurthy cs不必要。只是当我将原始xml转换为json时。它看起来乱七八糟的元组>表格>元组。。。所以我想用xslt将其转换为一个更简单的版本,我可以将其转换为json。名称>数据内容。matthias_h的解决方案很好,但如果你有其他解决方案,你仍然可以发布。谢谢谢谢@matthias_h的回答。我将对其进行修改,使其输出:tuple>objmanufacturerf>children[nameOr…nameOr..AddPhy]。感谢xsltransform.net我不知道那个网站。谢谢@matthias_h的回答。我将对其进行修改,使其输出:tuple>objmanufacturerf>children[nameOr…nameOr..AddPhy]。谢谢你的xsltransform.net,我不知道那个网站。