简体   繁体   English

通过xsl转换创建多个xml,然后根据源xml的标签属性将数据放入这些xml中

[英]create multiple xmls from xsl transformation and place data into those xml's based on the tag attributes of source xml

I'm trying to create different xml files based on language name using xsl 2.0. 我正在尝试使用xsl 2.0根据语言名称创建不同的xml文件。 In my input xml, here there are only 2 languages, "en" and "es". 在我的输入xml中,这里只有“ en”和“ es”两种语言。

  1. All the "en" based persons will go to en.xml and 所有基于“ en”的人员都将转到en.xml,
  2. All the "es" based persons will go to es.xml and 所有基于“ es”的人员将转到es.xml,
  3. All the person who have both "en" and "es" will have to go to both en.xml and es.xml. 同时拥有“ en”和“ es”的所有人都必须同时访问en.xml和es.xml。

My Input XML: source xml is about the list of persons, their addresses and languages 我的输入XML:源xml关于人员列表,他们的地址和语言

<persons>
    <person name="Alice">
        <Addresses>
            <Address type ="personal">
                <language name = "en">
                </language>
            </Address>
            <Address type ="business">
                <language name = "en">
                </language>
            </Address>
        </Addresses>
    </person>
    <person name="Bob">
        <Addresses>
            <Address type ="personal">
                <language name = "es">
                </language>
            </Address>
            <Address type ="business">
                <language name = "es">
                </language>
            </Address>
        </Addresses>
    </person>
    <person name="Stacy">
        <Addresses>
            <Address type ="personal">
                <language name = "en">
                </language>
            </Address>
            <Address type ="business">
                <language name = "es">
                </language>
            </Address>
        </Addresses>
    </person>
</persons>

output file en.xml will have: 输出文件en.xml将具有:

all the persons containing language attribute value = "en" should go to the en.xml file. 所有包含language attribute value =“ en”的人员都应转到en.xml文件。 In this case, both Alice and Stacy will need to go to en.xml 在这种情况下,Alice和Stacy都需要转到en.xml

    <person name="Alice">
        <Addresses>
            <Address type ="personal">
                <language name = "en">
                </language>
            </Address>
            <Address type ="business">
                <language name = "en">
                </language>
            </Address>
        </Addresses>
    </person>
        <person name="Stacy">
        <Addresses>
            <Address type ="personal">
                <language name = "en">
                </language>
            </Address>
            <Address type ="business">
                <language name = "es">
                </language>
            </Address>
        </Addresses>
    </person>

output file es.xml will have: 输出文件es.xml将具有:

all the persons containing language attribute value = "es" should go to the en.xml file. 所有包含language attribute value =“ es”的人员都应转到en.xml文件。 In this case, both Bob and Stacy will need to go to es.xml 在这种情况下,Bob和Stacy都需要转到es.xml

    <person name="Bob">
        <Addresses>
            <Address type ="personal">
                <language name = "es">
                </language>
            </Address>
            <Address type ="business">
                <language name = "es">
                </language>
            </Address>
        </Addresses>
    </person>
    <person name="Stacy">
        <Addresses>
            <Address type ="personal">
                <language name = "en">
                </language>
            </Address>
            <Address type ="business">
                <language name = "es">
                </language>
            </Address>
        </Addresses>
    </person>

My XSL so far: 到目前为止,我的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="2.0">

<xsl:output method="xml" indent="yes" name="xml"/>

<xsl:template match="/">
    <xsl:for-each select="//persons">
       <xsl:variable name="filename"
             select="concat('allpersons/',//persons/person/Addresses/Address/language/@name,'.xml')" />
       <xsl:value-of select="$filename" />  <!-- Creating  -->
       <xsl:result-document href="{$filename}" format="xml">
             <xsl:value-of select="."/>
       </xsl:result-document>
     </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Solution should not contain static matching of attributes like "en", "es".. beacause there are lot of languages. 解决方案不应包含诸如“ en”,“ es”之类的属性的静态匹配。因为存在多种语言。 Any Help is well appreciated? 任何帮助都很好吗?

References: http://www.ibm.com/developerworks/xml/library/x-tipmultxsl/index.html 参考: http : //www.ibm.com/developerworks/xml/library/x-tipmultxsl/index.html

Here is an XSLT 2.0 sample: 这是XSLT 2.0示例:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="persons">
  <xsl:for-each-group select="person" group-by="Addresses/Address/language/@name">
    <xsl:result-document href="{current-grouping-key()}.xml">
      <xsl:apply-templates select="current-group()"/>
    </xsl:result-document>
  </xsl:for-each-group>
</xsl:template>

<xsl:template match="person/Addresses">
  <xsl:if test="Address/language/@name = current-grouping-key()">
    <xsl:next-match/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

I am not sure you really want to create those result files without a root but if you want a root change the persons template to 我不确定您是否真的要创建没有根目录的结果文件,但是如果您希望根目录,请将persons模板更改为

<xsl:template match="persons">
  <xsl:for-each-group select="person" group-by="Addresses/Address/language/@name">
    <xsl:result-document href="{current-grouping-key()}.xml">
      <persons>
        <xsl:apply-templates select="current-group()"/>
      </persons>
    </xsl:result-document>
  </xsl:for-each-group>
</xsl:template>

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

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