XSD / XSLT-将信息映射到类别

时间:2016-10-05 14:17:36

标签: xml xslt

我正在尝试将人员信息转换为不同类别。基本的想法是这样的: 来源:

<Person>
  <id1>1234</id1>
  <id2>4321</id2>
  <name>Doe</name>
  <firstname>John</firstname>
  <firstletter>J</firstletter>
  <sex>M</sex>
</Person>

期望的结果:

<Result>
  <Categories>
    <Category>
      <name>ids</name>
      <number>01</number>
      <SubCategories>
        <SubCategory>
          <name>id1</name>
          <number>01</number>
          <value>1234</value>
        </SubCategory>
        <SubCategory>
          <name>id2</name>
          <number>02</number>
          <value>4321</value>
        </SubCategory>
      </SubCategories>
    </Category>
    <Category>
      <name>info</name>
      <number>02</number>
      <SubCategories>
        <SubCategory>
          <name>lastname</name>
          <number>01</number>
          <value>Doe</value>
        </SubCategory>
        <SubCategory>
          <name>firstname</name>
          <number>02</number>
          <value>John</value>
        </SubCategory>
        <SubCategory>
          <name>gender</name>
          <number>03</number>
          <value>M</value>
        </SubCategory>
      </SubCategories>
    </Category>
  </Categories>
</Result>   

因此目标包含重复节点,但源不包含重复节点。我想将Person(不是所有)中的元素映射到正确的类别。 我知道哪个元素属于哪个类别和子类别(如所需结果中所示),但我不知道如何有条件地填充结果。

1 个答案:

答案 0 :(得分:0)

我大多在猜这里。也许你只想做这样的事情:

XSLT 1.0

<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="/Person">
    <Result>
        <Categories>
            <Category>
                <name>ids</name>
                <number>01</number>
                <SubCategories>
                    <SubCategory>
                        <name>id1</name>
                        <number>01</number>
                        <value>
                            <xsl:value-of select="id1"/>
                        </value>
                    </SubCategory>
                    <SubCategory>
                        <name>id2</name>
                        <number>02</number>
                        <value>
                            <xsl:value-of select="id2"/>
                        </value>
                    </SubCategory>
                </SubCategories>
            </Category>
            <Category>
                <name>info</name>
                <number>02</number>
                <SubCategories>
                    <SubCategory>
                        <name>lastname</name>
                        <number>01</number>
                        <value>
                            <xsl:value-of select="name"/>
                        </value>
                    </SubCategory>
                    <SubCategory>
                        <name>firstname</name>
                        <number>02</number>
                        <value>
                            <xsl:value-of select="firstname"/>
                        </value>
                    </SubCategory>
                    <SubCategory>
                        <name>gender</name>
                        <number>03</number>
                        <value>
                            <xsl:value-of select="sex"/>
                        </value>
                    </SubCategory>
                </SubCategories>
            </Category>
        </Categories>
    </Result>
</xsl:template>

</xsl:stylesheet>