Xslt 使用XLST 1.0进行多项更改(大写元素首字母、顺序元素、累加/分组元素)

Xslt 使用XLST 1.0进行多项更改(大写元素首字母、顺序元素、累加/分组元素),xslt,xslt-1.0,xslt-grouping,Xslt,Xslt 1.0,Xslt Grouping,我的输入xml如下所示: <myFamily> <spouse type="1">Halle Berry</spouse> <parent type="bio">Jane Smith-Doe</parent> <spouse type="2">Eva Longoria</spouse> <uncle type="pater

我的输入xml如下所示:

<myFamily>
  <spouse type="1">Halle Berry</spouse>
  <parent type="bio">Jane Smith-Doe</parent>
  <spouse type="2">Eva Longoria</spouse>
  <uncle type="paternal">Bob Beam</uncle>
  <parent type="bio">Jim Beam</parent>
  <uncle type="maternal">Mike Smith</uncle>
  <aunt type="paternal">Viola Davis</aunt>
  <inLaw type="mother">Dr. Curry-Pepper</inLaw>
  <brother name="Ron Isley">
    <child>Sara Isley</child>
    <child>Ron Isley Jr.</child>
    <child>Martha Isley-Focker</child>
  </brother>
  <parent type="step">Jon Doe</parent>
  <inLaw type="father">Dr. Pepper</inLaw>
  <spouse type="3">Sofia Vergara</spouse>
  <uncle type="paternal">Bo Beam</uncle>
  <spouse type="3">Sonya Curry</spouse>
  <Sister name ="Shelly Isley"/>
</myFamily>
<MyFamily>
  <Parents>
    <Parent type="bio">Jane Smith-Doe</Parent>
    <Parent type="bio">Jim Beam</Parent>
    <Parent type="step">Jon Doe</Parent>
  </Parents>
  <Siblings>
    <Sister name ="Shelly Isley"/>
    <Brother name="Ron Isley">
      <Child>Sara Isley</Child>
      <Child>Ron Isley Jr.</Child>
      <Child>Martha Isley-Focker</Child>
    </Brother>
  <Siblings>
  <Uncles>
    <Uncle type="paternal">Bob Beam</Uncle>
    <Uncle type="maternal">Mike Smith</Uncle>
    <Uncle type="paternal">Bo Beam</Uncle>  
  </Uncles>
  <Aunts><Aunt type="paternal">Viola Davis</Aunt><Aunts>
  <InLaws>
    <InLaw type="mother">Dr. Curry-Pepper</InLaw>
    <InLaw type="father">Dr. Pepper</InLaw>
  </InLaws>
  <Wives>
    <Wife type="1">Halle Berry</Wife>
    <Wife type="2">Eva Longoria</Wife>
    <Wife type="3">Sofia Vergara</Wife>
    <Wife type="3">Sonya Curry</Wife>
  </Wives>
</MyFamily>

哈莉·贝瑞
简·史密斯·多伊
伊娃·朗格利亚
摆梁
占边
迈克·史密斯

),但我不明白。老实说,我几乎什么都不懂。

对于您的示例中所示的预定义组,您可以简单地执行以下操作:

<xsl:template match="/myFamily">
    <MyFamily>
        <Parents>
            <xsl:apply-templates select="parent"/>
        </Parents>
        <Siblings>
            <xsl:apply-templates select="brother | sister"/>
        </Siblings>
        
        <!-- continue for other groups -->
        
    </MyFamily>
</xsl:template>

或者,如果您希望省略空组:

<xsl:template match="/myFamily">
    <MyFamily>
        <xsl:variable name="parents" select="parent" />
        <xsl:if test="$parents">
            <Parents>
                <xsl:apply-templates select="$parents"/>
            </Parents>
        </xsl:if>
        
        <!-- continue for other groups -->
        
    </MyFamily>
</xsl:template>

对于示例中所示的预定义组,您可以简单地执行以下操作:

<xsl:template match="/myFamily">
    <MyFamily>
        <Parents>
            <xsl:apply-templates select="parent"/>
        </Parents>
        <Siblings>
            <xsl:apply-templates select="brother | sister"/>
        </Siblings>
        
        <!-- continue for other groups -->
        
    </MyFamily>
</xsl:template>

或者,如果您希望省略空组:

<xsl:template match="/myFamily">
    <MyFamily>
        <xsl:variable name="parents" select="parent" />
        <xsl:if test="$parents">
            <Parents>
                <xsl:apply-templates select="$parents"/>
            </Parents>
        </xsl:if>
        
        <!-- continue for other groups -->
        
    </MyFamily>
</xsl:template>


请一次问一个问题。了解XSLT1.0分组,我建议您阅读以下内容:。虽然使用预定义的组,但您可能不需要它。由于标题大小写不匹配(即,
Sara Isley
),您所需的结果格式不正确。记住XML是区分大小写的。为什么是
标题而不是
?您希望的解决方案具有多大的通用性?配偶永远是妻子吗?为什么不通过这些根节(即所有父节点、所有同级节点等)检索节点?这感觉像是一个课程问题。哎呀……就是让他们匹配大写字母。这是一个剪贴式的失败,请一次问一个问题。了解XSLT1.0分组,我建议您阅读以下内容:。虽然使用预定义的组,但您可能不需要它。由于标题大小写不匹配(即,
Sara Isley
),您所需的结果格式不正确。记住XML是区分大小写的。为什么是
标题而不是
?您希望的解决方案具有多大的通用性?配偶永远是妻子吗?为什么不通过这些根节(即所有父节点、所有同级节点等)检索节点?这感觉像是一个课程问题。哎呀……就是让他们匹配大写字母。这是一个剪贴式的失败,帮助很大。真不敢相信我没想到。如何使嵌套元素也大写?使用这种方法的人太多了。这个答案会改为,但无法修复。这就是为什么我要试着用翻译来翻译。尽管如此,这还是一个很大的帮助。只需添加另一个与样式表中最后一个模板匹配的模板即可。这非常有帮助。真不敢相信我没想到。如何使嵌套元素也大写?使用这种方法的人太多了。这个答案会改为,但无法修复。这就是为什么我要试着用翻译来翻译。不过,这还是一个很大的帮助。只需添加另一个与样式表中最后一个模板相同的模板即可。