如何通过XSLT转换XML并忽略2个XMLNS

时间:2018-09-24 12:43:10

标签: xml xslt xslt-1.0

sudo: false
language: android
jdk: oraclejdk8

before_cache:
  - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/

cache:
  directories:
  - $HOME/.gradle/caches/
  - $HOME/.gradle/wrapper/
  - $HOME/.android/build-cache


before_script: 
  - cd TreasurePleasure
  - echo no | android create avd --force -n test -t "android-"$ANDROID_EMULATOR_LEVEL --abi $ANDROID_ABI --tag $ANDROID_TAG
  - emulator -avd test -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &

android:
  components:
    - tools
    - tools # Running this twice get's the latest build tools (https://github.com/codepath/android_guides/wiki/Setting-up-Travis-CI)
    - platform-tools
    - build-tools-27.0.3
    - android-27
    - android-$ANDROID_EMULATOR_LEVEL
    - sys-img-armeabi-v7a-google_apis-$ANDROID_EMULATOR_LEVEL
  licenses:
    - '.+'

env:
  global:
    - ANDROID_API_LEVEL=27
    - ANDROID_EMULATOR_LEVEL=21
    - ANDROID_BUILD_TOOLS_VERSION=27.0.3
    - ANDROID_ABI=armeabi-v7a
    - ANDROID_TAG=google_apis
    - ADB_INSTALL_TIMEOUT=20

before_install:
  - chmod +x gradlew
  - yes | sdkmanager "platforms;android-27"


script:
  - "./gradlew clean build connectedCheck -PdisablePreDex --stacktrace"
  # run tests  against the emulator
  - ./gradlew connectedAndroidTest
  # run tests  against the JVM
  - ./gradlew test

如上所示。
如何修复XSLT文件?

<Products xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" 
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

<Product>
<status>1</status> 
<ProductName>******************</ProductName> 
<model>******************</model>
<ProductQTY>******************</ProductQTY> 
<CostPrice>******************</CostPrice>
<RetailPrice>******************</RetailPrice> 
<FullCategoryPath>******************</FullCategoryPath>
<description> 
<![CDATA[******************
]]></description> 
<image_link>https://******************</image_link> 
</Product>
<Product>
<status>1</status>
<ProductName>******************</ProductName>
<model>******************7</model>
<ProductQTY>0</ProductQTY> 
<CostPrice>1******************</CostPrice>
<RetailPrice>141.9</RetailPrice> 
<FullCategoryPath>******************</FullCategoryPath><description> 
<![CDATA******************
  ]]></description> 
<image_link>https://******************</image_link>
</Product>
</Products>    <!-- Added by edit -->

例如:
如何显示类别名称产品名称ex。从我的node()。我无法解析它们。

  

xmlns =“ http://www.sitemaps.org/schemas/sitemap/0.9”
  xmlns:image =“ http://www.google.com/schemas/sitemap-image/1.1”

这两个名称空间严重毁了我。它们在我的根元素“产品”中。

输出为:所有值均为false。

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
            xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" 
            xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"               
            exclude-result-prefixes="sitemap image">

    <xsl:output method="xml" 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="*">
        <xml>
            <xsl:for-each select="node()">
                <xsl:choose >
                    <xsl:when test="node() | model !=''">
                        <Product>     
                            <xsl:apply-templates select="@* | node() "/>
                            <SProductCode >
                                <xsl:value-of select="node() | @model" />
                            </SProductCode>
                            <CategoryName>
                                <value-of select=""/>
                            </CategoryName>
                            .
                            .
                            .
                            .
                        </Product>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </xml>
    </xsl:template>
</xsl:stylesheet>    <!-- Added by edit -->

我应该为“ SProductCode”或“ CategoryName”编写什么以使其正常运行?

1 个答案:

答案 0 :(得分:0)

以下修改后的模板接近您所需的输出。我删除了一些不必要的代码,但无法导出要放入xsl:value-of select="..." />中的值。我还将您的xsl:choose更改为Product xsl:for-each循环的简单谓词。

<xsl:template match="/Products">
    <xml>
        <xsl:for-each select="Product[model != '']">
            <Product>     
                <SProductCode >
                    <xsl:value-of select="model" />
                </SProductCode>
                <CategoryName>
                    <xsl:value-of select="FullCategoryPath"/>
                </CategoryName>
                .
                .
                .
                .
            </Product>
        </xsl:for-each>
    </xml>
</xsl:template>

输出为:

<xml>
    <Product>
        <SProductCode>******************</SProductCode>
        <CategoryName>******************</CategoryName>
        .
        .
        .
        .
    </Product>
    <Product>
        <SProductCode>******************7</SProductCode>
        <CategoryName>******************</CategoryName>
        .
        .
        .
        .
    </Product>
</xml>