1

我正在尝试使用 XSLT 1.0 基于子元素名称展平 XML 元素

源 XML:

<Contact>
  <ContactPurpose>
    <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
  </ContactPurpose>
  <ContactPurpose>
    <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
  </ContactPurpose>
</Contact>

应转换为以下 XML:

<Contact>
  <ContactPurpose>O</ContactPurpose>
  <ContactPurpose>Call</ContactPurpose>
</Contact>

逻辑是:

如果子元素名称是“PurposeAsPlainText”,则在目标中为“Other”设置“O”

ELSEIF 子元素名称为“PurposeAsEnum” THEN 将源值复制到目标

编辑 1:我可以更清楚,因为没有一个解决方案使 xml 变平,请参阅修改后的源和目标 XML。

编辑 2:这是我正在测试的 XML。下面的两个转换解决方案实际上适用于我的原始 xml,但不适用于我使用 .NET 4.0 XslCompiledTransform 测试的修改后的 xml。还是我应该提出一个新问题?

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PatientRecord>
    <Demographics>
      <Contact>
        <ContactPurpose>
          <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
        </ContactPurpose>
        <ContactPurpose>
          <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
        </ContactPurpose>
      </Contact>
    </Demographics>
  </PatientRecord>
</MyDS>
4

3 回答 3

1
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cds="cds_dt" exclude-result-prefixes="cds">
<!-- identity transform - just copy things that don't have a better rule -->
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<!- a rule for what needs changing -->
<xsl:template match="ContactPurpose[cds:PurposeAsPlainText] ">
    <ContactPurpose>O</ContactPurpose>
</xsl:template>
</xsl:stylesheet>
于 2012-03-05T19:23:57.173 回答
1

更新:修改答案以适应更改的 XML 源文档。

描述不是很清楚,但这是我认为你正在尝试做的事情:

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

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

<xsl:template match="cds:ContactPurpose">
   <xsl:copy>
      <xsl:choose>
         <!-- when there is a child element PurposeAsPlainText
            in the cds_dt namespace: -->
         <xsl:when test="cds_dt:PurposeAsPlainText">0</xsl:when>
         <!-- I'm guessing that PurposeAsEnum is also supposed to be
            in the cds_dt namespace. -->
         <xsl:otherwise>
            <xsl:value-of select="cds_dt:PurposeAsEnum" />
         </xsl:otherwise>
      </xsl:choose>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>
于 2012-03-05T20:42:30.150 回答
1

这可以通过简单而简短的方式完成(没有明确的条件)

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="cds_dt" exclude-result-prefixes="x">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="ContactPurpose/x:PurposeAsPlainText/text()">0</xsl:template>

 <xsl:template match="ContactPurpose/*"><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>

当应用于以下 XML 文档时(扩展以包含两种感兴趣的情况):

<Contact>
    <ContactPurpose>
        <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
    </ContactPurpose>
    <ContactPurpose>
        <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
    </ContactPurpose>
</Contact>

产生想要的正确结果

<Contact>
   <ContactPurpose>0</ContactPurpose>
   <ContactPurpose>Call</ContactPurpose>
</Contact>

说明

覆盖身份规则并适当使用模板/匹配模式。

更新:OP 修改了他的 XML 文档,该文档现在位于默认命名空间中:

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <PatientRecord>
        <Demographics>
            <Contact>
                <ContactPurpose>
                    <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
                </ContactPurpose>
                <ContactPurpose>
                    <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
                </ContactPurpose>
            </Contact>
        </Demographics>
    </PatientRecord>
</MyDS>

因此,这里是一个稍微修改的转换,它产生了想要的结果

<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:x="cds_dt" xmlns:c="cds" exclude-result-prefixes="c x">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

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

     <xsl:template match="c:ContactPurpose/x:PurposeAsPlainText/text()">0</xsl:template>

     <xsl:template match="c:ContactPurpose/*"><xsl:apply-templates/></xsl:template>
</xsl:stylesheet>

当这个转换应用于新的 XML 文档(上面最接近的)时,会产生新的想要的正确结果

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <PatientRecord>
      <Demographics>
         <Contact>
            <ContactPurpose>0</ContactPurpose>
            <ContactPurpose>Call</ContactPurpose>
         </Contact>
      </Demographics>
   </PatientRecord>
</MyDS>
于 2012-03-06T02:49:05.160 回答